You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

17 lines
740 B

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 $RangeError('Incorrect index');
var A = new C(len);
var k = 0;
for (; k < len; k++) A[k] = k === actualIndex ? value : O[k];
return A;
};