19 lines
758 B
JavaScript
19 lines
758 B
JavaScript
|
'use strict';
|
||
|
var lengthOfArrayLike = require('../internals/length-of-array-like');
|
||
|
var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
|
||
|
|
||
|
var $RangeError = RangeError;
|
||
|
|
||
|
// https://tc39.es/proposal-change-array-by-copy/#sec-array.prototype.with
|
||
|
// https://tc39.es/proposal-change-array-by-copy/#sec-%typedarray%.prototype.with
|
||
|
module.exports = function (O, C, index, value) {
|
||
|
var len = lengthOfArrayLike(O);
|
||
|
var relativeIndex = toIntegerOrInfinity(index);
|
||
|
var actualIndex = relativeIndex < 0 ? len + relativeIndex : relativeIndex;
|
||
|
if (actualIndex >= len || actualIndex < 0) throw new $RangeError('Incorrect index');
|
||
|
var A = new C(len);
|
||
|
var k = 0;
|
||
|
for (; k < len; k++) A[k] = k === actualIndex ? value : O[k];
|
||
|
return A;
|
||
|
};
|