-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyfills.js
80 lines (71 loc) · 2.99 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
76
77
78
79
80
'use strict';
fin.polyfills = function(window) {
// IE 11 missing .find
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
if (!window.Array.prototype.find) {
window.Array.prototype.find = function (predicate) { // eslint-disable-line no-extend-native
if (this === null) {
throw new window.TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new window.TypeError('predicate must be a function');
}
var list = window.Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return value;
}
}
return undefined;
};
}
// IE 11 missing .findIndex
// Lite version of: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex#Polyfill
if (typeof window.Array.prototype.findIndex !== 'function') {
window.Array.prototype.findIndex = function(predicate) {
var context = arguments[1];
for (var i = 0, len = this.length; i < len; i++) {
if (predicate.call(context, this[i], i, this)) {
return i;
}
}
return -1;
};
}
// IE 11 missing CustomEvent constructor
if ( typeof window.CustomEvent !== "function" ) {
window.CustomEvent = function(event, params) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = window.document.createEvent('CustomEvent');
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
return evt;
};
window.CustomEvent.prototype = window.Event.prototype;
}
// IE 11 does not support 2nd param `force` of `classList.toggle`
if (window.DOMTokenList.prototype.toggle.toString().indexOf('key') < 0) {
var toggle = window.DOMTokenList.prototype.toggle;
window.DOMTokenList.prototype.toggle = function(key, force) {
if (arguments.length < 2) {
toggle.call(this, key);
} else switch (force) {
case true:
this.add(key);
break;
case false:
this.remove(key);
break;
}
};
}
// Some browsers already set NodeList.prototype.forEach but not IE 11 and none set others
// for return from querySelectorAll, getElementsBy*
['forEach', 'find', 'findIndex'].forEach(function(methodName) {
window.NodeList.prototype[methodName] = window.HTMLCollection.prototype[methodName] = window.Array.prototype[methodName];
});
};
fin.polyfills(window);