forked from snapapps/edgy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
es6-shim.js
2867 lines (2622 loc) · 98.2 KB
/
es6-shim.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* https://github.com/paulmillr/es6-shim
* @license es6-shim Copyright 2013-2015 by Paul Miller (http://paulmillr.com)
* and contributors, MIT License
* es6-shim: v0.27.1
* see https://github.com/paulmillr/es6-shim/blob/0.27.1/LICENSE
* Details and documentation:
* https://github.com/paulmillr/es6-shim/
*/
// UMD (Universal Module Definition)
// see https://github.com/umdjs/umd/blob/master/returnExports.js
(function (root, factory) {
/*global define, module, exports */
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.returnExports = factory();
}
}(this, function () {
'use strict';
var not = function notThunker(func) {
return function notThunk() { return !func.apply(this, arguments); };
};
var throwsError = function (func) {
try {
func();
return false;
} catch (e) {
return true;
}
};
var valueOrFalseIfThrows = function valueOrFalseIfThrows(func) {
try {
return func();
} catch (e) {
return false;
}
};
var isCallableWithoutNew = not(throwsError);
var arePropertyDescriptorsSupported = function () {
// if Object.defineProperty exists but throws, it's IE 8
return !throwsError(function () { Object.defineProperty({}, 'x', {}); });
};
var supportsDescriptors = !!Object.defineProperty && arePropertyDescriptorsSupported();
var _forEach = Function.call.bind(Array.prototype.forEach);
var _map = Function.call.bind(Array.prototype.map);
var _reduce = Function.call.bind(Array.prototype.reduce);
var _filter = Function.call.bind(Array.prototype.filter);
var defineProperty = function (object, name, value, force) {
if (!force && name in object) { return; }
if (supportsDescriptors) {
Object.defineProperty(object, name, {
configurable: true,
enumerable: false,
writable: true,
value: value
});
} else {
object[name] = value;
}
};
// Define configurable, writable and non-enumerable props
// if they don’t exist.
var defineProperties = function (object, map) {
_forEach(Object.keys(map), function (name) {
var method = map[name];
defineProperty(object, name, method, false);
});
};
// Simple shim for Object.create on ES3 browsers
// (unlike real shim, no attempt to support `prototype === null`)
var create = Object.create || function (prototype, properties) {
function Prototype() {}
Prototype.prototype = prototype;
var object = new Prototype();
if (typeof properties !== 'undefined') {
defineProperties(object, properties);
}
return object;
};
var supportsSubclassing = function (C, f) {
if (!Object.setPrototypeOf) { return false; /* skip test on IE < 11 */ }
return valueOrFalseIfThrows(function () {
var Sub = function Subclass(arg) {
var o = new C(arg);
Object.setPrototypeOf(o, Subclass.prototype);
return o;
};
Sub.prototype = create(C.prototype, {
constructor: { value: C }
});
return f(Sub);
});
};
var startsWithRejectsRegex = function () {
return String.prototype.startsWith && throwsError(function () {
/* throws if spec-compliant */
'/a/'.startsWith(/a/);
});
};
var startsWithHandlesInfinity = (function () {
return String.prototype.startsWith && 'abc'.startsWith('a', Infinity) === false;
}());
/*jshint evil: true */
var getGlobal = new Function('return this;');
/*jshint evil: false */
var globals = getGlobal();
var globalIsFinite = globals.isFinite;
var hasStrictMode = (function () { return this === null; }.call(null));
var startsWithIsCompliant = startsWithRejectsRegex() && startsWithHandlesInfinity;
var _indexOf = Function.call.bind(String.prototype.indexOf);
var _toString = Function.call.bind(Object.prototype.toString);
var _hasOwnProperty = Function.call.bind(Object.prototype.hasOwnProperty);
var ArrayIterator; // make our implementation private
var noop = function () {};
var Symbol = globals.Symbol || {};
var symbolSpecies = Symbol.species || '@@species';
var Type = {
object: function (x) { return x !== null && typeof x === 'object'; },
string: function (x) { return _toString(x) === '[object String]'; },
regex: function (x) { return _toString(x) === '[object RegExp]'; },
symbol: function (x) {
return typeof globals.Symbol === 'function' && typeof x === 'symbol';
}
};
var numberIsNaN = Number.isNaN || function isNaN(value) {
// NaN !== NaN, but they are identical.
// NaNs are the only non-reflexive value, i.e., if x !== x,
// then x is NaN.
// isNaN is broken: it converts its argument to number, so
// isNaN('foo') => true
return value !== value;
};
var numberIsFinite = Number.isFinite || function isFinite(value) {
return typeof value === 'number' && globalIsFinite(value);
};
var Value = {
getter: function (object, name, getter) {
if (!supportsDescriptors) {
throw new TypeError('getters require true ES5 support');
}
Object.defineProperty(object, name, {
configurable: true,
enumerable: false,
get: getter
});
},
proxy: function (originalObject, key, targetObject) {
if (!supportsDescriptors) {
throw new TypeError('getters require true ES5 support');
}
var originalDescriptor = Object.getOwnPropertyDescriptor(originalObject, key);
Object.defineProperty(targetObject, key, {
configurable: originalDescriptor.configurable,
enumerable: originalDescriptor.enumerable,
get: function getKey() { return originalObject[key]; },
set: function setKey(value) { originalObject[key] = value; }
});
},
redefine: function (object, property, newValue) {
if (supportsDescriptors) {
var descriptor = Object.getOwnPropertyDescriptor(object, property);
descriptor.value = newValue;
Object.defineProperty(object, property, descriptor);
} else {
object[property] = newValue;
}
},
preserveToString: function (target, source) {
defineProperty(target, 'toString', source.toString.bind(source), true);
}
};
var overrideNative = function overrideNative(object, property, replacement) {
var original = object[property];
defineProperty(object, property, replacement, true);
Value.preserveToString(object[property], original);
};
// This is a private name in the es6 spec, equal to '[Symbol.iterator]'
// we're going to use an arbitrary _-prefixed name to make our shims
// work properly with each other, even though we don't have full Iterator
// support. That is, `Array.from(map.keys())` will work, but we don't
// pretend to export a "real" Iterator interface.
var $iterator$ = Type.symbol(Symbol.iterator) ? Symbol.iterator : '_es6-shim iterator_';
// Firefox ships a partial implementation using the name @@iterator.
// https://bugzilla.mozilla.org/show_bug.cgi?id=907077#c14
// So use that name if we detect it.
if (globals.Set && typeof new globals.Set()['@@iterator'] === 'function') {
$iterator$ = '@@iterator';
}
var addIterator = function (prototype, impl) {
var implementation = impl || function iterator() { return this; };
var o = {};
o[$iterator$] = implementation;
defineProperties(prototype, o);
if (!prototype[$iterator$] && Type.symbol($iterator$)) {
// implementations are buggy when $iterator$ is a Symbol
prototype[$iterator$] = implementation;
}
};
// taken directly from https://github.com/ljharb/is-arguments/blob/master/index.js
// can be replaced with require('is-arguments') if we ever use a build process instead
var isArguments = function isArguments(value) {
var str = _toString(value);
var result = str === '[object Arguments]';
if (!result) {
result = str !== '[object Array]' &&
value !== null &&
typeof value === 'object' &&
typeof value.length === 'number' &&
value.length >= 0 &&
_toString(value.callee) === '[object Function]';
}
return result;
};
var safeApply = Function.call.bind(Function.apply);
var ES = {
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-call-f-v-args
Call: function Call(F, V) {
var args = arguments.length > 2 ? arguments[2] : [];
if (!ES.IsCallable(F)) {
throw new TypeError(F + ' is not a function');
}
return safeApply(F, V, args);
},
RequireObjectCoercible: function (x, optMessage) {
/* jshint eqnull:true */
if (x == null) {
throw new TypeError(optMessage || 'Cannot call method on ' + x);
}
},
TypeIsObject: function (x) {
/* jshint eqnull:true */
// this is expensive when it returns false; use this function
// when you expect it to return true in the common case.
return x != null && Object(x) === x;
},
ToObject: function (o, optMessage) {
ES.RequireObjectCoercible(o, optMessage);
return Object(o);
},
IsCallable: function (x) {
// some versions of IE say that typeof /abc/ === 'function'
return typeof x === 'function' && _toString(x) === '[object Function]';
},
ToInt32: function (x) {
return ES.ToNumber(x) >> 0;
},
ToUint32: function (x) {
return ES.ToNumber(x) >>> 0;
},
ToNumber: function (value) {
if (_toString(value) === '[object Symbol]') {
throw new TypeError('Cannot convert a Symbol value to a number');
}
return +value;
},
ToInteger: function (value) {
var number = ES.ToNumber(value);
if (numberIsNaN(number)) { return 0; }
if (number === 0 || !numberIsFinite(number)) { return number; }
return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
},
ToLength: function (value) {
var len = ES.ToInteger(value);
if (len <= 0) { return 0; } // includes converting -0 to +0
if (len > Number.MAX_SAFE_INTEGER) { return Number.MAX_SAFE_INTEGER; }
return len;
},
SameValue: function (a, b) {
if (a === b) {
// 0 === -0, but they are not identical.
if (a === 0) { return 1 / a === 1 / b; }
return true;
}
return numberIsNaN(a) && numberIsNaN(b);
},
SameValueZero: function (a, b) {
// same as SameValue except for SameValueZero(+0, -0) == true
return (a === b) || (numberIsNaN(a) && numberIsNaN(b));
},
IsIterable: function (o) {
return ES.TypeIsObject(o) && (typeof o[$iterator$] !== 'undefined' || isArguments(o));
},
GetIterator: function (o) {
if (isArguments(o)) {
// special case support for `arguments`
return new ArrayIterator(o, 'value');
}
var itFn = o[$iterator$];
if (!ES.IsCallable(itFn)) {
throw new TypeError('value is not an iterable');
}
var it = itFn.call(o);
if (!ES.TypeIsObject(it)) {
throw new TypeError('bad iterator');
}
return it;
},
IteratorNext: function (it) {
var result = arguments.length > 1 ? it.next(arguments[1]) : it.next();
if (!ES.TypeIsObject(result)) {
throw new TypeError('bad iterator');
}
return result;
},
Construct: function (C, args) {
// CreateFromConstructor
var obj;
if (ES.IsCallable(C[symbolSpecies])) {
obj = C[symbolSpecies]();
} else {
// OrdinaryCreateFromConstructor
obj = create(C.prototype || null);
}
// Mark that we've used the es6 construct path
// (see emulateES6construct)
defineProperties(obj, { _es6construct: true });
// Call the constructor.
var result = ES.Call(C, obj, args);
return ES.TypeIsObject(result) ? result : obj;
},
CreateHTML: function (string, tag, attribute, value) {
var S = String(string);
var p1 = '<' + tag;
if (attribute !== '') {
var V = String(value);
var escapedV = V.replace(/"/g, '"');
p1 += ' ' + attribute + '="' + escapedV + '"';
}
var p2 = p1 + '>';
var p3 = p2 + S;
return p3 + '</' + tag + '>';
}
};
var emulateES6construct = function (o) {
if (!ES.TypeIsObject(o)) { throw new TypeError('bad object'); }
var object = o;
// es5 approximation to es6 subclass semantics: in es6, 'new Foo'
// would invoke Foo.@@species to allocation/initialize the new object.
// In es5 we just get the plain object. So if we detect an
// uninitialized object, invoke o.constructor.@@species
if (!object._es6construct) {
if (object.constructor && ES.IsCallable(object.constructor[symbolSpecies])) {
object = object.constructor[symbolSpecies](object);
}
defineProperties(object, { _es6construct: true });
}
return object;
};
// Firefox 31 reports this function's length as 0
// https://bugzilla.mozilla.org/show_bug.cgi?id=1062484
if (String.fromCodePoint && String.fromCodePoint.length !== 1) {
var originalFromCodePoint = Function.apply.bind(String.fromCodePoint);
overrideNative(String, 'fromCodePoint', function fromCodePoint(codePoints) { return originalFromCodePoint(this, arguments); });
}
var StringShims = {
fromCodePoint: function fromCodePoint(codePoints) {
var result = [];
var next;
for (var i = 0, length = arguments.length; i < length; i++) {
next = Number(arguments[i]);
if (!ES.SameValue(next, ES.ToInteger(next)) || next < 0 || next > 0x10FFFF) {
throw new RangeError('Invalid code point ' + next);
}
if (next < 0x10000) {
result.push(String.fromCharCode(next));
} else {
next -= 0x10000;
result.push(String.fromCharCode((next >> 10) + 0xD800));
result.push(String.fromCharCode((next % 0x400) + 0xDC00));
}
}
return result.join('');
},
raw: function raw(callSite) {
var cooked = ES.ToObject(callSite, 'bad callSite');
var rawString = ES.ToObject(cooked.raw, 'bad raw value');
var len = rawString.length;
var literalsegments = ES.ToLength(len);
if (literalsegments <= 0) {
return '';
}
var stringElements = [];
var nextIndex = 0;
var nextKey, next, nextSeg, nextSub;
while (nextIndex < literalsegments) {
nextKey = String(nextIndex);
nextSeg = String(rawString[nextKey]);
stringElements.push(nextSeg);
if (nextIndex + 1 >= literalsegments) {
break;
}
next = nextIndex + 1 < arguments.length ? arguments[nextIndex + 1] : '';
nextSub = String(next);
stringElements.push(nextSub);
nextIndex++;
}
return stringElements.join('');
}
};
defineProperties(String, StringShims);
if (String.raw({ raw: { 0: 'x', 1: 'y', length: 2 } }) !== 'xy') {
// IE 11 TP has a broken String.raw implementation
overrideNative(String, 'raw', StringShims.raw);
}
// Fast repeat, uses the `Exponentiation by squaring` algorithm.
// Perf: http://jsperf.com/string-repeat2/2
var stringRepeat = function repeat(s, times) {
if (times < 1) { return ''; }
if (times % 2) { return repeat(s, times - 1) + s; }
var half = repeat(s, times / 2);
return half + half;
};
var stringMaxLength = Infinity;
var StringPrototypeShims = {
repeat: function repeat(times) {
ES.RequireObjectCoercible(this);
var thisStr = String(this);
var numTimes = ES.ToInteger(times);
if (numTimes < 0 || numTimes >= stringMaxLength) {
throw new RangeError('repeat count must be less than infinity and not overflow maximum string size');
}
return stringRepeat(thisStr, numTimes);
},
startsWith: function startsWith(searchString) {
ES.RequireObjectCoercible(this);
var thisStr = String(this);
if (Type.regex(searchString)) {
throw new TypeError('Cannot call method "startsWith" with a regex');
}
var searchStr = String(searchString);
var startArg = arguments.length > 1 ? arguments[1] : void 0;
var start = Math.max(ES.ToInteger(startArg), 0);
return thisStr.slice(start, start + searchStr.length) === searchStr;
},
endsWith: function endsWith(searchString) {
ES.RequireObjectCoercible(this);
var thisStr = String(this);
if (Type.regex(searchString)) {
throw new TypeError('Cannot call method "endsWith" with a regex');
}
var searchStr = String(searchString);
var thisLen = thisStr.length;
var posArg = arguments.length > 1 ? arguments[1] : void 0;
var pos = typeof posArg === 'undefined' ? thisLen : ES.ToInteger(posArg);
var end = Math.min(Math.max(pos, 0), thisLen);
return thisStr.slice(end - searchStr.length, end) === searchStr;
},
includes: function includes(searchString) {
var position = arguments.length > 1 ? arguments[1] : void 0;
// Somehow this trick makes method 100% compat with the spec.
return _indexOf(this, searchString, position) !== -1;
},
codePointAt: function codePointAt(pos) {
ES.RequireObjectCoercible(this);
var thisStr = String(this);
var position = ES.ToInteger(pos);
var length = thisStr.length;
if (position >= 0 && position < length) {
var first = thisStr.charCodeAt(position);
var isEnd = (position + 1 === length);
if (first < 0xD800 || first > 0xDBFF || isEnd) { return first; }
var second = thisStr.charCodeAt(position + 1);
if (second < 0xDC00 || second > 0xDFFF) { return first; }
return ((first - 0xD800) * 1024) + (second - 0xDC00) + 0x10000;
}
}
};
defineProperties(String.prototype, StringPrototypeShims);
if ('a'.includes('a', Infinity) !== false) {
overrideNative(String.prototype, 'includes', StringPrototypeShims.includes);
}
var hasStringTrimBug = '\u0085'.trim().length !== 1;
if (hasStringTrimBug) {
delete String.prototype.trim;
// whitespace from: http://es5.github.io/#x15.5.4.20
// implementation from https://github.com/es-shims/es5-shim/blob/v3.4.0/es5-shim.js#L1304-L1324
var ws = [
'\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003',
'\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028',
'\u2029\uFEFF'
].join('');
var trimRegexp = new RegExp('(^[' + ws + ']+)|([' + ws + ']+$)', 'g');
defineProperties(String.prototype, {
trim: function trim() {
if (typeof this === 'undefined' || this === null) {
throw new TypeError("can't convert " + this + ' to object');
}
return String(this).replace(trimRegexp, '');
}
});
}
// see https://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype-@@iterator
var StringIterator = function (s) {
ES.RequireObjectCoercible(s);
this._s = String(s);
this._i = 0;
};
StringIterator.prototype.next = function () {
var s = this._s, i = this._i;
if (typeof s === 'undefined' || i >= s.length) {
this._s = void 0;
return { value: void 0, done: true };
}
var first = s.charCodeAt(i), second, len;
if (first < 0xD800 || first > 0xDBFF || (i + 1) === s.length) {
len = 1;
} else {
second = s.charCodeAt(i + 1);
len = (second < 0xDC00 || second > 0xDFFF) ? 1 : 2;
}
this._i = i + len;
return { value: s.substr(i, len), done: false };
};
addIterator(StringIterator.prototype);
addIterator(String.prototype, function () {
return new StringIterator(this);
});
if (!startsWithIsCompliant) {
// Firefox (< 37?) and IE 11 TP have a noncompliant startsWith implementation
overrideNative(String.prototype, 'startsWith', StringPrototypeShims.startsWith);
overrideNative(String.prototype, 'endsWith', StringPrototypeShims.endsWith);
}
var ArrayShims = {
from: function from(iterable) {
var mapFn = arguments.length > 1 ? arguments[1] : void 0;
var list = ES.ToObject(iterable, 'bad iterable');
if (typeof mapFn !== 'undefined' && !ES.IsCallable(mapFn)) {
throw new TypeError('Array.from: when provided, the second argument must be a function');
}
var hasThisArg = arguments.length > 2;
var thisArg = hasThisArg ? arguments[2] : void 0;
var usingIterator = ES.IsIterable(list);
// does the spec really mean that Arrays should use ArrayIterator?
// https://bugs.ecmascript.org/show_bug.cgi?id=2416
//if (Array.isArray(list)) { usingIterator=false; }
var length;
var result, i, value;
if (usingIterator) {
i = 0;
result = ES.IsCallable(this) ? Object(new this()) : [];
var it = usingIterator ? ES.GetIterator(list) : null;
var iterationValue;
do {
iterationValue = ES.IteratorNext(it);
if (!iterationValue.done) {
value = iterationValue.value;
if (mapFn) {
result[i] = hasThisArg ? mapFn.call(thisArg, value, i) : mapFn(value, i);
} else {
result[i] = value;
}
i += 1;
}
} while (!iterationValue.done);
length = i;
} else {
length = ES.ToLength(list.length);
result = ES.IsCallable(this) ? Object(new this(length)) : new Array(length);
for (i = 0; i < length; ++i) {
value = list[i];
if (mapFn) {
result[i] = hasThisArg ? mapFn.call(thisArg, value, i) : mapFn(value, i);
} else {
result[i] = value;
}
}
}
result.length = length;
return result;
},
of: function of() {
return Array.from.call(this, arguments);
}
};
defineProperties(Array, ArrayShims);
// Given an argument x, it will return an IteratorResult object,
// with value set to x and done to false.
// Given no arguments, it will return an iterator completion object.
var iteratorResult = function (x) {
return { value: x, done: arguments.length === 0 };
};
// Our ArrayIterator is private; see
// https://github.com/paulmillr/es6-shim/issues/252
ArrayIterator = function (array, kind) {
this.i = 0;
this.array = array;
this.kind = kind;
};
defineProperties(ArrayIterator.prototype, {
next: function () {
var i = this.i, array = this.array;
if (!(this instanceof ArrayIterator)) {
throw new TypeError('Not an ArrayIterator');
}
if (typeof array !== 'undefined') {
var len = ES.ToLength(array.length);
for (; i < len; i++) {
var kind = this.kind;
var retval;
if (kind === 'key') {
retval = i;
} else if (kind === 'value') {
retval = array[i];
} else if (kind === 'entry') {
retval = [i, array[i]];
}
this.i = i + 1;
return { value: retval, done: false };
}
}
this.array = void 0;
return { value: void 0, done: true };
}
});
addIterator(ArrayIterator.prototype);
var ObjectIterator = function (object, kind) {
this.object = object;
// Don't generate keys yet.
this.array = null;
this.kind = kind;
};
function getAllKeys(object) {
var keys = [];
for (var key in object) {
keys.push(key);
}
return keys;
}
defineProperties(ObjectIterator.prototype, {
next: function () {
var key, array = this.array;
if (!(this instanceof ObjectIterator)) {
throw new TypeError('Not an ObjectIterator');
}
// Keys not generated
if (array === null) {
array = this.array = getAllKeys(this.object);
}
// Find next key in the object
while (ES.ToLength(array.length) > 0) {
key = array.shift();
// The candidate key isn't defined on object.
// Must have been deleted, or object[[Prototype]]
// has been modified.
if (!(key in this.object)) {
continue;
}
if (this.kind === 'key') {
return iteratorResult(key);
} else if (this.kind === 'value') {
return iteratorResult(this.object[key]);
} else {
return iteratorResult([key, this.object[key]]);
}
}
return iteratorResult();
}
});
addIterator(ObjectIterator.prototype);
// note: this is positioned here because it depends on ArrayIterator
var arrayOfSupportsSubclassing = (function () {
// Detects a bug in Webkit nightly r181886
var Foo = function Foo(len) { this.length = len; };
Foo.prototype = [];
var fooArr = Array.of.apply(Foo, [1, 2]);
return fooArr instanceof Foo && fooArr.length === 2;
}());
if (!arrayOfSupportsSubclassing) {
overrideNative(Array, 'of', ArrayShims.of);
}
var ArrayPrototypeShims = {
copyWithin: function copyWithin(target, start) {
var end = arguments[2]; // copyWithin.length must be 2
var o = ES.ToObject(this);
var len = ES.ToLength(o.length);
var relativeTarget = ES.ToInteger(target);
var relativeStart = ES.ToInteger(start);
var to = relativeTarget < 0 ? Math.max(len + relativeTarget, 0) : Math.min(relativeTarget, len);
var from = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
end = typeof end === 'undefined' ? len : ES.ToInteger(end);
var fin = end < 0 ? Math.max(len + end, 0) : Math.min(end, len);
var count = Math.min(fin - from, len - to);
var direction = 1;
if (from < to && to < (from + count)) {
direction = -1;
from += count - 1;
to += count - 1;
}
while (count > 0) {
if (_hasOwnProperty(o, from)) {
o[to] = o[from];
} else {
delete o[from];
}
from += direction;
to += direction;
count -= 1;
}
return o;
},
fill: function fill(value) {
var start = arguments.length > 1 ? arguments[1] : void 0;
var end = arguments.length > 2 ? arguments[2] : void 0;
var O = ES.ToObject(this);
var len = ES.ToLength(O.length);
start = ES.ToInteger(typeof start === 'undefined' ? 0 : start);
end = ES.ToInteger(typeof end === 'undefined' ? len : end);
var relativeStart = start < 0 ? Math.max(len + start, 0) : Math.min(start, len);
var relativeEnd = end < 0 ? len + end : end;
for (var i = relativeStart; i < len && i < relativeEnd; ++i) {
O[i] = value;
}
return O;
},
find: function find(predicate) {
var list = ES.ToObject(this);
var length = ES.ToLength(list.length);
if (!ES.IsCallable(predicate)) {
throw new TypeError('Array#find: predicate must be a function');
}
var thisArg = arguments.length > 1 ? arguments[1] : null;
for (var i = 0, value; i < length; i++) {
value = list[i];
if (thisArg) {
if (predicate.call(thisArg, value, i, list)) { return value; }
} else if (predicate(value, i, list)) {
return value;
}
}
},
findIndex: function findIndex(predicate) {
var list = ES.ToObject(this);
var length = ES.ToLength(list.length);
if (!ES.IsCallable(predicate)) {
throw new TypeError('Array#findIndex: predicate must be a function');
}
var thisArg = arguments.length > 1 ? arguments[1] : null;
for (var i = 0; i < length; i++) {
if (thisArg) {
if (predicate.call(thisArg, list[i], i, list)) { return i; }
} else if (predicate(list[i], i, list)) {
return i;
}
}
return -1;
},
keys: function keys() {
return new ArrayIterator(this, 'key');
},
values: function values() {
return new ArrayIterator(this, 'value');
},
entries: function entries() {
return new ArrayIterator(this, 'entry');
}
};
// Safari 7.1 defines Array#keys and Array#entries natively,
// but the resulting ArrayIterator objects don't have a "next" method.
if (Array.prototype.keys && !ES.IsCallable([1].keys().next)) {
delete Array.prototype.keys;
}
if (Array.prototype.entries && !ES.IsCallable([1].entries().next)) {
delete Array.prototype.entries;
}
// Chrome 38 defines Array#keys and Array#entries, and Array#@@iterator, but not Array#values
if (Array.prototype.keys && Array.prototype.entries && !Array.prototype.values && Array.prototype[$iterator$]) {
defineProperties(Array.prototype, {
values: Array.prototype[$iterator$]
});
if (Type.symbol(Symbol.unscopables)) {
Array.prototype[Symbol.unscopables].values = true;
}
}
// Chrome 40 defines Array#values with the incorrect name, although Array#{keys,entries} have the correct name
if (Array.prototype.values && Array.prototype.values.name !== 'values') {
var originalArrayPrototypeValues = Array.prototype.values;
overrideNative(Array.prototype, 'values', function values() { return originalArrayPrototypeValues.call(this); });
defineProperty(Array.prototype, $iterator$, Array.prototype.values, true);
}
defineProperties(Array.prototype, ArrayPrototypeShims);
addIterator(Array.prototype, function () { return this.values(); });
// Chrome defines keys/values/entries on Array, but doesn't give us
// any way to identify its iterator. So add our own shimmed field.
if (Object.getPrototypeOf) {
addIterator(Object.getPrototypeOf([].values()));
}
// note: this is positioned here because it relies on Array#entries
var arrayFromSwallowsNegativeLengths = (function () {
// Detects a Firefox bug in v32
// https://bugzilla.mozilla.org/show_bug.cgi?id=1063993
return valueOrFalseIfThrows(function () { return Array.from({ length: -1 }).length === 0; });
}());
var arrayFromHandlesIterables = (function () {
// Detects a bug in Webkit nightly r181886
var arr = Array.from([0].entries());
return arr.length === 1 && arr[0][0] === 0 && arr[0][1] === 1;
}());
if (!arrayFromSwallowsNegativeLengths || !arrayFromHandlesIterables) {
overrideNative(Array, 'from', ArrayShims.from);
}
var toLengthsCorrectly = function (method, reversed) {
var obj = { length: -1 };
obj[reversed ? ((-1 >>> 0) - 1) : 0] = true;
return valueOrFalseIfThrows(function () {
method.call(obj, function () {
// note: in nonconforming browsers, this will be called
// -1 >>> 0 times, which is 4294967295, so the throw matters.
throw new RangeError('should not reach here');
}, []);
});
};
if (!toLengthsCorrectly(Array.prototype.forEach)) {
var originalForEach = Array.prototype.forEach;
overrideNative(Array.prototype, 'forEach', function forEach(callbackFn) {
return originalForEach.apply(this.length >= 0 ? this : [], arguments);
}, true);
}
if (!toLengthsCorrectly(Array.prototype.map)) {
var originalMap = Array.prototype.map;
overrideNative(Array.prototype, 'map', function map(callbackFn) {
return originalMap.apply(this.length >= 0 ? this : [], arguments);
}, true);
}
if (!toLengthsCorrectly(Array.prototype.filter)) {
var originalFilter = Array.prototype.filter;
overrideNative(Array.prototype, 'filter', function filter(callbackFn) {
return originalFilter.apply(this.length >= 0 ? this : [], arguments);
}, true);
}
if (!toLengthsCorrectly(Array.prototype.some)) {
var originalSome = Array.prototype.some;
overrideNative(Array.prototype, 'some', function some(callbackFn) {
return originalSome.apply(this.length >= 0 ? this : [], arguments);
}, true);
}
if (!toLengthsCorrectly(Array.prototype.every)) {
var originalEvery = Array.prototype.every;
overrideNative(Array.prototype, 'every', function every(callbackFn) {
return originalEvery.apply(this.length >= 0 ? this : [], arguments);
}, true);
}
if (!toLengthsCorrectly(Array.prototype.reduce)) {
var originalReduce = Array.prototype.reduce;
overrideNative(Array.prototype, 'reduce', function reduce(callbackFn) {
return originalReduce.apply(this.length >= 0 ? this : [], arguments);
}, true);
}
if (!toLengthsCorrectly(Array.prototype.reduceRight, true)) {
var originalReduceRight = Array.prototype.reduceRight;
overrideNative(Array.prototype, 'reduceRight', function reduceRight(callbackFn) {
return originalReduceRight.apply(this.length >= 0 ? this : [], arguments);
}, true);
}
var maxSafeInteger = Math.pow(2, 53) - 1;
defineProperties(Number, {
MAX_SAFE_INTEGER: maxSafeInteger,
MIN_SAFE_INTEGER: -maxSafeInteger,
EPSILON: 2.220446049250313e-16,
parseInt: globals.parseInt,
parseFloat: globals.parseFloat,
isFinite: numberIsFinite,
isInteger: function isInteger(value) {
return numberIsFinite(value) && ES.ToInteger(value) === value;
},
isSafeInteger: function isSafeInteger(value) {
return Number.isInteger(value) && Math.abs(value) <= Number.MAX_SAFE_INTEGER;
},
isNaN: numberIsNaN
});
// Firefox 37 has a conforming Number.parseInt, but it's not === to the global parseInt (fixed in v40)
defineProperty(Number, 'parseInt', globals.parseInt, Number.parseInt !== globals.parseInt);
// Work around bugs in Array#find and Array#findIndex -- early
// implementations skipped holes in sparse arrays. (Note that the
// implementations of find/findIndex indirectly use shimmed
// methods of Number, so this test has to happen down here.)
/*jshint elision: true */
if (![, 1].find(function (item, idx) { return idx === 0; })) {
overrideNative(Array.prototype, 'find', ArrayPrototypeShims.find);
}
if ([, 1].findIndex(function (item, idx) { return idx === 0; }) !== 0) {
overrideNative(Array.prototype, 'findIndex', ArrayPrototypeShims.findIndex);
}
/*jshint elision: false */
var isEnumerableOn = Function.bind.call(Function.bind, Object.prototype.propertyIsEnumerable);
var sliceArgs = function sliceArgs() {
// per https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#32-leaking-arguments
// and https://gist.github.com/WebReflection/4327762cb87a8c634a29
var initial = Number(this);
var len = arguments.length;
var desiredArgCount = len - initial;
var args = new Array(desiredArgCount < 0 ? 0 : desiredArgCount);
for (var i = initial; i < len; ++i) {
args[i - initial] = arguments[i];
}
return args;
};
var assignTo = function assignTo(source) {