-
Notifications
You must be signed in to change notification settings - Fork 19
/
es.js
444 lines (414 loc) · 13.4 KB
/
es.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**
* @author xiaojue [[email protected]]
* @fileoverview ES5-6 shim for client javascript
*/
(function(win, doc, Arr, Str, D, M, Num, Obj, Reg, B, global, undef) {
var AP = Arr.prototype,
SP = Str.prototype,
DP = D.prototype,
NP = Num.prototype,
BP = B.prototype,
RP = Reg.prototype,
toString = Obj.prototype.toString,
is = function(type) {
return function(val) {
return toString.call(val) === '[object ' + type + ']';
};
},
isArr = is('Array'),
isFun = is('Function'),
isStr = is('String'),
isReg = is('RegExp');
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
function toInteger(num) {
var n = Num(num);
if (n != num || n === Infinity) { //isNaN,expect string
n = 0;
} else if (n !== 0 && n != (1 / 0) && n != - (1 / 0)) {
n = (n > 0 || - 1) * M.floor(M.abs(n));
}
return n;
}
var Arr5 = {
isArray: isArr
};
//http:https://es5.github.com/#x15.4.4.16
var ArrPro5 = {
//https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
every: function(fun, thisP) {
var O = Obj(this),
len = O.length >>> 0,
k = 0;
if (!isFun(fun)) throw new TypeError(fun + ' is not a function');
while (k < len) {
if (k in O) {
if (!fun.call(thisP, O[k], k, O)) return false;
}
k++;
}
return true;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
filter: function(fun, thisP) {
var O = Obj(this),
len = O.length >>> 0,
res = [];
if (!isFun(fun)) throw new TypeError(fun + ' is not a function');
for (var i = 0; i < len; i++) {
if (i in O) {
var value = O[i];
if (fun.call(thisP, value, i, O)) res.push(value);
}
}
return res;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
forEach: function(fun, thisP) {
var O = Obj(this),
len = O.length >>> 0,
k = 0;
if (!isFun(fun)) throw new TypeError(fun + ' is not a function');
while (k < len) {
if (k in O) {
fun.call(thisP, O[k], k, O);
}
k++;
}
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
indexOf: function(searchElement, fromIndex) {
var O = Obj(this),
k,
len = O.length >>> 0,
n = toInteger(fromIndex);
if (len === 0 || n >= len) return - 1;
k = M.max(n >= 0 ? n: len - M.abs(n), 0);
while (k < len) {
if (k in O && O[k] === searchElement) return k;
k++;
}
return - 1;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
lastIndexOf: function(searchElement, fromIndex) {
var O = Obj(this),
k,
n,
len = O.length >>> 0;
if (len === 0) return - 1;
n = len - 1;
if (arguments.length > 1) {
n = toInteger(fromIndex);
}
for (k = n >= 0 ? M.min(n, len - 1) : len - M.abs(n); k >= 0; k--) {
if (k in O && O[k] === searchElement) return k;
}
return - 1;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
map: function(fun, thisP) {
var O = Obj(this),
len = O.length >>> 0,
t,
arr = new Arr(len),
k = 0;
if (!isFun(fun)) throw new TypeError(fun + ' is not a function');
while (k < len) {
if (k in O) {
arr[k] = fun.call(thisP, O[k], k, O);
}
k++;
}
return arr;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
some: function(fun, thisP) {
var O = Obj(this),
len = O.length >>> 0;
if (!isFun(fun)) throw new TypeError(fun + ' is not a function');
for (var i = 0; i < len; i++) {
if (i in O && fun.call(thisP, O[i], i, O)) return true;
}
return false;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
reduce: function(fun, initialValue) {
var O = Obj(this);
return reduce(O, fun, initialValue, 'left');
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight
reduceRight: function(fun, initialValue) {
var O = Obj(this);
return reduce(O, fun, initialValue, 'right');
}
};
function reduce(source, fun, initialValue, type) {
var len = source.length >>> 0,
isLeft = type === 'left',
k = isLeft ? 0: len - 1,
t,
index,
express,
value;
if (!isFun(fun)) throw new TypeError(fun + ' is not a function');
if (initialValue) {
value = initialValue;
} else {
do {
if (k in source) {
value = source[k];
k += ( !! isLeft || - 1);
break;
}
index = isLeft ? k + 1: k - 1;
express = isLeft ? index >= len: index < 0;
if (express) throw new TypeError('Reduce of empty array with no initial value');
} while (true);
}
while (isLeft ? k < len: k >= 0) {
if (k in source) value = fun.call(undefined, value, source[k], k, source);
k += ( !! isLeft || - 1);
}
return value;
}
var Arr6 = {
from: function() {
},
of: function() {
}
};
var ArrPro6 = {
copyWithin: function() {},
fill: function() {},
find: function() {},
findIndex: function() {},
keys: function() {},
entries: function() {},
values: function() {}
};
var StrPro5 = {
split: function() {},
trim: function() {},
replace: function() {}
};
var Str6 = {
fromCodePoint: function() {},
raw: function() {}
};
var StrPro6 = {
normalize: function() {},
codePointAt: function() {},
repeat: function() {},
startsWith: function() {},
endsWith: function() {},
includes: function() {}
};
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
function pad(number) {
if (number < 10) return '0' + number;
return number;
}
var D5 = {
now: function() {
return new D().getTime();
},
toISOString: function() {
return this.getUTCFullYear() + '-' + pad(this.getUTCMonth() + 1) + '-' + pad(this.getUTCDate()) + 'T' + pad(this.getUTCHours()) + ':' + pad(this.getUTCMinutes()) + ':' + pad(this.getUTCSeconds()) + '.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) + 'Z';
}
};
var DPro5 = {
parse: function() {},
toJSON: function() {}
};
//http:https://baike.baidu.com/link?url=FVj1cXvzfX11Gnf-0kPD4AAVXZh6l631NaKvU_lGW7tHTjk9JqZqRH0io4to_bna7NLczhfHcbMG0l8b88jW5a 双曲线百科
//http:https://baike.baidu.com/view/6688537.htm 反双曲线百科
var M6 = {
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/acosh
//acosh 函数返回一个数的反双曲余弦。
acosh: function(x) {
return M.log(x + M.sqrt(x * x - 1));
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/asinh
//asinh 函数返回参数的反双曲正弦值
asinh: function(x) {
if (x === - Infinity) return x;
else return M.log(x + M.sqrt(x * x + 1));
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atanh
//atanh 返回参数的反双曲正切值
atanh: function(x) {
return Math.log((1 + x) / (1 - x)) / 2;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cbrt
//cbrt 函数返回 x 的立方根值。这个函数不会失败;任何可表示的实数总有一个可表示的立方根。
cbrt: function(x) {
var y = Math.pow(Math.abs(x), 1 / 3);
return x < 0 ? - y: y;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/clz32
//CountLeadingZeroes32 计算一个数字在32位无符号位整形数字的二进制形式开头有多少个0。
clz32: function(x) {
var value = Number(x) >>> 0;
return value ? 32 - value.toString(2).length: 32;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cosh
//cosh 返回数字的双曲余弦值。
cosh: function(x) {
var y = Math.exp(x);
return (y + 1 / y) / 2;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/expm1
//expm1 计算参数的以 e 为底的指数,减去 1
expm1: function(x) {
return Math.exp(x) - 1;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot
//hypot 函数计算一直角三角形的斜边长度。
hypot: function(x, y) {
var r = 0;
if (M.abs(x) === Infinity || M.abs(y) === Infinity) return Infinity;
r += x * y;
return M.sqrt(y);
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2
//log2 计算参数以 2 为底的对数
log2: function(x) {
return M.log(x) / M.LN2;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10
//log10 计算参数以 10 为底的对数
log10: function(x) {
return M.log(x) / M.LN10;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log1p
//log1p 计算参数加1的对数值
log1p: function(x) {
return Math.log(1 + x);
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
//sign 判断一个数的符号位
sign: function(x) {
x = + x; // convert to a number
if (x === 0 || isNaN(x)) return x;
return x > 0 ? 1: - 1;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sinh
//sinh 双曲正弦函数
sinh: function(x) {
var y = M.exp(x);
return (y - 1 / y) / 2;
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/tanh
//tanh 双曲线正切函数
tanh: function(x) {
if (x === Infinity) {
return 1;
} else if (x === - Infinity) {
return - 1;
} else {
var y = M.exp(2 * x);
return (y - 1) / (y + 1);
}
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc
//trunc 取整函数
trunc: function(x) {
return x < 0 ? M.ceil(x) : M.floor(x);
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul
//imul 该函数返回两个参数的类C的32位整数乘法运算的运算结果.
imul: function(a, b) {
var ah = (a >>> 16) & 0xffff;
var al = a & 0xffff;
var bh = (b >>> 16) & 0xffff;
var bl = b & 0xffff;
return ((al * bl) + (((ah * bl + al * bh) << 16) >>> 0) | 0);
},
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround
//fround 将参数中指定的数字转换为离它最近的单精度浮点数形式.
fround: function(x) {
if (x === 0 || x === Infinity || x === - Infinity || isNaN(x)) {
return x;
}
var num = Num(x);
//TODO
return numberConversion.toFloat32(num);
}
};
var NumPro5 = {
toJSON: function() {},
toFixed: function() {}
};
var Num6 = {
MAX_SAFE_INTEGER: '',
MIN_SAFE_INTEGER: '',
EPSILON: '',
parseInt: function() {},
parseFloat: function() {},
isNaN: function() {},
isInteger: function() {},
isSafeInteger: function() {},
isFinite: function() {}
};
var BPro5 = {
toJSON: function() {}
};
var Obj5 = {
keys: function() {},
create: function() {},
getPrototypeOf: function() {},
getOwnPropertyNames: function() {},
defineProperty: function() {},
getOwnPropertDescriptor: function() {}
};
var Obj6 = {
keys: function() {},
is: function() {},
assign: function() {},
setPrototypeOf: function() {}
};
var RegPro6 = {
flags: function() {}
};
var Global5 = {
parseInt: function() {},
JSON: {
parse: function() {},
stringify: function() {}
}
};
var Global6 = {
Promise: function() {},
Map: function() {},
Set: function() {}
};
var supportES5 = checkES(Global5, global) && checkES(ArrPro5, AP),
supportES6 = checkES(Global6, global) && checkES(ArrPro6, AP);
function checkES(features, target) {
for (var feature in features) {
if (!target.hasOwnProperty(feature)) return false;
}
return true;
}
function extend() {
var args = arguments;
for (var i = 0; i < args.length; i++) {
var exts = args[i],
source = exts[0],
target = exts[1],
forceAssign = exts[3];
for (var j in source) {
//if (!forceAssign && (j in target)) continue;
target[j] = source[j];
}
}
}
//if (!supportES5) {
console.log(5);
extend([Arr5, Arr], [ArrPro5, AP], [StrPro5, SP], [D5, D], [DPro5, DP], [NumPro5, NP], [Obj5, Obj], [BPro5, BP], [Global5, global]);
//}
if (!supportES6) {
console.log(6);
extend([Arr6, Arr], [ArrPro6, AP], [Str6, Str], [StrPro6, SP], [Num6, Num], [M6, M], [Obj6, Obj, true], [RegPro6, RP], [Global6, global, true]);
}
})(window, document, Array, String, Date, Math, Number, Object, RegExp, Boolean, this);