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