-
Notifications
You must be signed in to change notification settings - Fork 4
/
polyfills.js
75 lines (64 loc) · 2.15 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
65
66
67
68
69
70
71
72
73
74
75
'use strict'
define(function(require) {
if (!Array.prototype.flat) {
// From https://github.com/jonathantneal/array-flat-polyfill
console.log('!Polyfilling flat')
Object.defineProperty(Array.prototype, 'flat', {
configurable: true,
value: function flat () {
var depth = isNaN(arguments[0]) ? 1 : Number(arguments[0]);
return depth ? Array.prototype.reduce.call(this, function (acc, cur) {
if (Array.isArray(cur)) {
acc.push.apply(acc, flat.call(cur, depth - 1));
} else {
acc.push(cur);
}
return acc;
}, []) : Array.prototype.slice.call(this);
},
writable: true
});
}
if (!Array.prototype.flatMap) {
// From https://github.com/jonathantneal/array-flat-polyfill
console.log('!Polyfilling flatMap')
Object.defineProperty(Array.prototype, 'flatMap', {
configurable: true,
value: function flatMap (callback) {
return Array.prototype.map.apply(this, arguments).flat();
},
writable: true
});
}
if (!Array.prototype.toSorted) {
// From https://github.com/es-shims/Array.prototype.toSorted
console.log('!Polyfilling toSorted')
Object.defineProperty(Array.prototype, 'toSorted', {
configurable: true,
value: function toSorted (comparefn) {
if (typeof comparefn !== 'function') { throw '`comparefn` must be a function'; }
var len = this.length;
var A = [];
for (var j=0; j<len; j++) { A[j] = this[j]; }
A.sort(comparefn);
return A;
},
writable: true
});
}
if (!Object.fromEntries) {
// From https://gitlab.com/moongoal/js-polyfill-object.fromentries/-/blob/master/index.js
console.log('!Polyfilling fromEntries')
Object.defineProperty(Object, 'fromEntries', {
value(entries) {
if (!entries || !entries[Symbol.iterator]) { throw new Error('Object.fromEntries() requires a single iterable argument'); }
const o = {};
Object.keys(entries).forEach((key) => {
const [k, v] = entries[key];
o[k] = v;
});
return o;
},
});
}
})