-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathPolyfill.js
414 lines (407 loc) · 15.3 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* Polyfill.js *
* *
* Polyfill for JavaScript. *
* *
* LastModified: Nov 18, 2016 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
(function (generic, undefined) {
'use strict';
/* Function */
if (!Function.prototype.bind) {
Object.defineProperty(Function.prototype, 'bind', { value: function(oThis) {
if (typeof this !== 'function') {
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var aArgs = Array.prototype.slice.call(arguments, 1),
toBind = this,
NOP = function() {},
bound = function() {
return toBind.apply(this instanceof NOP ? this : oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
if (this.prototype) {
NOP.prototype = this.prototype;
}
bound.prototype = new NOP();
return bound;
} });
}
/* Array */
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', { value: function(searchElement /*, fromIndex*/ ) {
var O = Object(this);
var len = parseInt(O.length, 10) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1], 10) || 0;
var k;
if (n >= 0) {
k = n;
}
else {
k = len + n;
if (k < 0) { k = 0; }
}
var currentElement;
while (k < len) {
currentElement = O[k];
if (searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
return true;
}
k++;
}
return false;
} });
}
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', { value: function(predicate) {
if (this === null || this === undefined) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = 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;
} });
}
if (!Array.prototype.findIndex) {
Object.defineProperty(Array.prototype, 'findIndex', { value: function(predicate) {
if (this === null || this === undefined) {
throw new TypeError('Array.prototype.findIndex called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = 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 i;
}
}
return -1;
} });
}
if (!Array.prototype.fill) {
Object.defineProperty(Array.prototype, 'fill', { value: function(value) {
if (this === null || this === undefined) {
throw new TypeError('this is null or not defined');
}
var O = Object(this);
var len = O.length >>> 0;
var start = arguments[1];
var relativeStart = start >> 0;
var k = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
var end = arguments[2];
var relativeEnd = end === undefined ? len : end >> 0;
var f = relativeEnd < 0 ? Math.max(len + relativeEnd, 0) : Math.min(relativeEnd, len);
while (k < f) {
O[k] = value;
k++;
}
return O;
} });
}
if (!Array.prototype.copyWithin) {
Object.defineProperty(Array.prototype, 'copyWithin', { value: function(target, start/*, end*/) {
if (this === null || this === undefined) {
throw new TypeError('this is null or not defined');
}
var O = Object(this);
var len = O.length >>> 0;
var relativeTarget = target >> 0;
var to = relativeTarget < 0 ? Math.max(len + relativeTarget, 0) : Math.min(relativeTarget, len);
var relativeStart = start >> 0;
var from = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
var end = arguments[2];
var relativeEnd = end === undefined ? len : end >> 0;
var f = relativeEnd < 0 ? Math.max(len + relativeEnd, 0) : Math.min(relativeEnd, len);
var count = Math.min(f - from, len - to);
var direction = 1;
if (from < to && to < (from + count)) {
direction = -1;
from += count - 1;
to += count - 1;
}
while (count > 0) {
if (from in O) {
O[to] = O[from];
}
else {
delete O[to];
}
from += direction;
to += direction;
count--;
}
return O;
} });
}
if (!Array.from) {
Object.defineProperty(Array, 'from', { value: (function() {
var toStr = Object.prototype.toString;
var isCallable = function(fn) {
return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
};
var toInteger = function(value) {
var number = Number(value);
if (isNaN(number)) { return 0; }
if (number === 0 || !isFinite(number)) { return number; }
return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
};
var maxSafeInteger = Math.pow(2, 53) - 1;
var toLength = function(value) {
var len = toInteger(value);
return Math.min(Math.max(len, 0), maxSafeInteger);
};
return function(arrayLike/*, mapFn, thisArg */) {
var C = this;
var items = Object(arrayLike);
if (arrayLike === null || arrayLike === undefined) {
throw new TypeError("Array.from requires an array-like object - not null or undefined");
}
var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
var T;
if (typeof mapFn !== 'undefined') {
if (!isCallable(mapFn)) {
throw new TypeError('Array.from: when provided, the second argument must be a function');
}
if (arguments.length > 2) {
T = arguments[2];
}
}
var len = toLength(items.length);
var A = isCallable(C) ? Object(new C(len)) : new Array(len);
var k = 0;
var kValue;
while (k < len) {
kValue = items[k];
if (mapFn) {
A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
}
else {
A[k] = kValue;
}
k += 1;
}
A.length = len;
return A;
};
}()) });
}
if (!Array.of) {
Object.defineProperty(Array, 'of', { value: function() {
return Array.prototype.slice.call(arguments);
} });
}
/* String */
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', { value: function(searchString, position){
position = position || 0;
return this.substr(position, searchString.length) === searchString;
} });
}
if (!String.prototype.endsWith) {
Object.defineProperty(String.prototype, 'endsWith', { value: function(searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
} });
}
if (!String.prototype.includes) {
Object.defineProperty(String.prototype, 'includes', { value: function() {
if (typeof arguments[1] === "number") {
if (this.length < arguments[0].length + arguments[1].length) {
return false;
}
else {
return this.substr(arguments[1], arguments[0].length) === arguments[0];
}
}
else {
return String.prototype.indexOf.apply(this, arguments) !== -1;
}
} });
}
if (!String.prototype.repeat) {
Object.defineProperty(String.prototype, 'repeat', { value: function(count) {
var str = this.toString();
count = +count;
if (count !== count) {
count = 0;
}
if (count < 0) {
throw new RangeError('repeat count must be non-negative');
}
if (count === Infinity) {
throw new RangeError('repeat count must be less than infinity');
}
count = Math.floor(count);
if (str.length === 0 || count === 0) {
return '';
}
// Ensuring count is a 31-bit integer allows us to heavily optimize the
// main part. But anyway, most current (August 2014) browsers can't handle
// strings 1 << 28 chars or longer, so:
if (str.length * count >= 1 << 28) {
throw new RangeError('repeat count must not overflow maximum string size');
}
var rpt = '';
for (;;) {
if ((count & 1) === 1) {
rpt += str;
}
count >>>= 1;
if (count === 0) {
break;
}
str += str;
}
// Could we try:
// return Array(count + 1).join(this);
return rpt;
} });
}
if (!String.prototype.trim) {
Object.defineProperty(String.prototype, 'trim', { value: function() {
return this.toString().replace(/^[\s\xa0]+|[\s\xa0]+$/g, '');
} });
}
if (!String.prototype.trimLeft) {
Object.defineProperty(String.prototype, 'trimLeft', { value: function() {
return this.toString().replace(/^[\s\xa0]+/, '');
} });
}
if (!String.prototype.trimRight) {
Object.defineProperty(String.prototype, 'trimRight', { value: function() {
return this.toString().replace(/[\s\xa0]+$/, '');
} });
}
/* Object */
if (!Object.keys) {
Object.defineProperty(Object, 'keys', { value: (function () {
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function (obj) {
if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) {
throw new TypeError('Object.keys called on non-object');
}
var result = [];
for (var prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
if (hasDontEnumBug) {
for (var i=0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
}
return result;
};
})() });
}
function genericMethods(obj, properties) {
var proto = obj.prototype;
for (var i = 0, len = properties.length; i < len; i++) {
var property = properties[i];
var method = proto[property];
if (typeof method === 'function' && typeof obj[property] === 'undefined') {
Object.defineProperty(obj, property, { value: generic(method) });
}
}
}
genericMethods(Array, [
"pop",
"push",
"reverse",
"shift",
"sort",
"splice",
"unshift",
"concat",
"join",
"slice",
"indexOf",
"lastIndexOf",
"filter",
"forEach",
"every",
"map",
"some",
"reduce",
"reduceRight",
"includes",
"find",
"findIndex"
]);
genericMethods(String, [
'quote',
'substring',
'toLowerCase',
'toUpperCase',
'charAt',
'charCodeAt',
'indexOf',
'lastIndexOf',
'include',
'startsWith',
'endsWith',
'repeat',
'trim',
'trimLeft',
'trimRight',
'toLocaleLowerCase',
'toLocaleUpperCase',
'match',
'search',
'replace',
'split',
'substr',
'concat',
'slice'
]);
})(hprose.generic);