forked from shouldjs/should.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
should.js
4091 lines (3528 loc) · 120 KB
/
should.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
/*!
* should - test framework agnostic BDD-style assertions
* @version v11.1.2
* @author TJ Holowaychuk <[email protected]>, Denis Bardadym <[email protected]>
* @link https://github.com/shouldjs/should.js
* @license MIT
*/
(function (root) {
'use strict';
var types = {
NUMBER: 'number',
UNDEFINED: 'undefined',
STRING: 'string',
BOOLEAN: 'boolean',
OBJECT: 'object',
FUNCTION: 'function',
NULL: 'null',
ARRAY: 'array',
REGEXP: 'regexp',
DATE: 'date',
ERROR: 'error',
ARGUMENTS: 'arguments',
SYMBOL: 'symbol',
ARRAY_BUFFER: 'array-buffer',
TYPED_ARRAY: 'typed-array',
DATA_VIEW: 'data-view',
MAP: 'map',
SET: 'set',
WEAK_SET: 'weak-set',
WEAK_MAP: 'weak-map',
PROMISE: 'promise',
// node buffer
BUFFER: 'buffer',
// dom html element
HTML_ELEMENT: 'html-element',
HTML_ELEMENT_TEXT: 'html-element-text',
DOCUMENT: 'document',
WINDOW: 'window',
FILE: 'file',
FILE_LIST: 'file-list',
BLOB: 'blob',
HOST: 'host',
XHR: 'xhr',
// simd
SIMD: 'simd'
};
/*
* Simple data function to store type information
* @param {string} type Usually what is returned from typeof
* @param {string} cls Sanitized @Class via Object.prototype.toString
* @param {string} sub If type and cls the same, and need to specify somehow
* @private
* @example
*
* //for null
* new Type('null');
*
* //for Date
* new Type('object', 'date');
*
* //for Uint8Array
*
* new Type('object', 'typed-array', 'uint8');
*/
function Type(type, cls, sub) {
if (!type) {
throw new Error('Type class must be initialized at least with `type` information');
}
this.type = type;
this.cls = cls;
this.sub = sub;
}
Type.prototype = {
toString: function(sep) {
sep = sep || ';';
var str = [this.type];
if (this.cls) {
str.push(this.cls);
}
if (this.sub) {
str.push(this.sub);
}
return str.join(sep);
},
toTryTypes: function() {
var _types = [];
if (this.sub) {
_types.push(new Type(this.type, this.cls, this.sub));
}
if (this.cls) {
_types.push(new Type(this.type, this.cls));
}
_types.push(new Type(this.type));
return _types;
}
};
var toString = Object.prototype.toString;
/**
* Function to store type checks
* @private
*/
function TypeChecker() {
this.checks = [];
}
TypeChecker.prototype = {
add: function(func) {
this.checks.push(func);
return this;
},
addBeforeFirstMatch: function(obj, func) {
var match = this.getFirstMatch(obj);
if (match) {
this.checks.splice(match.index, 0, func);
} else {
this.add(func);
}
},
addTypeOf: function(type, res) {
return this.add(function(obj, tpeOf) {
if (tpeOf === type) {
return new Type(res);
}
});
},
addClass: function(cls, res, sub) {
return this.add(function(obj, tpeOf, objCls) {
if (objCls === cls) {
return new Type(types.OBJECT, res, sub);
}
});
},
getFirstMatch: function(obj) {
var typeOf = typeof obj;
var cls = toString.call(obj);
for (var i = 0, l = this.checks.length; i < l; i++) {
var res = this.checks[i].call(this, obj, typeOf, cls);
if (typeof res !== 'undefined') {
return { result: res, func: this.checks[i], index: i };
}
}
},
getType: function(obj) {
var match = this.getFirstMatch(obj);
return match && match.result;
}
};
var main = new TypeChecker();
//TODO add iterators
main
.addTypeOf(types.NUMBER, types.NUMBER)
.addTypeOf(types.UNDEFINED, types.UNDEFINED)
.addTypeOf(types.STRING, types.STRING)
.addTypeOf(types.BOOLEAN, types.BOOLEAN)
.addTypeOf(types.FUNCTION, types.FUNCTION)
.addTypeOf(types.SYMBOL, types.SYMBOL)
.add(function(obj) {
if (obj === null) {
return new Type(types.NULL);
}
})
.addClass('[object String]', types.STRING)
.addClass('[object Boolean]', types.BOOLEAN)
.addClass('[object Number]', types.NUMBER)
.addClass('[object Array]', types.ARRAY)
.addClass('[object RegExp]', types.REGEXP)
.addClass('[object Error]', types.ERROR)
.addClass('[object Date]', types.DATE)
.addClass('[object Arguments]', types.ARGUMENTS)
.addClass('[object ArrayBuffer]', types.ARRAY_BUFFER)
.addClass('[object Int8Array]', types.TYPED_ARRAY, 'int8')
.addClass('[object Uint8Array]', types.TYPED_ARRAY, 'uint8')
.addClass('[object Uint8ClampedArray]', types.TYPED_ARRAY, 'uint8clamped')
.addClass('[object Int16Array]', types.TYPED_ARRAY, 'int16')
.addClass('[object Uint16Array]', types.TYPED_ARRAY, 'uint16')
.addClass('[object Int32Array]', types.TYPED_ARRAY, 'int32')
.addClass('[object Uint32Array]', types.TYPED_ARRAY, 'uint32')
.addClass('[object Float32Array]', types.TYPED_ARRAY, 'float32')
.addClass('[object Float64Array]', types.TYPED_ARRAY, 'float64')
.addClass('[object Bool16x8]', types.SIMD, 'bool16x8')
.addClass('[object Bool32x4]', types.SIMD, 'bool32x4')
.addClass('[object Bool8x16]', types.SIMD, 'bool8x16')
.addClass('[object Float32x4]', types.SIMD, 'float32x4')
.addClass('[object Int16x8]', types.SIMD, 'int16x8')
.addClass('[object Int32x4]', types.SIMD, 'int32x4')
.addClass('[object Int8x16]', types.SIMD, 'int8x16')
.addClass('[object Uint16x8]', types.SIMD, 'uint16x8')
.addClass('[object Uint32x4]', types.SIMD, 'uint32x4')
.addClass('[object Uint8x16]', types.SIMD, 'uint8x16')
.addClass('[object DataView]', types.DATA_VIEW)
.addClass('[object Map]', types.MAP)
.addClass('[object WeakMap]', types.WEAK_MAP)
.addClass('[object Set]', types.SET)
.addClass('[object WeakSet]', types.WEAK_SET)
.addClass('[object Promise]', types.PROMISE)
.addClass('[object Blob]', types.BLOB)
.addClass('[object File]', types.FILE)
.addClass('[object FileList]', types.FILE_LIST)
.addClass('[object XMLHttpRequest]', types.XHR)
.add(function(obj) {
if ((typeof Promise === types.FUNCTION && obj instanceof Promise) ||
(typeof obj.then === types.FUNCTION)) {
return new Type(types.OBJECT, types.PROMISE);
}
})
.add(function(obj) {
if (typeof Buffer !== 'undefined' && obj instanceof Buffer) {// eslint-disable-line no-undef
return new Type(types.OBJECT, types.BUFFER);
}
})
.add(function(obj) {
if (typeof Node !== 'undefined' && obj instanceof Node) {
return new Type(types.OBJECT, types.HTML_ELEMENT, obj.nodeName);
}
})
.add(function(obj) {
// probably at the begginging should be enough these checks
if (obj.Boolean === Boolean && obj.Number === Number && obj.String === String && obj.Date === Date) {
return new Type(types.OBJECT, types.HOST);
}
})
.add(function() {
return new Type(types.OBJECT);
});
/**
* Get type information of anything
*
* @param {any} obj Anything that could require type information
* @return {Type} type info
* @private
*/
function getGlobalType(obj) {
return main.getType(obj);
}
getGlobalType.checker = main;
getGlobalType.TypeChecker = TypeChecker;
getGlobalType.Type = Type;
Object.keys(types).forEach(function(typeName) {
getGlobalType[typeName] = types[typeName];
});
function format$1(msg) {
var args = arguments;
for (var i = 1, l = args.length; i < l; i++) {
msg = msg.replace(/%s/, args[i]);
}
return msg;
}
var hasOwnProperty = Object.prototype.hasOwnProperty;
function EqualityFail(a, b, reason, path) {
this.a = a;
this.b = b;
this.reason = reason;
this.path = path;
}
function typeToString(tp) {
return tp.type + (tp.cls ? '(' + tp.cls + (tp.sub ? ' ' + tp.sub : '') + ')' : '');
}
var PLUS_0_AND_MINUS_0 = '+0 is not equal to -0';
var DIFFERENT_TYPES = 'A has type %s and B has type %s';
var EQUALITY = 'A is not equal to B';
var EQUALITY_PROTOTYPE = 'A and B have different prototypes';
var WRAPPED_VALUE = 'A wrapped value is not equal to B wrapped value';
var FUNCTION_SOURCES = 'function A is not equal to B by source code value (via .toString call)';
var MISSING_KEY = '%s has no key %s';
var SET_MAP_MISSING_KEY = 'Set/Map missing key %s';
var DEFAULT_OPTIONS = {
checkProtoEql: true,
checkSubType: true,
plusZeroAndMinusZeroEqual: true,
collectAllFails: false
};
function setBooleanDefault(property, obj, opts, defaults) {
obj[property] = typeof opts[property] !== 'boolean' ? defaults[property] : opts[property];
}
var METHOD_PREFIX = '_check_';
function EQ(opts, a, b, path) {
opts = opts || {};
setBooleanDefault('checkProtoEql', this, opts, DEFAULT_OPTIONS);
setBooleanDefault('plusZeroAndMinusZeroEqual', this, opts, DEFAULT_OPTIONS);
setBooleanDefault('checkSubType', this, opts, DEFAULT_OPTIONS);
setBooleanDefault('collectAllFails', this, opts, DEFAULT_OPTIONS);
this.a = a;
this.b = b;
this._meet = opts._meet || [];
this.fails = opts.fails || [];
this.path = path || [];
}
function ShortcutError(fail) {
this.name = 'ShortcutError';
this.message = 'fail fast';
this.fail = fail;
}
ShortcutError.prototype = Object.create(Error.prototype);
EQ.checkStrictEquality = function(a, b) {
this.collectFail(a !== b, EQUALITY);
};
EQ.add = function add(type, cls, sub, f) {
var args = Array.prototype.slice.call(arguments);
f = args.pop();
EQ.prototype[METHOD_PREFIX + args.join('_')] = f;
};
EQ.prototype = {
check: function() {
try {
this.check0();
} catch (e) {
if (e instanceof ShortcutError) {
return [e.fail];
}
throw e;
}
return this.fails;
},
check0: function() {
var a = this.a;
var b = this.b;
// equal a and b exit early
if (a === b) {
// check for +0 !== -0;
return this.collectFail(a === 0 && (1 / a !== 1 / b) && !this.plusZeroAndMinusZeroEqual, PLUS_0_AND_MINUS_0);
}
var typeA = getGlobalType(a);
var typeB = getGlobalType(b);
// if objects has different types they are not equal
if (typeA.type !== typeB.type || typeA.cls !== typeB.cls || typeA.sub !== typeB.sub) {
return this.collectFail(true, format$1(DIFFERENT_TYPES, typeToString(typeA), typeToString(typeB)));
}
// as types the same checks type specific things
var name1 = typeA.type, name2 = typeA.type;
if (typeA.cls) {
name1 += '_' + typeA.cls;
name2 += '_' + typeA.cls;
}
if (typeA.sub) {
name2 += '_' + typeA.sub;
}
var f = this[METHOD_PREFIX + name2] || this[METHOD_PREFIX + name1] || this[METHOD_PREFIX + typeA.type] || this.defaultCheck;
f.call(this, this.a, this.b);
},
collectFail: function(comparison, reason, showReason) {
if (comparison) {
var res = new EqualityFail(this.a, this.b, reason, this.path);
res.showReason = !!showReason;
this.fails.push(res);
if (!this.collectAllFails) {
throw new ShortcutError(res);
}
}
},
checkPlainObjectsEquality: function(a, b) {
// compare deep objects and arrays
// stacks contain references only
//
var meet = this._meet;
var m = this._meet.length;
while (m--) {
var st = meet[m];
if (st[0] === a && st[1] === b) {
return;
}
}
// add `a` and `b` to the stack of traversed objects
meet.push([a, b]);
// TODO maybe something else like getOwnPropertyNames
var key;
for (key in b) {
if (hasOwnProperty.call(b, key)) {
if (hasOwnProperty.call(a, key)) {
this.checkPropertyEquality(key);
} else {
this.collectFail(true, format$1(MISSING_KEY, 'A', key));
}
}
}
// ensure both objects have the same number of properties
for (key in a) {
if (hasOwnProperty.call(a, key)) {
this.collectFail(!hasOwnProperty.call(b, key), format$1(MISSING_KEY, 'B', key));
}
}
meet.pop();
if (this.checkProtoEql) {
//TODO should i check prototypes for === or use eq?
this.collectFail(Object.getPrototypeOf(a) !== Object.getPrototypeOf(b), EQUALITY_PROTOTYPE, true);
}
},
checkPropertyEquality: function(propertyName) {
var _eq = new EQ(this, this.a[propertyName], this.b[propertyName], this.path.concat([propertyName]));
_eq.check0();
},
defaultCheck: EQ.checkStrictEquality
};
EQ.add(getGlobalType.NUMBER, function(a, b) {
this.collectFail((a !== a && b === b) || (b !== b && a === a) || (a !== b && a === a && b === b), EQUALITY);
});
[getGlobalType.SYMBOL, getGlobalType.BOOLEAN, getGlobalType.STRING].forEach(function(tp) {
EQ.add(tp, EQ.checkStrictEquality);
});
EQ.add(getGlobalType.FUNCTION, function(a, b) {
// functions are compared by their source code
this.collectFail(a.toString() !== b.toString(), FUNCTION_SOURCES);
// check user properties
this.checkPlainObjectsEquality(a, b);
});
EQ.add(getGlobalType.OBJECT, getGlobalType.REGEXP, function(a, b) {
// check regexp flags
var flags = ['source', 'global', 'multiline', 'lastIndex', 'ignoreCase', 'sticky', 'unicode'];
while (flags.length) {
this.checkPropertyEquality(flags.shift());
}
// check user properties
this.checkPlainObjectsEquality(a, b);
});
EQ.add(getGlobalType.OBJECT, getGlobalType.DATE, function(a, b) {
//check by timestamp only (using .valueOf)
this.collectFail(+a !== +b, EQUALITY);
// check user properties
this.checkPlainObjectsEquality(a, b);
});
[getGlobalType.NUMBER, getGlobalType.BOOLEAN, getGlobalType.STRING].forEach(function(tp) {
EQ.add(getGlobalType.OBJECT, tp, function(a, b) {
//primitive type wrappers
this.collectFail(a.valueOf() !== b.valueOf(), WRAPPED_VALUE);
// check user properties
this.checkPlainObjectsEquality(a, b);
});
});
EQ.add(getGlobalType.OBJECT, function(a, b) {
this.checkPlainObjectsEquality(a, b);
});
[getGlobalType.ARRAY, getGlobalType.ARGUMENTS, getGlobalType.TYPED_ARRAY].forEach(function(tp) {
EQ.add(getGlobalType.OBJECT, tp, function(a, b) {
this.checkPropertyEquality('length');
this.checkPlainObjectsEquality(a, b);
});
});
EQ.add(getGlobalType.OBJECT, getGlobalType.ARRAY_BUFFER, function(a, b) {
this.checkPropertyEquality('byteLength');
this.checkPlainObjectsEquality(a, b);
});
EQ.add(getGlobalType.OBJECT, getGlobalType.ERROR, function(a, b) {
this.checkPropertyEquality('name');
this.checkPropertyEquality('message');
this.checkPlainObjectsEquality(a, b);
});
EQ.add(getGlobalType.OBJECT, getGlobalType.BUFFER, function(a) {
this.checkPropertyEquality('length');
var l = a.length;
while (l--) {
this.checkPropertyEquality(l);
}
//we do not check for user properties because
//node Buffer have some strange hidden properties
});
[getGlobalType.MAP, getGlobalType.SET, getGlobalType.WEAK_MAP, getGlobalType.WEAK_SET].forEach(function(tp) {
EQ.add(getGlobalType.OBJECT, tp, function(a, b) {
this._meet.push([a, b]);
var iteratorA = a.entries();
for (var nextA = iteratorA.next(); !nextA.done; nextA = iteratorA.next()) {
var iteratorB = b.entries();
var keyFound = false;
for (var nextB = iteratorB.next(); !nextB.done; nextB = iteratorB.next()) {
// try to check keys first
var r = eq(nextA.value[0], nextB.value[0], { collectAllFails: false, _meet: this._meet });
if (r.length === 0) {
keyFound = true;
// check values also
eq(nextA.value[1], nextB.value[1], this);
}
}
if (!keyFound) {
// no such key at all
this.collectFail(true, format$1(SET_MAP_MISSING_KEY, nextA.value[0]));
}
}
this._meet.pop();
this.checkPlainObjectsEquality(a, b);
});
});
function eq(a, b, opts) {
return new EQ(opts, a, b).check();
}
eq.EQ = EQ;
var _hasOwnProperty = Object.prototype.hasOwnProperty;
var _propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
function hasOwnProperty$1(obj, key) {
return _hasOwnProperty.call(obj, key);
}
function propertyIsEnumerable(obj, key) {
return _propertyIsEnumerable.call(obj, key);
}
function merge(a, b) {
if (a && b) {
for (var key in b) {
a[key] = b[key];
}
}
return a;
}
function isIterator(obj) {
if (!obj) {
return false;
}
if (obj.__shouldIterator__) {
return true;
}
return typeof obj.next === 'function' &&
typeof Symbol === 'function' &&
typeof Symbol.iterator === 'symbol' &&
typeof obj[Symbol.iterator] === 'function' &&
obj[Symbol.iterator]() === obj;
}
//TODO find better way
function isGeneratorFunction(f) {
return typeof f === 'function' && /^function\s*\*\s*/.test(f.toString());
}
// TODO in future add generators instead of forEach and iterator implementation
function ObjectIterator(obj) {
this._obj = obj;
}
ObjectIterator.prototype = {
__shouldIterator__: true, // special marker
next: function() {
if (this._done) {
throw new Error('Iterator already reached the end');
}
if (!this._keys) {
this._keys = Object.keys(this._obj);
this._index = 0;
}
var key = this._keys[this._index];
this._done = this._index === this._keys.length;
this._index += 1;
return {
value: this._done ? void 0: [key, this._obj[key]],
done: this._done
};
}
};
if (typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol') {
ObjectIterator.prototype[Symbol.iterator] = function() {
return this;
};
}
function TypeAdaptorStorage() {
this._typeAdaptors = [];
this._iterableTypes = {};
}
TypeAdaptorStorage.prototype = {
add: function(type, cls, sub, adaptor) {
return this.addType(new getGlobalType.Type(type, cls, sub), adaptor);
},
addType: function(type, adaptor) {
this._typeAdaptors[type.toString()] = adaptor;
},
getAdaptor: function(tp, funcName) {
var tries = tp.toTryTypes();
while (tries.length) {
var toTry = tries.shift();
var ad = this._typeAdaptors[toTry];
if (ad && ad[funcName]) {
return ad[funcName];
}
}
},
requireAdaptor: function(tp, funcName) {
var a = this.getAdaptor(tp, funcName);
if (!a) {
throw new Error('There is no type adaptor `' + funcName + '` for ' + tp.toString());
}
return a;
},
addIterableType: function(tp) {
this._iterableTypes[tp.toString()] = true;
},
isIterableType: function(tp) {
return !!this._iterableTypes[tp.toString()];
}
};
var defaultTypeAdaptorStorage = new TypeAdaptorStorage();
var objectAdaptor = {
forEach: function(obj, f, context) {
for (var prop in obj) {
if (hasOwnProperty$1(obj, prop) && propertyIsEnumerable(obj, prop)) {
if (f.call(context, obj[prop], prop, obj) === false) {
return;
}
}
}
},
has: function(obj, prop) {
return hasOwnProperty$1(obj, prop);
},
get: function(obj, prop) {
return obj[prop];
},
iterator: function(obj) {
return new ObjectIterator(obj);
}
};
// default for objects
defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.OBJECT), objectAdaptor);
defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.FUNCTION), objectAdaptor);
var mapAdaptor = {
has: function(obj, key) {
return obj.has(key);
},
get: function(obj, key) {
return obj.get(key);
},
forEach: function(obj, f, context) {
var iter = obj.entries();
forEach(iter, function(value) {
return f.call(context, value[1], value[0], obj);
});
},
size: function(obj) {
return obj.size;
},
isEmpty: function(obj) {
return obj.size === 0;
},
iterator: function(obj) {
return obj.entries();
}
};
var setAdaptor = merge({}, mapAdaptor);
setAdaptor.get = function(obj, key) {
if (obj.has(key)) {
return key;
}
};
defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.MAP), mapAdaptor);
defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.SET), setAdaptor);
defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.WEAK_SET), setAdaptor);
defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.WEAK_MAP), mapAdaptor);
defaultTypeAdaptorStorage.addType(new getGlobalType.Type(getGlobalType.STRING), {
isEmpty: function(obj) {
return obj === '';
},
size: function(obj) {
return obj.length;
}
});
defaultTypeAdaptorStorage.addIterableType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.ARRAY));
defaultTypeAdaptorStorage.addIterableType(new getGlobalType.Type(getGlobalType.OBJECT, getGlobalType.ARGUMENTS));
function forEach(obj, f, context) {
if (isGeneratorFunction(obj)) {
return forEach(obj(), f, context);
} else if (isIterator(obj)) {
var value = obj.next();
while (!value.done) {
if (f.call(context, value.value, 'value', obj) === false) {
return;
}
value = obj.next();
}
} else {
var type = getGlobalType(obj);
var func = defaultTypeAdaptorStorage.requireAdaptor(type, 'forEach');
func(obj, f, context);
}
}
function size(obj) {
var type = getGlobalType(obj);
var func = defaultTypeAdaptorStorage.getAdaptor(type, 'size');
if (func) {
return func(obj);
} else {
var len = 0;
forEach(obj, function() {
len += 1;
});
return len;
}
}
function isEmpty(obj) {
var type = getGlobalType(obj);
var func = defaultTypeAdaptorStorage.getAdaptor(type, 'isEmpty');
if (func) {
return func(obj);
} else {
var res = true;
forEach(obj, function() {
res = false;
return false;
});
return res;
}
}
// return boolean if obj has such 'key'
function has(obj, key) {
var type = getGlobalType(obj);
var func = defaultTypeAdaptorStorage.requireAdaptor(type, 'has');
return func(obj, key);
}
// return value for given key
function get(obj, key) {
var type = getGlobalType(obj);
var func = defaultTypeAdaptorStorage.requireAdaptor(type, 'get');
return func(obj, key);
}
function some(obj, f, context) {
var res = false;
forEach(obj, function(value, key) {
if (f.call(context, value, key, obj)) {
res = true;
return false;
}
}, context);
return res;
}
function isIterable(obj) {
return defaultTypeAdaptorStorage.isIterableType(getGlobalType(obj));
}
function iterator(obj) {
return defaultTypeAdaptorStorage.requireAdaptor(getGlobalType(obj), 'iterator')(obj);
}
function looksLikeANumber(n) {
return !!n.match(/\d+/);
}
function keyCompare(a, b) {
var aNum = looksLikeANumber(a);
var bNum = looksLikeANumber(b);
if (aNum && bNum) {
return 1*a - 1*b;
} else if (aNum && !bNum) {
return -1;
} else if (!aNum && bNum) {
return 1;
} else {
return a.localeCompare(b);
}
}
function genKeysFunc(f) {
return function(value) {
var k = f(value);
k.sort(keyCompare);
return k;
};
}
function Formatter(opts) {
opts = opts || {};
this.seen = [];
var keysFunc;
if (typeof opts.keysFunc === 'function') {
keysFunc = opts.keysFunc;
} else if (opts.keys === false) {
keysFunc = Object.getOwnPropertyNames;
} else {
keysFunc = Object.keys;
}
this.getKeys = genKeysFunc(keysFunc);
this.maxLineLength = typeof opts.maxLineLength === 'number' ? opts.maxLineLength : 60;
this.propSep = opts.propSep || ',';
this.isUTCdate = !!opts.isUTCdate;
}
Formatter.prototype = {
constructor: Formatter,
format: function(value) {
var tp = getGlobalType(value);
if (this.alreadySeen(value)) {
return '[Circular]';
}
var tries = tp.toTryTypes();
var f = this.defaultFormat;
while (tries.length) {
var toTry = tries.shift();
var name = Formatter.formatterFunctionName(toTry);
if (this[name]) {
f = this[name];
break;
}
}
return f.call(this, value).trim();
},
defaultFormat: function(obj) {
return String(obj);
},
alreadySeen: function(value) {
return this.seen.indexOf(value) >= 0;
}
};
Formatter.addType = function addType(tp, f) {
Formatter.prototype[Formatter.formatterFunctionName(tp)] = f;
};
Formatter.formatterFunctionName = function formatterFunctionName(tp) {
return '_format_' + tp.toString('_');
};
var EOL = '\n';
function indent$1(v, indentation) {
return v
.split(EOL)
.map(function(vv) {
return indentation + vv;
})
.join(EOL);
}
function pad(str, value, filler) {
str = String(str);
var isRight = false;
if (value < 0) {
isRight = true;
value = -value;
}
if (str.length < value) {
var padding = new Array(value - str.length + 1).join(filler);
return isRight ? str + padding : padding + str;
} else {
return str;
}
}
function pad0(str, value) {
return pad(str, value, '0');
}
var functionNameRE = /^\s*function\s*(\S*)\s*\(/;
function functionName$1(f) {
if (f.name) {
return f.name;
}
var matches = f.toString().match(functionNameRE);
if (matches === null) {
// `functionNameRE` doesn't match arrow functions.
return '';