-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.js
More file actions
813 lines (738 loc) · 28.8 KB
/
mod.js
File metadata and controls
813 lines (738 loc) · 28.8 KB
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
/**
* Assertion helpers for runtime type checks and expressive assertion errors.
* @module assert
*/
const protoToString = Object.prototype.toString;
/**
* Custom error class for assertion failures.
* Extends the native Error class with additional properties for better error handling.
*/
export class AssertionError extends Error {
/**
* @param {string} message - The error message to display.
* @param {Object} [spec] - Optional specification object with additional error properties.
* @param {Error} [spec.cause] - The cause of this error.
* @param {string} [spec.name] - Custom name for the error.
* @param {string} [spec.code] - Error code identifier.
* @param {string} [spec.operator] - The assertion operator that failed.
* @param {Function} [sourceFunction] - Function to exclude from stack trace.
*/
constructor(message, spec, sourceFunction) {
spec = spec || {};
const cause = spec.cause || null;
super(message, { cause });
const name = spec.name || this.constructor.name;
const causeCode = cause && cause.code;
const code = spec.code || causeCode || this.constructor.CODE;
// Use Object.defineProperty() so we can have public, enumerable
// properties, but make them unwritable.
// Secondly, only define properties if they exist.
if (typeof name !== 'undefined') {
Object.defineProperty(this, 'name', {
enumerable: true,
value: name,
});
}
if (typeof code !== 'undefined') {
Object.defineProperty(this, 'code', {
enumerable: true,
value: code,
});
}
if (typeof spec.operator !== 'undefined') {
Object.defineProperty(this, 'operator', {
enumerable: true,
value: spec.operator,
});
}
if (Error.captureStackTrace && typeof sourceFunction === 'function') {
Error.captureStackTrace(this, sourceFunction);
}
}
}
/**
* Default error code used by AssertionError instances.
* @name AssertionError.CODE
* @type {string}
*/
Object.defineProperties(AssertionError, {
CODE: {
enumerable: true,
value: 'ASSERTION_ERROR',
},
});
/**
* Determines if the given value is a String.
* This function correctly identifies both string primitives and String objects.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a String, false otherwise.
*/
export function isString(value) {
// The typeof expression will not catch strings created
// with new String('foo'):
//
// ```js
// return typeof new String('foo') === 'string'; // false
// ```
//
// So we use `Object.prototype.toString` instead.
return protoToString.call(value) === '[object String]';
}
/**
* Determines if the given value is a String with length greater than zero.
* @see {@link isString}
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a non-empty String, false otherwise.
*/
export function isNonEmptyString(value) {
return Boolean(value && isString(value));
}
/**
* Determines if the given value is a Number or BigInt.
* This function correctly identifies both number primitives and Number objects,
* as well as BigInt values.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a Number or BigInt, false otherwise.
*/
export function isNumber(value) {
// The typeof expression will not catch numbers created with new Number(1):
//
// ```js
// return typeof new Number(1) === 'number'; // false
// ```
//
// So we use `Object.prototype.toString` instead.
const tag = protoToString.call(value);
return tag === '[object Number]' || tag === '[object BigInt]';
}
/**
* Determines if the given value is a Number but is not NaN.
* @see {@link isNumber}
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a Number and not NaN, false otherwise.
*/
export function isNumberNotNaN(value) {
return isNumber(value) && !Number.isNaN(value);
}
/**
* Determines if the given value is a Boolean.
* This function correctly identifies both boolean primitives and Boolean objects.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a Boolean, false otherwise.
*/
export function isBoolean(value) {
// The typeof expression will not catch values created with new Boolean(1):
//
// ```js
// return typeof new Boolean(1) === 'boolean'; // false
// ```
//
// So we use `Object.prototype.toString` instead.
return protoToString.call(value) === '[object Boolean]';
}
/**
* Determines if the given value is undefined.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is undefined, false otherwise.
*/
export function isUndefined(value) {
return typeof value === 'undefined';
}
/**
* Determines if the given value is a primitive value.
* Primitive values are defined as String, Number, BigInt, Boolean, Symbol, null, and undefined.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a primitive, false otherwise.
*/
export function isPrimitive(value) {
return value === null
|| isString(value)
|| isNumber(value)
|| (typeof value === 'bigint')
|| isBoolean(value)
|| (typeof value === 'symbol')
|| isUndefined(value);
}
/**
* Determines if the given value is a Function.
* This will work as expected for function declarations, function expressions, async functions,
* class static methods, class methods, and object methods.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a Function, false otherwise.
*/
export function isFunction(value) {
return typeof value === 'function';
}
/**
* Determines if the given value is an object but not null.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is an object and not null, false otherwise.
*/
export function isObjectNotNull(value) {
return typeof value === 'object' && value !== null;
}
/**
* Determines if the given value is a plain object.
* A plain object is defined as an object that either has no prototype or has a constructor named "Object".
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a plain object, false otherwise.
*/
export function isPlainObject(value) {
if (!value || typeof value !== 'object') {
return false;
}
if (!Object.getPrototypeOf(value)) {
return true;
}
return value.constructor && value.constructor.name === 'Object';
}
/**
* Determines if the given value is a native JavaScript Date instance.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a Date, false otherwise.
*/
export function isDate(value) {
// Using the protoToString tag is more reliable than using `instanceof`.
return protoToString.call(value) === '[object Date]';
}
/**
* Determines if the given value is a valid JavaScript Date instance.
* Validity is determined by checking if the date's timestamp is not NaN.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a valid Date, false otherwise.
*/
export function isValidDate(value) {
if (isDate(value)) {
return !Number.isNaN(value.getTime());
}
return false;
}
/**
* Determines if the given value is a native JavaScript RegExp instance.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a RegExp, false otherwise.
*/
export function isRegExp(value) {
// Using the protoToString tag is more reliable than using `instanceof`.
return protoToString.call(value) === '[object RegExp]';
}
/**
* Determines if the given value is a native JavaScript Map or WeakMap.
* This will work as expected, returning true when passing an instance of a class
* which extends Map or WeakMap.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a Map or WeakMap, false otherwise.
*/
export function isMap(value) {
// Using the protoToString tag is more reliable than using `instanceof`.
const tag = protoToString.call(value);
return tag === '[object Map]' || tag === '[object WeakMap]';
}
/**
* Determines if the given value is a native JavaScript Set or WeakSet.
* This will work as expected, returning true when passing an instance of a class
* which extends Set or WeakSet.
* @param {*} value - The value to check.
* @returns {boolean} True if the value is a Set or WeakSet, false otherwise.
*/
export function isSet(value) {
// Using the protoToString tag is more reliable than using `instanceof`.
const tag = protoToString.call(value);
return tag === '[object Set]' || tag === '[object WeakSet]';
}
/**
* Compares two values for equality.
* If `a === b` then returns `true`. Otherwise ensures date and NaN comparison is
* done as expected.
*
* Will return a curried version of this function if only
* a single argument is supplied.
* @param {*} a - The first value to compare.
* @param {*} b - The second value to compare.
* @returns {boolean|Function} True if the values are equal, or a curried function if only one argument is provided.
*/
export function isEqual(a, b) {
if (arguments.length < 2) {
return function curriedIsEqual(_b) {
return isEqual(a, _b);
};
}
if (a === b) {
return true;
}
if (isValidDate(a) && isValidDate(b)) {
return a <= b && a >= b;
}
// Make sure NaN === NaN.
return a !== a && b !== b;
}
/**
* Performs string matching with various strategies.
* If the matcher is a regular expression, it will call RegExp.test().
* If the matcher equals the value using isEqual(), it returns true.
* If the value is a String, it checks if the String contains the matcher using String.includes().
* If the value is a valid Date, it converts it to a string using Date.toISOString() before comparison.
*
* Will return a curried version of this function if only
* a single argument is supplied.
*
* @see {@link isEqual}
* @see {@link isValidDate}
* @param {string|RegExp} matcher - The pattern or value to match against.
* @param {*} value - The value to test.
* @returns {boolean|Function} True if the value matches, or a curried function if only one argument is provided.
*/
export function doesMatch(matcher, value) {
if (arguments.length < 2) {
return function curriedDoesMatch(_value) {
return doesMatch(matcher, _value);
};
}
if (isEqual(matcher, value)) {
return true;
}
if (isValidDate(value)) {
value = value.toISOString();
}
if (typeof matcher?.test === 'function') {
return matcher.test(value);
}
if (typeof value?.includes === 'function') {
return value.includes(matcher);
}
return false;
}
/**
* Converts any JavaScript value to a human-friendly string representation.
* @param {*} value - The value to convert.
* @returns {string} A human-readable string representation of the value.
*/
export function toFriendlyString(value) {
if (isString(value)) {
return `String(${ value })`;
}
if (typeof value === 'bigint') {
return `BigInt(${ value })`;
}
// WARNING
// Checking isNumber() will return true for BigInt instances as well as
// Numbers, so the isBigInt() check needs to come before isNumber().
if (isNumber(value)) {
return `Number(${ value })`;
}
if (isBoolean(value)) {
return `Boolean(${ value })`;
}
if (typeof value === 'symbol') {
return value.toString();
}
if (isUndefined(value)) {
return 'undefined';
}
if (isFunction(value)) {
if (value.toString().startsWith('class ')) {
return `class ${ value.name } {}`;
}
// This will get "Function" or "AsyncFunction":
const prefix = protoToString.call(value).slice(8, -1);
if (value.name) {
return `${ prefix }(${ value.name })`;
}
return `${ prefix }(function)`;
}
if (value === null) {
return 'null';
}
if (Object.getPrototypeOf(value) === null) {
return 'Object(null)';
}
if (isPlainObject(value)) {
return 'Object({})';
}
if (Array.isArray(value)) {
if (value.length === 0) {
return 'Array([])';
}
return `Array([0..${ (value.length - 1) }])`;
}
if (isValidDate(value)) {
return `Date(${ value.toISOString() })`;
}
if (isDate(value)) {
return 'Date(Invalid)';
}
if (isRegExp(value)) {
return `RegExp(${ value })`;
}
if (isMap(value) || isSet(value)) {
return `${ value.constructor.name }()`;
}
const name = value.constructor?.name || 'Object';
return `${ name }(${ value })`;
}
/**
* Creates a function which can create assertion functions that can be curried.
* If the returned function is called with only a single argument, it will
* return a curried version of the assertion function.
*
* @param {string} operator - The name of the assertion operator which will be
* passed to new AssertionError({ operator }).
* @param {Function} guard - The guard function should return a message string
* in the case of failure and null in the case of success.
* @returns {Function} A curried assertion function.
*
* @example
* const assertEqual = curryAssertion2('assertEqual', (expected, actual, messagePrefix) => {
* if (actual !== expected) {
* return `${messagePrefix}. Values are not equal.`;
* }
* return null;
* });
*
* const assertIsZero = assertEqual(0);
*
* // This will fail.
* assertIsZero(1, 'What happens when we pass in 1?');
*/
export function curryAssertion2(operator, guard) {
return function curriedAssertion2(expected, actual, messagePrefix) {
if (arguments.length < 2) {
return function curriedInnerAssert(_actual, _messagePrefix) {
const message = guard(expected, _actual, _messagePrefix);
if (message) {
throw new AssertionError(message, { operator }, curriedInnerAssert);
}
return true;
};
}
const message = guard(expected, actual, messagePrefix);
if (message) {
throw new AssertionError(message, { operator }, curriedAssertion2);
}
return true;
};
}
/**
* Asserts that the given value is truthy.
* If the value is falsy, assert() will throw an AssertionError.
*
* @param {*} actual - The value to test.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the value is falsy.
* @returns {void}
*/
export function assert(actual, messagePrefix) {
const assertionMessage = `Expected ${ toFriendlyString(actual) } to be truthy`;
const message = isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
if (!actual) {
throw new AssertionError(message, { operator: 'assert' }, assert);
}
}
/**
* Asserts that the given value is falsy.
* If the value is truthy, assertFalsy() will throw an AssertionError.
*
* @param {*} actual - The value to test.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the value is truthy.
* @returns {void}
*/
export function assertFalsy(actual, messagePrefix) {
const assertionMessage = `Expected ${ toFriendlyString(actual) } to be falsy`;
const message = isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
if (actual) {
throw new AssertionError(message, { operator: 'assertFalsy' }, assertFalsy);
}
}
/**
* Asserts equality using isEqual().
* If the actual value does not equal the expected value, an AssertionError will be thrown.
*
* @see {@link isEqual}
* @param {*} expected - The value to test against.
* @param {*} actual - The value to test.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the values are not equal.
* @returns {true|Function} True when the assertion passes, or a curried function if only one argument is provided.
*/
export const assertEqual = curryAssertion2('assertEqual', (expected, actual, messagePrefix) => {
if (!isEqual(expected, actual)) {
const assertionMessage = `Expected ${ toFriendlyString(actual) } to equal (===) ${ toFriendlyString(expected) }`;
return isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
}
return null;
});
/**
* Asserts non-equality using isEqual().
* If the actual value equals the expected value, an AssertionError will be thrown.
*
* @see {@link isEqual}
* @param {*} expected - The value to test against.
* @param {*} actual - The value to test.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the values are equal.
* @returns {true|Function} True when the assertion passes, or a curried function if only one argument is provided.
*/
export const assertNotEqual = curryAssertion2('assertNotEqual', (expected, actual, messagePrefix) => {
if (isEqual(expected, actual)) {
const assertionMessage = `Expected ${ toFriendlyString(actual) } to NOT equal (!==) ${ toFriendlyString(expected) }`;
return isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
}
return null;
});
/**
* Asserts that the actual value matches the matcher value according to doesMatch().
* If the actual does not match the matcher, an AssertionError will be thrown.
*
* @see {@link doesMatch}
* @param {string|RegExp} matcher - The matcher to test against. See doesMatch() for more info.
* @param {*} actual - The value to test. See doesMatch() for more info.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the value does not match.
* @returns {true|Function} True when the assertion passes, or a curried function if only one argument is provided.
*/
export const assertMatches = curryAssertion2('assertMatches', (matcher, actual, messagePrefix) => {
if (!doesMatch(matcher, actual)) {
const assertionMessage = `Expected ${ toFriendlyString(actual) } to match ${ toFriendlyString(matcher) }`;
return isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
}
return null;
});
/**
* Asserts that the actual value does NOT match the matcher value according to doesMatch().
* If the actual value matches the matcher, an AssertionError will be thrown.
*
* @see {@link doesMatch}
* @param {string|RegExp} matcher - The matcher to test against. See doesMatch() for more info.
* @param {*} actual - The value to test. See doesMatch() for more info.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the value matches.
* @returns {true|Function} True when the assertion passes, or a curried function if only one argument is provided.
*/
export const assertNotMatches = curryAssertion2('assertNotMatches', (matcher, actual, messagePrefix) => {
if (doesMatch(matcher, actual)) {
const assertionMessage = `Expected ${ toFriendlyString(actual) } NOT to match ${ toFriendlyString(matcher) }`;
return isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
}
return null;
});
/**
* Asserts that the given value is not undefined as determined by isUndefined().
* If the value is undefined, an AssertionError will be thrown.
*
* @see {@link isUndefined}
* @param {*} value - The value to test.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the value is undefined.
* @returns {void}
*/
export function assertDefined(value, messagePrefix) {
if (isUndefined(value)) {
const assertionMessage = `Expected ${ toFriendlyString(value) } to be defined`;
const message = isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
throw new AssertionError(message, { operator: 'assertDefined' }, assertDefined);
}
}
/**
* Asserts that the given value is undefined as determined by isUndefined().
* If the value is NOT undefined, an AssertionError will be thrown.
*
* @see {@link isUndefined}
* @param {*} value - The value to test.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the value is not undefined.
* @returns {void}
*/
export function assertUndefined(value, messagePrefix) {
if (!isUndefined(value)) {
const assertionMessage = `Expected ${ toFriendlyString(value) } to be undefined`;
const message = isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
throw new AssertionError(message, { operator: 'assertUndefined' }, assertUndefined);
}
}
/**
* Asserts that the given value is a non-empty String as determined by isNonEmptyString().
* If the value is not a String or is an empty String, an AssertionError will be thrown.
*
* @see {@link isNonEmptyString}
* @param {*} value - The value to test.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the value is not a non-empty String.
* @returns {void}
*/
export function assertNonEmptyString(value, messagePrefix) {
if (!isNonEmptyString(value)) {
const assertionMessage = `Expected ${ toFriendlyString(value) } to be a non-empty String`;
const message = isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
throw new AssertionError(message, { operator: 'assertNonEmptyString' }, assertNonEmptyString);
}
}
/**
* Asserts that the given value is a Number but not NaN as determined by isNumberNotNaN().
* If the value is not a Number or is NaN, an AssertionError will be thrown.
*
* @see {@link isNumberNotNaN}
* @param {*} value - The value to test.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the value is not a Number or is NaN.
* @returns {void}
*/
export function assertNumberNotNaN(value, messagePrefix) {
if (!isNumberNotNaN(value)) {
const assertionMessage = `Expected ${ toFriendlyString(value) } to be a Number and not NaN`;
const message = isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
throw new AssertionError(message, { operator: 'assertNumberNotNaN' }, assertNumberNotNaN);
}
}
/**
* Asserts that the given value is an Array as determined by Array.isArray().
* If the value is not an Array, an AssertionError will be thrown.
*
* @param {*} value - The value to test.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the value is not an Array.
* @returns {void}
*/
export function assertArray(value, messagePrefix) {
if (!Array.isArray(value)) {
const assertionMessage = `Expected ${ toFriendlyString(value) } to be an Array`;
const message = isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
throw new AssertionError(message, { operator: 'assertArray' }, assertArray);
}
}
/**
* Asserts that the given value is a Boolean as determined by isBoolean().
* If the value is not a Boolean, an AssertionError will be thrown.
*
* @see {@link isBoolean}
* @param {*} value - The value to test.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the value is not a Boolean.
* @returns {void}
*/
export function assertBoolean(value, messagePrefix) {
if (!isBoolean(value)) {
const assertionMessage = `Expected ${ toFriendlyString(value) } to be a Boolean`;
const message = isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
throw new AssertionError(message, { operator: 'assertBoolean' }, assertBoolean);
}
}
/**
* Asserts that the given value is a Function as determined by isFunction().
* If the value is not a Function, an AssertionError will be thrown.
*
* @see {@link isFunction}
* @param {*} value - The value to test.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the value is not a Function.
* @returns {void}
*/
export function assertFunction(value, messagePrefix) {
if (!isFunction(value)) {
const assertionMessage = `Expected ${ toFriendlyString(value) } to be a Function`;
const message = isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
throw new AssertionError(message, { operator: 'assertFunction' }, assertFunction);
}
}
/**
* Asserts that the given value is a valid Date as determined by isValidDate().
* If the value is not a valid Date, an AssertionError will be thrown.
*
* @see {@link isValidDate}
* @param {*} value - The value to test.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the value is not a valid Date.
* @returns {void}
*/
export function assertValidDate(value, messagePrefix) {
if (!isValidDate(value)) {
const assertionMessage = `Expected ${ toFriendlyString(value) } to be a valid Date`;
const message = isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
throw new AssertionError(message, { operator: 'assertValidDate' }, assertValidDate);
}
}
/**
* Asserts that the given value is a RegExp as determined by isRegExp().
* If the value is not a RegExp, an AssertionError will be thrown.
*
* @see {@link isRegExp}
* @param {*} value - The value to test.
* @param {string} [messagePrefix] - An optional error message prefix string.
* @throws {AssertionError} When the value is not a RegExp.
* @returns {void}
*/
export function assertRegExp(value, messagePrefix) {
if (!isRegExp(value)) {
const assertionMessage = `Expected ${ toFriendlyString(value) } to be a RegExp`;
const message = isNonEmptyString(messagePrefix)
? `${ messagePrefix } (${ assertionMessage })`
: assertionMessage;
throw new AssertionError(message, { operator: 'assertRegExp' }, assertRegExp);
}
}
/**
* Asserts that the subject value is greater than the control value.
* If the subject is less than or equal to the control value, an AssertionError will be thrown.
*
* @param {number} control - The value to test against.
* @param {number} subject - The value to test.
* @param {string} [messageSuffix] - An optional error message suffix string.
* @throws {AssertionError} When the subject is not greater than the control.
* @returns {true|Function} True when the assertion passes, or a curried function if only one argument is provided.
*/
export const assertGreaterThan = curryAssertion2('assertGreaterThan', (control, subject, messageSuffix) => {
if (subject <= control) {
const assertionMessage = `Expected ${ toFriendlyString(subject) } to be greater than ${ toFriendlyString(control) }`;
return isNonEmptyString(messageSuffix)
? `${ assertionMessage } (${ messageSuffix })`
: assertionMessage;
}
return null;
});
/**
* Asserts that the subject value is less than the control value.
* If the subject is greater than or equal to the control value, an AssertionError will be thrown.
*
* @param {number} control - The value to test against.
* @param {number} subject - The value to test.
* @param {string} [messageSuffix] - An optional error message suffix string.
* @throws {AssertionError} When the subject is not less than the control.
* @returns {true|Function} True when the assertion passes, or a curried function if only one argument is provided.
*/
export const assertLessThan = curryAssertion2('assertLessThan', (control, subject, messageSuffix) => {
if (subject >= control) {
const assertionMessage = `Expected ${ toFriendlyString(subject) } to be less than ${ toFriendlyString(control) }`;
return isNonEmptyString(messageSuffix)
? `${ assertionMessage } (${ messageSuffix })`
: assertionMessage;
}
return null;
});