-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpolyfill.js
67 lines (54 loc) · 1.83 KB
/
polyfill.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
61
62
63
64
65
66
67
'use strict';
var GetIntrinsic = require('get-intrinsic');
var IsArray = require('es-abstract/2024/IsArray');
var HasOwnProperty = require('es-abstract/2024/HasOwnProperty');
var DefinePropertyOrThrow = require('es-abstract/2024/DefinePropertyOrThrow');
var hasPropertyDescriptors = require('has-property-descriptors')();
var callBind = require('call-bind');
var $ArrayPrototype = GetIntrinsic('%Array.prototype%');
var implementation = require('./implementation');
var $pushApply = callBind.apply(GetIntrinsic('%Array.prototype.push%'));
module.exports = function getPolyfill() {
if (!$ArrayPrototype.push) {
return implementation;
}
var pushIsNotGeneric = (function () {
var obj = {};
var result = $ArrayPrototype.push.call(obj, undefined);
return result !== 1 || obj.length !== 1 || typeof obj[0] !== 'undefined' || !HasOwnProperty(obj, '0');
}());
if (pushIsNotGeneric) {
var implApply = callBind.apply(implementation);
/* eslint-disable no-invalid-this */
// eslint-disable-next-line no-unused-vars
return function push(item) {
if (IsArray(this)) {
return $pushApply(this, arguments);
}
return implApply(this, arguments);
};
}
// This fixes a very weird bug in Opera 10.6 when pushing `undefined`
var pushUndefinedIsWeird = (function () {
var arr = [];
var result = arr.push(undefined);
return result !== 1 || arr.length !== 1 || typeof arr[0] !== 'undefined' || !HasOwnProperty(arr, '0');
}());
if (pushUndefinedIsWeird) {
return implementation;
}
var inf = { length: Infinity };
try {
$ArrayPrototype.call(inf);
return implementation;
} catch (e) { /**/ }
if (hasPropertyDescriptors) {
var nonwritable = [];
DefinePropertyOrThrow(nonwritable, 'length', { '[[Writable]]': false });
try {
nonwritable.push();
return implementation;
} catch (e) { /**/ }
}
return $ArrayPrototype.push;
};