-
Notifications
You must be signed in to change notification settings - Fork 4
/
global.js
3242 lines (2836 loc) · 114 KB
/
global.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
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
(function (global){
/* eslint-disable no-undef, global-require */
"use strict"
if (typeof define === "function" && define.amd) {
define("clean-assert", function () { return require("./index") })
} else {
global.assert = require("./index")
}
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"./index":2}],2:[function(require,module,exports){
"use strict"
/**
* Core TDD-style assertions. These are done by a composition of DSLs, since
* there is *so* much repetition.
*/
var match = require("./lib/match")
var comparison = require("./lib/comparison")
var inspect = require("./lib/inspect")
exports.inspect = inspect
exports.matchLoose = match.loose
exports.matchStrict = match.strict
var hasOwn = Object.prototype.hasOwnProperty
var AssertionError
try {
AssertionError = new Function( // eslint-disable-line no-new-func
"class AssertionError extends Error{" +
"constructor(m,a,e){super(m);this.actual=a;this.expected=e}" +
"get name(){return'AssertionError'}" +
"}" +
// check native subclassing support
"if('a'!=new AssertionError('a',1,2).message)throw'';" +
"return AssertionError"
)()
} catch (e) {
// Take advantage of V8's always-exposed stack trace API.
AssertionError = typeof Error.captureStackTrace === "function"
? function AssertionError(message, actual, expected) {
this.message = message || ""
this.expected = expected
this.actual = actual
Error.captureStackTrace(this, AssertionError)
}
: function AssertionError(message, actual, expected) {
var e = new Error(message)
e.name = "AssertionError"
this.message = e.message
this.expected = expected
this.actual = actual
this.stack = e.stack
// PhantomJS, IE, and possibly Edge don't set the stack trace until
// the error is thrown. Note that this prefers an existing stack
// first, since most everyone else already contains this.
if (this.stack == null) {
try {
throw e
} catch (e) {
this.stack = e.stack
}
}
}
AssertionError.prototype = Object.create(Error.prototype)
Object.defineProperty(AssertionError.prototype, "constructor", {
configurable: true,
writable: true,
enumerable: false,
value: AssertionError,
})
Object.defineProperty(AssertionError.prototype, "name", {
configurable: true,
writable: true,
enumerable: false,
value: "AssertionError",
})
}
exports.AssertionError = AssertionError
exports.escape = function (string) {
if (typeof string !== "string") {
throw new TypeError("`string` must be a string")
}
return string.replace(/\{.+?\}/g, function (m) { return "\\" + m })
}
// This formats the assertion error messages.
exports.format = format
function format(message, opts, prettify) {
if (prettify == null) prettify = inspect
if (typeof message !== "string") {
throw new TypeError("`message` must be a string")
}
if (typeof opts !== "object" || opts === null) {
throw new TypeError("`opts` must be an object")
}
checkCallable(prettify, "prettify")
return message.replace(/\\?\{.+?\}/g, function (m) {
if (m[0] === "\\") return m.slice(1)
var prop = m.slice(1, -1)
if (!hasOwn.call(opts, prop)) return m
return prettify(opts[prop], {depth: 5})
})
}
// The basic assert, like `assert.ok`, but gives you an optional message.
exports.assert = assert
function assert(test, message, actual, expected) {
if (!test) {
if (arguments.length > 2) {
message = format(message, {actual: actual, expected: expected})
}
throw new AssertionError(message, actual, expected)
}
}
exports.fail = fail
function fail(message, actual, expected) {
if (arguments.length <= 1) throw new AssertionError(message)
throw new AssertionError(
format(message, {actual: actual, expected: expected}),
actual, expected
)
}
exports.failFormat = failFormat
function failFormat(message, opts, prettify) {
throw new AssertionError(
format(message, opts, prettify),
opts.actual, opts.expected
)
}
function checkNumber(value, name) {
if (typeof value !== "number") {
throw new TypeError("`" + name + "` must be a number")
}
}
function checkCallable(value, name) {
if (typeof value !== "function") {
throw new TypeError("`" + name + "` must be a function")
}
}
function isCollection(value) {
return value != null && (
typeof value[comparison.symbolIterator] === "function" ||
typeof value.length === "number"
)
}
function checkCollection(value, name) {
if (!isCollection(value)) {
throw new TypeError("`" + name + "` must be an iterable or array-like")
}
}
function isReferenceType(object) {
return object != null && (
typeof object === "function" ||
typeof object === "object"
)
}
function checkReferenceType(value, name) {
if (!isReferenceType(value)) {
throw new TypeError("`" + name + "` must be an iterable or array-like")
}
}
/* eslint-disable max-len */
var hasMessages = [
"Expected {object} to have own key {key} equal to {expected}, but found {actual}",
"Expected {object} to have own key {expected}",
"Expected {object} to not have own key {key} equal to {actual}",
"Expected {object} to not have own key {expected}",
"Expected {object} to have key {key} equal to {expected}, but found {actual}",
"Expected {object} to have key {expected}",
"Expected {object} to not have key {key} equal to {actual}",
"Expected {object} to not have key {expected}",
]
/* eslint-enable max-len */
function makeHas(test, get, is, index) {
return function (object, key, value) {
var invert = (index & 2) !== 0
if ((test(object, key) && is(get(object, key), value)) === invert) {
failFormat(hasMessages[index], {
actual: object[key], expected: value,
object: object, key: key,
})
}
}
}
function makeHasOverload(test, get, is, index) {
return function (object, key, value) {
var invert = (index & 2) !== 0
if (arguments.length >= 3) {
if ((test(object, key) && is(get(object, key), value)) === invert) {
failFormat(hasMessages[index], {
actual: object[key], expected: value,
object: object, key: key,
})
}
} else if (test(object, key) === invert) {
failFormat(hasMessages[index + 1], {
actual: object[key], expected: value,
object: object, key: key,
})
}
}
}
function testHasOwn(object, key) { return hasOwn.call(object, key) }
function testHasIn(object, key) { return key in object }
function testHasKey(object, key) { return object.has(key) }
function getProperty(object, key) { return object[key] }
function getMethod(object, key) { return object.get(key) }
var includesTemplates = [
"Expected {coll} to include {expected}",
"Expected {coll} to not include {expected}",
]
function makeIncludes(type, mask) {
return function (coll, expected) {
checkCollection(coll, "coll")
if (comparison.simpleIncludes(
comparison.arrayFrom(coll), expected, type
) === ((mask & 1) !== 0)) {
failFormat(
includesTemplates[mask >>> 1],
{coll: coll, expected: expected}
)
}
}
}
var includesMultiTemplates = [
"Expected {coll} to include all values in {expected}",
"Expected {coll} to exclude all values in {expected}",
"Expected {coll} to include any value in {expected}",
"Expected {coll} to exclude any value in {expected}",
]
function makeIncludesMulti(type, mask) {
return function (coll, expected) {
checkCollection(coll, "coll")
checkCollection(expected, "expected")
if (comparison.includesDeepCheck(
comparison.arrayFrom(coll),
comparison.arrayFrom(expected),
type, (mask & 1) !== 0
) === ((mask & 2) !== 0)) {
failFormat(
includesMultiTemplates[mask],
{coll: coll, expected: expected}
)
}
}
}
var hasKeysMessages = [
"Expected {object} to have all keys in {expected}, but found {actual}",
"Expected {object} to have any key in {expected}, but found {actual}",
"Expected {object} to lack all keys in {expected}, but found {actual}",
"Expected {object} to lack any key in {expected}, but found {actual}",
]
function makeHasKeys(mask, test, get, is) {
return function (object, expected) {
checkReferenceType(object, "object")
if (!isReferenceType(expected) && typeof expected !== "string") {
throw new TypeError(
"`expected` must be an object, iterable, or array-like"
)
}
var checkKeysOnly = isCollection(expected)
// exclusive or to invert the result if `invert` is true
// boolean equality is equivalent to exclusive or
if ((
checkKeysOnly
? comparison.hasKeysTest(
object, comparison.arrayFrom(expected),
test, (mask & 1) !== 0
)
: object === expected || comparison.hasKeysCheck(
object, expected,
test, get, is, (mask & 1) !== 0
)
) === ((mask & 2) !== 0)) {
var actual
if (checkKeysOnly) {
actual = Object.keys(expected)
var count = 0
for (var i = 0; i < actual.length; i++) {
if (test(object, actual[i])) actual[count++] = actual[i]
}
actual.length = count
} else {
actual = Object.create(null)
for (var key in expected) {
if (hasOwn.call(expected, key) && test(object, key)) {
actual[key] = get(object, key)
}
}
}
failFormat(
hasKeysMessages[mask],
{object: object, actual: actual, expected: expected}
)
}
}
}
/**
* Pretty much all the assertion methods fit within a single 2x3x14 matrix:
*
* Top level: (truth, falsehood)
*
* - Is true (like `assert.ok`)
* - Not true (like `assert.notOk`)
*
* Second level: (match type)
*
* - Structural via `matchLoose` (like `assert.includes`)
* - Exact via [SameValueZero][1] (like `assert.exactlyIncludes`)
* - Deep via `matchStrict` (like `assert.deeplyIncludes`)
*
* Third level: (match location)
*
* - Equality (like `assert.equals`)
* - Has own key (like `assert.hasOwn(object, k)`)
* - Has accessible key (like `assert.hasIn(object, k)`)
* - Has map/set key (like `assert.hasKey(object, k)`)
* - Has own key set to a value (like `assert.hasOwn(object, k, v)`)
* - Has accessible key set to a value (like `assert.hasIn(object, k, v)`)
* - Has map/set key (like `assert.hasKey(object, k, v)`)
* - Includes single value (like `assert.includes`)
* - Includes all values (like `assert.includesAll`)
* - Includes any value (like `assert.includesAny`)
* - Has all own keys (like `assert.hasAllOwn(object, [...keys])`)
* - Has any own key (like `assert.hasAnyOwn(object, [...keys])`)
* - Has all own pairs (like `assert.hasAllOwn(object, {...pairs})`)
* - Has any own pair (like `assert.hasAnyOwn(object, {...pairs})`)
* - Has all accessible keys (like `assert.hasAllIn(object, [...keys])`)
* - Has any accessible key (like `assert.hasAnyIn(object, [...keys])`)
* - Has all accessible pairs (like `assert.hasAllIn(object, {...pairs})`)
* - Has any accessible pair (like `assert.hasAnyIn(object, {...pairs})`)
* - Has all map keys (like `assert.hasAllKeys(object, [...keys])`)
* - Has any map key (like `assert.hasAnyKey(object, [...keys])`)
* - Has all map pairs (like `assert.hasAllKeys(object, {...pairs})`)
* - Has any map pair (like `assert.hasAnyKey(object, {...pairs})`)
*
* Not all assertion methods have variants in each of these:
*
* - `assert` has no inverse.
* - `fail` and `failFormat` just fail unconditionally.
* - `throws` and `throwsMatching` have no inverse, since it usually makes more
* sense to just let the exception propagate instead. You get to keep the
* stack trace this way, which is all around just generally more helpful for
* debugging.
* - Most methods have inverses named after single-word or two-word antonyms:
* - "includes" becomes "excludes"
* - "has" becomes "lacks"
* - `atLeast` becomes `below`
* - `atMost` becomes `above`
* - Several methods only have one match type/location: `atLeast`/`below`,
* `atMost`/`above`, `ok`/`notOk`, `is`/`not`, `maybeIs`/`maybeNot`,
* `between`/`notBetween`, and `closeTo`/`notCloseTo`.
*
* In total, this is a little over 100 separate, dedicated assertions
*
* [1]: http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero
*/
exports.ok = function (x) {
assert(x != null, "Expected {actual} to not be null or undefined", x)
}
exports.notOk = function (x) {
assert(x == null, "Expected {actual} to be null or undefined", x)
}
exports.is = function (Type, object) {
if (object == null || !comparison.isType(Type, object)) {
if (typeof Type === "string") {
fail(
"Expected {actual} to be " +
(/^[aeiou]/.test(Type) ? "a " : "an ") + Type,
object, undefined
)
} else {
failFormat(
"Expected {object} to be an instance of {expected}",
{actual: object.constructor, expected: Type, object: object}
)
}
}
}
exports.not = function (Type, object) {
if (object != null && comparison.isType(Type, object)) {
if (typeof Type === "string") {
fail(
"Expected {actual} to not be " +
(/^[aeiou]/.test(Type) ? "a " : "an ") + Type,
object, undefined
)
} else {
failFormat(
"Expected {object} to not be an instance of {expected}",
{actual: object.constructor, expected: Type, object: object}
)
}
}
}
exports.possibly = function (Type, object) {
if (object != null && !comparison.isType(Type, object)) {
if (typeof Type === "string") {
fail(
"Expected {actual} to possibly be " +
(/^[aeiou]/.test(Type) ? "a " : "an ") + Type,
object, undefined
)
} else {
failFormat(
"Expected {object} to possibly be an instance of {expected}",
{actual: object.constructor, expected: Type, object: object}
)
}
}
}
exports.notPossibly = function (Type, object) {
if (object == null || comparison.isType(Type, object)) {
if (typeof Type === "string") {
fail(
"Expected {actual} to not possibly be " +
(/^[aeiou]/.test(Type) ? "a " : "an ") + Type,
object, undefined
)
} else {
failFormat(
"Expected {object} to not possibly be " +
"an instance of {expected}",
{actual: object.constructor, expected: Type, object: object}
)
}
}
}
exports.atLeast = function (actual, expected) {
checkNumber(actual, "actual")
checkNumber(expected, "expected")
assert(
actual >= expected,
"Expected {actual} to be at least {expected}",
actual, expected
)
}
exports.atMost = function (actual, expected) {
checkNumber(actual, "actual")
checkNumber(expected, "expected")
assert(
actual <= expected,
"Expected {actual} to be at most {expected}",
actual, expected
)
}
exports.above = function (actual, expected) {
checkNumber(actual, "actual")
checkNumber(expected, "expected")
assert(
actual > expected,
"Expected {actual} to be above {expected}",
actual, expected
)
}
exports.below = function (actual, expected) {
checkNumber(actual, "actual")
checkNumber(expected, "expected")
assert(
actual < expected,
"Expected {actual} to be below {expected}",
actual, expected
)
}
exports.between = function (actual, lower, upper) {
checkNumber(actual, "actual")
checkNumber(lower, "lower")
checkNumber(upper, "upper")
if (
// eslint-disable-next-line no-self-compare
actual !== actual || lower !== lower || upper !== upper ||
actual < lower || actual > upper
) {
failFormat(
"Expected {actual} to be between {lower} and {upper}",
{actual: actual, lower: lower, upper: upper}
)
}
}
// Note: this is high enough to cover roundoff and other highly inaccurate
// computations that frequently end up in application code (where precision
// isn't always important), but not so high it fails to address truly non-equal
// values. It was chosen through a bit of trial and error.
var defaultTolerance = 1e-8
// Note: these two always fail when dealing with NaNs.
exports.closeTo = function (actual, expected, tolerance) {
checkNumber(actual, "actual")
checkNumber(expected, "expected")
if (tolerance == null) tolerance = defaultTolerance
checkNumber(tolerance, "tolerance")
if (tolerance < 0) throw new RangeError("`tolerance` must be non-negative")
assert(
// eslint-disable-next-line no-self-compare
actual === actual && expected === expected && tolerance === tolerance &&
comparison.closeTo(actual, expected, tolerance),
"Expected {actual} to be close to {expected}",
actual, expected
)
}
exports.notCloseTo = function (actual, expected, tolerance) {
checkNumber(actual, "actual")
checkNumber(expected, "expected")
if (tolerance == null) tolerance = defaultTolerance
checkNumber(tolerance, "tolerance")
if (tolerance < 0) throw new RangeError("`tolerance` must be non-negative")
assert(
// eslint-disable-next-line no-self-compare
actual === actual && expected === expected && tolerance === tolerance &&
!comparison.closeTo(actual, expected, tolerance),
"Expected {actual} to not be close to {expected}",
actual, expected
)
}
exports.notBetween = function (actual, lower, upper) {
checkNumber(actual, "actual")
checkNumber(lower, "lower")
checkNumber(upper, "upper")
if (
// eslint-disable-next-line no-self-compare
actual !== actual || lower !== lower || upper !== upper ||
actual >= lower && actual <= upper
) {
failFormat(
"Expected {actual} to not be between {lower} and {upper}",
{actual: actual, lower: lower, upper: upper}
)
}
}
exports.equal = function (actual, expected) {
assert(match.loose(actual, expected),
"Expected {actual} to equal {expected}",
actual, expected
)
}
exports.notEqual = function (actual, expected) {
assert(!match.loose(actual, expected),
"Expected {actual} to not equal {expected}",
actual, expected
)
}
exports.hasOwn = makeHasOverload(testHasOwn, getProperty, match.loose, 0x0)
exports.lacksOwn = makeHasOverload(testHasOwn, getProperty, match.loose, 0x2)
exports.hasIn = makeHasOverload(testHasIn, getProperty, match.loose, 0x4)
exports.lacksIn = makeHasOverload(testHasIn, getProperty, match.loose, 0x6)
exports.hasKey = makeHasOverload(testHasKey, getMethod, match.loose, 0x0)
exports.lacksKey = makeHasOverload(testHasKey, getMethod, match.loose, 0x6)
exports.includes = makeIncludes(match.loose, 0x0)
exports.excludes = makeIncludes(match.loose, 0x1)
exports.includesAll = makeIncludesMulti(match.loose, 0x0)
exports.excludesAll = makeIncludesMulti(match.loose, 0x2)
exports.includesAny = makeIncludesMulti(match.loose, 0x1)
exports.excludesAny = makeIncludesMulti(match.loose, 0x3)
exports.hasAllOwn = makeHasKeys(0x0, testHasOwn, getProperty, match.loose)
exports.lacksAllOwn = makeHasKeys(0x2, testHasOwn, getProperty, match.loose)
exports.hasAnyOwn = makeHasKeys(0x1, testHasOwn, getProperty, match.loose)
exports.lacksAnyOwn = makeHasKeys(0x3, testHasOwn, getProperty, match.loose)
exports.hasAllIn = makeHasKeys(0x0, testHasIn, getProperty, match.loose)
exports.lacksAllIn = makeHasKeys(0x2, testHasIn, getProperty, match.loose)
exports.hasAnyIn = makeHasKeys(0x1, testHasIn, getProperty, match.loose)
exports.lacksAnyIn = makeHasKeys(0x3, testHasIn, getProperty, match.loose)
exports.hasAllKeys = makeHasKeys(0x0, testHasKey, getMethod, match.loose)
exports.lacksAllKeys = makeHasKeys(0x2, testHasKey, getMethod, match.loose)
exports.hasAnyKey = makeHasKeys(0x1, testHasKey, getMethod, match.loose)
exports.lacksAnyKey = makeHasKeys(0x3, testHasKey, getMethod, match.loose)
exports.throws = function (Type, callback) {
if (!isReferenceType(Type) && typeof Type !== "string") {
throw new TypeError(
"`Type` must be a string, function, or object with " +
"`Symbol.hasInstance`"
)
}
checkCallable(callback, "callback")
try {
callback() // eslint-disable-line callback-return
} catch (e) {
assert(comparison.isType(Type, e),
"Expected callback to throw an instance of {expected}, " +
"but found {actual}",
e, Type
)
return
}
throw new AssertionError("Expected callback to throw")
}
exports.throwsMatching = function (matcher, callback) {
var type
if (typeof matcher === "string") {
type = 0
} else if (typeof matcher === "function") {
type = 1
} else if (matcher instanceof RegExp) {
type = 2
} else if (
matcher != null &&
Object.getPrototypeOf(matcher) === Object.prototype
) {
type = 3
} else {
throw new TypeError(
"`matcher` must be a string, function, RegExp, or object"
)
}
checkCallable(callback, "callback")
try {
callback() // eslint-disable-line callback-return
} catch (e) {
if (type === 0) {
assert(e.message === matcher,
"Expected callback to throw an error with message " +
"{expected}, but found {actual}",
e, matcher
)
} else if (type === 1) {
assert(
matcher(e),
"Expected callback to throw an error matching " +
"{expected}, but found {actual}",
e, matcher
)
} else if (type === 2) {
assert(
matcher.test(e.message),
"Expected callback to throw an error with message " +
"matching {expected}, but found {actual}",
e, matcher
)
} else {
assert(
comparison.hasKeysCheck(
e, matcher,
testHasIn, getProperty, match.loose, false
),
"Expected callback to throw an error matching " +
"{expected}, but found {actual}",
e, matcher
)
}
return
}
fail("Expected callback to throw an error", undefined, matcher)
}
exports.exactlyEqual = function (actual, expected) {
assert(comparison.sameValueZero(actual, expected),
"Expected {actual} to exactly equal {expected}",
actual, expected
)
}
exports.exactlyNotEqual = function (actual, expected) {
assert(!comparison.sameValueZero(actual, expected),
"Expected {actual} to not exactly equal {expected}",
actual, expected
)
}
/* eslint-disable max-len */
exports.exactlyHasOwn = makeHas(testHasOwn, getProperty, comparison.sameValueZero, 0x0)
exports.exactlyLacksOwn = makeHas(testHasOwn, getProperty, comparison.sameValueZero, 0x2)
exports.exactlyHasIn = makeHas(testHasIn, getProperty, comparison.sameValueZero, 0x4)
exports.exactlyLacksIn = makeHas(testHasIn, getProperty, comparison.sameValueZero, 0x6)
exports.exactlyHasKey = makeHas(testHasKey, getMethod, comparison.sameValueZero, 0x4)
exports.exactlyLacksKey = makeHas(testHasKey, getMethod, comparison.sameValueZero, 0x6)
exports.exactlyIncludes = makeIncludes(comparison.sameValueZero, 0x0)
exports.exactlyExcludes = makeIncludes(comparison.sameValueZero, 0x1)
exports.exactlyIncludesAll = makeIncludesMulti(comparison.sameValueZero, 0x0)
exports.exactlyExcludesAll = makeIncludesMulti(comparison.sameValueZero, 0x2)
exports.exactlyIncludesAny = makeIncludesMulti(comparison.sameValueZero, 0x1)
exports.exactlyExcludesAny = makeIncludesMulti(comparison.sameValueZero, 0x3)
exports.exactlyHasAllOwn = makeHasKeys(0x0, testHasOwn, getProperty, comparison.sameValueZero)
exports.exactlyLacksAllOwn = makeHasKeys(0x2, testHasOwn, getProperty, comparison.sameValueZero)
exports.exactlyHasAnyOwn = makeHasKeys(0x1, testHasOwn, getProperty, comparison.sameValueZero)
exports.exactlyLacksAnyOwn = makeHasKeys(0x3, testHasOwn, getProperty, comparison.sameValueZero)
exports.exactlyHasAllIn = makeHasKeys(0x0, testHasIn, getProperty, comparison.sameValueZero)
exports.exactlyLacksAllIn = makeHasKeys(0x2, testHasIn, getProperty, comparison.sameValueZero)
exports.exactlyHasAnyIn = makeHasKeys(0x1, testHasIn, getProperty, comparison.sameValueZero)
exports.exactlyLacksAnyIn = makeHasKeys(0x3, testHasIn, getProperty, comparison.sameValueZero)
exports.exactlyHasAllKeys = makeHasKeys(0x0, testHasKey, getMethod, comparison.sameValueZero)
exports.exactlyLacksAllKeys = makeHasKeys(0x2, testHasKey, getMethod, comparison.sameValueZero)
exports.exactlyHasAnyKey = makeHasKeys(0x1, testHasKey, getMethod, comparison.sameValueZero)
exports.exactlyLacksAnyKey = makeHasKeys(0x3, testHasKey, getMethod, comparison.sameValueZero)
/* eslint-enable max-len */
exports.deeplyEqual = function (actual, expected) {
assert(match.strict(actual, expected),
"Expected {actual} to deeply equal {expected}",
actual, expected
)
}
exports.deeplyNotEqual = function (actual, expected) {
assert(!match.strict(actual, expected),
"Expected {actual} to deeply equal {expected}",
actual, expected
)
}
exports.deeplyHasOwn = makeHas(testHasOwn, getProperty, match.strict, 0x0)
exports.deeplyLacksOwn = makeHas(testHasOwn, getProperty, match.strict, 0x2)
exports.deeplyHasIn = makeHas(testHasIn, getProperty, match.strict, 0x4)
exports.deeplyLacksIn = makeHas(testHasIn, getProperty, match.strict, 0x6)
exports.deeplyHasKey = makeHas(testHasKey, getMethod, match.strict, 0x4)
exports.deeplyLacksKey = makeHas(testHasKey, getMethod, match.strict, 0x6)
exports.deeplyIncludes = makeIncludes(match.strict, 0x0)
exports.deeplyExcludes = makeIncludes(match.strict, 0x1)
exports.deeplyIncludesAll = makeIncludesMulti(match.strict, 0x0)
exports.deeplyExcludesAll = makeIncludesMulti(match.strict, 0x2)
exports.deeplyIncludesAny = makeIncludesMulti(match.strict, 0x1)
exports.deeplyExcludesAny = makeIncludesMulti(match.strict, 0x3)
/* eslint-disable max-len */
exports.deeplyHasAllOwn = makeHasKeys(0x0, testHasOwn, getProperty, match.strict)
exports.deeplyLacksAllOwn = makeHasKeys(0x2, testHasOwn, getProperty, match.strict)
exports.deeplyHasAnyOwn = makeHasKeys(0x1, testHasOwn, getProperty, match.strict)
exports.deeplyLacksAnyOwn = makeHasKeys(0x3, testHasOwn, getProperty, match.strict)
exports.deeplyHasAllIn = makeHasKeys(0x0, testHasIn, getProperty, match.strict)
exports.deeplyLacksAllIn = makeHasKeys(0x2, testHasIn, getProperty, match.strict)
exports.deeplyHasAnyIn = makeHasKeys(0x1, testHasIn, getProperty, match.strict)
exports.deeplyLacksAnyIn = makeHasKeys(0x3, testHasIn, getProperty, match.strict)
exports.deeplyHasAllKeys = makeHasKeys(0x0, testHasKey, getMethod, match.strict)
exports.deeplyLacksAllKeys = makeHasKeys(0x2, testHasKey, getMethod, match.strict)
exports.deeplyHasAnyKey = makeHasKeys(0x1, testHasKey, getMethod, match.strict)
exports.deeplyLacksAnyKey = makeHasKeys(0x3, testHasKey, getMethod, match.strict)
/* eslint-enable max-len */
},{"./lib/comparison":4,"./lib/inspect":3,"./lib/match":5}],3:[function(require,module,exports){
"use strict"
// See https://github.com/substack/node-browserify/issues/1674
module.exports = require("util-inspect")
},{"util-inspect":12}],4:[function(require,module,exports){
"use strict"
/**
* The various non-matching comparison methods. In particular, this includes the
* core logic of `includes`, `includesAll`, `hasAllKeys`, `hasAnyKey`, etc.
*
* Most of the complexity here is to optimize for a few things:
*
* - Arrays and non-iterators shouldn't have to allocate any sort of iterator
* for inclusion checks. Arrays are the common case here, but array-likes are
* largely compatible with the same logic.
* - I tried to limit the number of polymorphic and megamorphic type checks.
* It's not to the extreme as with the object matcher, but it's not minimal.
*/
var symbolIterator = "@@iterator"
var symbolHasInstance = "@@hasInstance"
// Note: these specifically support globally polyfilled implementations as well
// as native implementations.
/* eslint-disable no-undef */
if (typeof Symbol === "function") {
// We don't need ES6 built-ins for this, so we can just check it directly.
if (Symbol.iterator != null) {
symbolIterator = Symbol.iterator
}
// Verify it's actually supported first.
if (Symbol.hasInstance != null) {
symbolHasInstance = Symbol.hasInstance
}
}
/* eslint-enable no-undef */
exports.symbolIterator = symbolIterator
exports.symbolHasInstance = symbolHasInstance
// Warning: lots of non-trivial stuff ahead. This is largely based off of this
// guide here, with a few adaptive alterations.
// https://floating-point-gui.de/errors/comparison/
//
// Specifically, here are the main deviations:
//
// - It's not *nearly* as strict when you get close to zero. Instead, it scales
// more evenly at the cost of missing things that are almost-zero that
// shouldn't be considered zero.
//
// - It isn't so naïve about handling special cases like infinite tolerances,
// zero tolerances, and so on. Instead, it just auto-accepts if the tolerance
// is infinite, and requires exact float value matching if the tolerance is
// zero.
//
// - I made a few optimizations that weren't present in their Java snippet. For
// example, the shortcuts are taken before most everything is computed.
//
// Or, to sum this up, edit with caution.
// Smallest normal IEEE 754 double - we can't use `Number.MIN_VALUE`, since
// that's a denormalized number.
var MIN_NORMAL = Math.pow(2, -1022)
var MAX_VALUE = Number.MAX_VALUE
exports.closeTo = function (actual, expected, tolerance) {
if (tolerance === Infinity || actual === expected) return true
if (tolerance === 0) return false
var diff = Math.abs(actual - expected)
// eslint-disable-next-line no-self-compare
if (diff !== diff || diff === Infinity) return false
// Relative error is less meaningful if either `actual` or `expected` are
// zero or both are extremely close to it
if (actual !== 0 && expected !== 0 && diff >= MIN_NORMAL) {
var sum = Math.abs(actual) + Math.abs(expected)
diff /= sum === Infinity ? MAX_VALUE : sum
}
return diff <= tolerance
}
// This checks `typeof` + `instanceof` + a few special types. It also correctly
// handles `Symbol.hasInstance`, even when transpiled.
//
// Note that `value` is never `null`/`undefined`
exports.isType = function (Type, value) {
if (typeof Type === "string") {
switch (Type) {
case "array":
return Array.isArray(value)
case "iterable":
return typeof value[symbolIterator] === "function"
case "array-like":
return typeof value.length === "number"
case "reference":
return typeof value === "function" || typeof value === "object"
default:
return typeof value === Type
}
} else if (Type != null && (
typeof Type === "function" || typeof Type === "object"
)) {
if (symbolHasInstance in Type) return !!Type[symbolHasInstance](value)
if (typeof Type === "function") return value instanceof Type
}
throw new TypeError(
"`Type` must be a string, function, or object with `Symbol.hasInstance`"
)
}
exports.arrayFrom = Array.from || function (array) {
var result = []
if (typeof array[symbolIterator] === "function") {
var iter = array[symbolIterator]()
var next = iter.next()
while (!next.done) {
result.push(next.value)
next = iter.next()
}
} else {
var length = array.length
if (typeof length === "number") {
length -= length % 1
for (var i = 0; i !== length; i++) result.push(array[i])
}
}
return result
}
// For a faster `NaN`-aware array inclusion test.
var arrayIncludes = [].includes || /** @this */ function (value) {
// eslint-disable-next-line no-self-compare
if (value === value) {
if (Array.isArray(this)) return this.indexOf(value) >= 0
for (var i = 0, ilen = this.length; i < ilen; i++) {
if (this[i] === value) return true
}
} else {
for (var j = 0, jlen = this.length; j < jlen; j++) {
var current = this[j]
// eslint-disable-next-line no-self-compare
if (current !== current) return true
}
}