es.array.unshift.js
1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
var $ = require('../internals/export');
var toObject = require('../internals/to-object');
var lengthOfArrayLike = require('../internals/length-of-array-like');
var setArrayLength = require('../internals/array-set-length');
var deletePropertyOrThrow = require('../internals/delete-property-or-throw');
var doesNotExceedSafeInteger = require('../internals/does-not-exceed-safe-integer');
// IE8-
var INCORRECT_RESULT = [].unshift(0) !== 1;
// V8 ~ Chrome < 71 and Safari <= 15.4, FF < 23 throws InternalError
var SILENT_ON_NON_WRITABLE_LENGTH = !function () {
try {
// eslint-disable-next-line es/no-object-defineproperty -- safe
Object.defineProperty([], 'length', { writable: false }).unshift();
} catch (error) {
return error instanceof TypeError;
}
}();
// `Array.prototype.unshift` method
// https://tc39.es/ecma262/#sec-array.prototype.unshift
$({ target: 'Array', proto: true, arity: 1, forced: INCORRECT_RESULT || SILENT_ON_NON_WRITABLE_LENGTH }, {
// eslint-disable-next-line no-unused-vars -- required for `.length`
unshift: function unshift(item) {
var O = toObject(this);
var len = lengthOfArrayLike(O);
var argCount = arguments.length;
if (argCount) {
doesNotExceedSafeInteger(len + argCount);
var k = len;
while (k--) {
var to = k + argCount;
if (k in O) O[to] = O[k];
else deletePropertyOrThrow(O, to);
}
for (var j = 0; j < argCount; j++) {
O[j] = arguments[j];
}
} return setArrayLength(O, len + argCount);
}
});