-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimplementation.js
60 lines (50 loc) · 2.09 KB
/
implementation.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
var DeletePropertyOrThrow = require('es-abstract/2024/DeletePropertyOrThrow');
var Get = require('es-abstract/2024/Get');
var HasProperty = require('es-abstract/2024/HasProperty');
var LengthOfArrayLike = require('es-abstract/2024/LengthOfArrayLike');
var Set = require('es-abstract/2024/Set');
var ToObject = require('es-object-atoms/ToObject');
var ToString = require('es-abstract/2024/ToString');
var forEach = require('es-abstract/helpers/forEach');
var MAX_SAFE_INTEGER = require('math-intrinsics/constants/maxSafeInteger');
var $TypeError = require('es-errors/type');
var callBound = require('call-bound');
var isString = require('is-string');
// Check failure of by-index access of string characters (IE < 9) and failure of `0 in boxedString` (Rhino)
var boxedString = ToObject('a');
var splitString = boxedString[0] !== 'a' || !(0 in boxedString);
var strSplit = callBound('String.prototype.split');
// eslint-disable-next-line no-unused-vars
module.exports = function unshift(items) {
var O = ToObject(this); // step 1
var self = splitString && isString(O) ? strSplit(O, '') : O;
var len = LengthOfArrayLike(self); // step 2
var argCount = arguments.length; // step 3
if (argCount > 0) { // step 4
if ((len + argCount) > MAX_SAFE_INTEGER) {
throw new $TypeError('unshift cannot produce an array of length larger than (2 ** 53) - 1'); // step 4.a
}
var k = len; // step 4.b
while (k > 0) { // step 4.c
var from = ToString(k - 1); // step 4.c.i
var to = ToString(k + argCount - 1); // step 4.c.ii
var fromPresent = HasProperty(O, from); // step 4.c.iii
if (fromPresent) { // step 4.c.iv
var fromValue = Get(O, from); // step 4.c.iv.1
Set(O, to, fromValue, true); // step 4.c.iv.2
} else { // step 4.c.v
DeletePropertyOrThrow(O, to); // step 4.c.v.2
}
k -= 1; // step 4.c.vi
}
var j = 0; // step 4.d
forEach(arguments, function (E) { // step 4.e
Set(O, ToString(j), E, true); // step 4.e.i
j += 1; // step 4.e.ii
});
}
Set(O, 'length', len + argCount, true); // step 5
return len + argCount; // step 6
};
module.exports.prototype = undefined;