-
Notifications
You must be signed in to change notification settings - Fork 608
Expand file tree
/
Copy pathhello.polyfill.js
More file actions
162 lines (135 loc) · 3.55 KB
/
Copy pathhello.polyfill.js
File metadata and controls
162 lines (135 loc) · 3.55 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// ES5 Object.create
if (!Object.create) {
// Shim, Object.create
// A shim for Object.create(), it adds a prototype to a new object
Object.create = (function () {
function F() {}
return function (o) {
if (arguments.length !== 1 || typeof o !== 'object' || o === null) {
throw new TypeError('Object.create implementation only accepts a non-null object.');
}
F.prototype = o;
return new F();
};
})();
}
// ES5 Object.keys
if (!Object.keys) {
Object.keys = function (o) {
if (o === null || typeof o !== 'object') {
throw new TypeError('Object.keys called on a non-object');
}
var r = [];
for (var k in o) {
if (Object.prototype.hasOwnProperty.call(o, k)) {
r.push(k);
}
}
return r;
};
}
/* eslint-disable no-extend-native */
// ES5 [].indexOf
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (s, fromIndex) {
var len = this.length >>> 0;
var start = fromIndex | 0;
if (len === 0) return -1;
start = Math.max(start >= 0 ? start : len - Math.abs(start), 0);
for (var j = start; j < len; j++) {
if (this[j] === s) {
return j;
}
}
return -1;
};
}
// ES5 [].forEach
if (!Array.prototype.forEach) {
Array.prototype.forEach = function (fun, thisArg) {
if (this == null) {
throw new TypeError('Array.prototype.forEach called on null or undefined');
}
if (typeof fun !== 'function') {
throw new TypeError(fun + ' is not a function');
}
var t = Object(this);
var len = t.length >>> 0;
for (var i = 0; i < len; i++) {
if (i in t) {
fun.call(thisArg, t[i], i, t);
}
}
};
}
// ES5 [].filter
if (!Array.prototype.filter) {
Array.prototype.filter = function (fun, thisArg) {
if (this == null) {
throw new TypeError('Array.prototype.filter called on null or undefined');
}
if (typeof fun !== 'function') {
throw new TypeError(fun + ' is not a function');
}
var a = [];
this.forEach(function (val, i, t) {
if (fun.call(thisArg || void 0, val, i, t)) {
a.push(val);
}
});
return a;
};
}
// ES5 [].map
if (!Array.prototype.map) {
Array.prototype.map = function (fun, thisArg) {
if (this == null) {
throw new TypeError('Array.prototype.map called on null or undefined');
}
if (typeof fun !== 'function') {
throw new TypeError(fun + ' is not a function');
}
var a = [];
this.forEach(function (val, i, t) {
a.push(fun.call(thisArg || void 0, val, i, t));
});
return a;
};
}
// ES5 isArray
if (!Array.isArray) {
Array.isArray = function (o) {
return Object.prototype.toString.call(o) === '[object Array]';
};
}
// Test for location.assign
if (typeof window === 'object' && typeof window.location === 'object' && !window.location.assign) {
window.location.assign = function (url) {
if (typeof url !== 'string') {
throw new TypeError('URL must be a string');
}
window.location.href = url;
};
}
// Test for Function.bind
if (!Function.prototype.bind) {
// MDN
// Polyfill IE8, does not support native Function.bind
Function.prototype.bind = function (context) {
if (typeof this !== 'function') {
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var args = Array.prototype.slice.call(arguments, 1);
var self = this;
var bound = function () {
return self.apply(this instanceof bound ? this : context, args.concat(Array.prototype.slice.call(arguments)));
};
function EmptyFunction() {}
if (this.prototype) {
EmptyFunction.prototype = this.prototype;
}
bound.prototype = new EmptyFunction();
return bound;
};
}
/* eslint-enable no-extend-native */