-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolyfills.js
64 lines (52 loc) · 1.48 KB
/
polyfills.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
/* eslint-disable no-bitwise, no-extend-native, prefer-rest-params */
module.exports = function polyfills() {
// polyfill for Array.prototype.fill
if (!Array.prototype.fill) {
Object.defineProperty(Array.prototype, 'fill', {
value(value) {
// Steps 1-2.
if (this == null) {
throw new TypeError('this is null or not defined');
}
const O = Object(this);
// Steps 3-5.
const len = O.length >>> 0;
// Steps 6-7.
const start = arguments[1];
const relativeStart = start >> 0;
// Step 8.
let k = relativeStart < 0
? Math.max(len + relativeStart, 0)
: Math.min(relativeStart, len);
// Steps 9-10.
const end = arguments[2];
const relativeEnd = end === undefined
? len : end >> 0;
// Step 11.
const final = relativeEnd < 0
? Math.max(len + relativeEnd, 0)
: Math.min(relativeEnd, len);
// Step 12.
while (k < final) {
O[k] = value;
k += 1;
}
// Step 13.
return O;
},
});
}
// polyfill for Object.entries
if (!Object.entries) {
Object.entries = function entries(obj) {
const ownProps = Object.keys(obj);
let i = ownProps.length;
const resArray = new Array(i); // preallocate the Array
while (i) {
i -= 1;
resArray[i] = [ownProps[i], obj[ownProps[i]]];
}
return resArray;
};
}
};