-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (146 loc) · 5.57 KB
/
index.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
import { inspect } from "node:util";
export class AssertionError extends Error {
// biome-ignore lint/complexity/noBannedTypes: Function is what stackStartFn is
constructor(message, stackStartFn) {
super();
this.name = 'AssertionError';
this.message = message;
Error.captureStackTrace(this, stackStartFn);
}
}
function _appendExtraMessage(message, extraMessageOrFn) {
let out = message;
if (typeof extraMessageOrFn === "function") {
// If function returns undefined, append value anyway
out += `: ${extraMessageOrFn()}`;
}
else if (extraMessageOrFn !== undefined) {
out += `: ${extraMessageOrFn}`;
}
return out;
}
/**
* Assert that `value` is truthy and throw `AssertionError` if it is not.
* @param value value to test
* @param extraMessageOrFn string to append to the assertion error, or a 0-arg callable that returns such a string
*/
export function A(value, extraMessageOrFn) {
if (!value) {
const message = _appendExtraMessage(`A(...): ${inspect(value)} not truthy`, extraMessageOrFn);
throw new AssertionError(message, A);
}
}
(function (A) {
/**
* Assert that `a` is the same value as `b` (using `Object.is`) and throw `AssertionError` if it is not.
* @param a first value
* @param b second value
* @param extraMessageOrFn string to append to the assertion error, or a 0-arg callable that returns such a string
*/
function is(a, b, extraMessageOrFn) {
if (!Object.is(a, b)) {
const message = _appendExtraMessage(`A.is(...): !Object.is(${inspect(a)}, ${inspect(b)})`, extraMessageOrFn);
throw new AssertionError(message, A.is);
}
}
A.is = is;
;
/**
* Assert that `a` is not the same value as `b` (using `Object.is`) and throw `AssertionError` if it is.
* @param a first value
* @param b second value
* @param extraMessageOrFn string to append to the assertion error, or a 0-arg callable that returns such a string
*/
function nis(a, b, extraMessageOrFn) {
if (Object.is(a, b)) {
const message = _appendExtraMessage(`A.nis(...): Object.is(${inspect(a)}, ${inspect(b)})`, extraMessageOrFn);
throw new AssertionError(message, A.nis);
}
}
A.nis = nis;
;
/**
* Assert that `a` is equal to `b` (`===`) and throw `AssertionError` if it is not.
* @param a first value
* @param b second value
* @param extraMessageOrFn string to append to the assertion error, or a 0-arg callable that returns such a string
*/
function eq(a, b, extraMessageOrFn) {
if (a !== b) {
const message = _appendExtraMessage(`A.eq(...): ${inspect(a)} !== ${inspect(b)}`, extraMessageOrFn);
throw new AssertionError(message, A.eq);
}
}
A.eq = eq;
;
/**
* Assert that `a` is not equal to `b` (`!==`) and throw `AssertionError` if it is.
* @param a first value
* @param b second value
* @param extraMessageOrFn string to append to the assertion error, or a 0-arg callable that returns such a string
*/
function neq(a, b, extraMessageOrFn) {
if (a === b) {
const message = _appendExtraMessage(`A.neq(...): ${inspect(a)} === ${inspect(b)}`, extraMessageOrFn);
throw new AssertionError(message, A.neq);
}
}
A.neq = neq;
;
/**
* Assert that `a` is less than `b` (`<`) and throw `AssertionError` if it is not.
* @param a first value
* @param b second value
* @param extraMessageOrFn string to append to the assertion error, or a 0-arg callable that returns such a string
*/
function lt(a, b, extraMessageOrFn) {
if (!(a < b)) {
const message = _appendExtraMessage(`A.lt(...): !(${inspect(a)} < ${inspect(b)})`, extraMessageOrFn);
throw new AssertionError(message, A.lt);
}
}
A.lt = lt;
;
/**
* Assert that `a` is less than or equal to `b` (`<=`) and throw `AssertionError` if it is not.
* @param a first value
* @param b second value
* @param extraMessageOrFn string to append to the assertion error, or a 0-arg callable that returns such a string
*/
function lte(a, b, extraMessageOrFn) {
if (!(a <= b)) {
const message = _appendExtraMessage(`A.lte(...): !(${inspect(a)} <= ${inspect(b)})`, extraMessageOrFn);
throw new AssertionError(message, A.lte);
}
}
A.lte = lte;
;
/**
* Assert that `a` is greater than `b` (`>`) and throw `AssertionError` if it is not.
* @param a first value
* @param b second value
* @param extraMessageOrFn string to append to the assertion error, or a 0-arg callable that returns such a string
*/
function gt(a, b, extraMessageOrFn) {
if (!(a > b)) {
const message = _appendExtraMessage(`A.gt(...): !(${inspect(a)} > ${inspect(b)})`, extraMessageOrFn);
throw new AssertionError(message, A.gt);
}
}
A.gt = gt;
;
/**
* Assert that `a` is greater than or equal to `b` (`>=`) and throw `AssertionError` if it is not.
* @param a first value
* @param b second value
* @param extraMessageOrFn string to append to the assertion error, or a 0-arg callable that returns such a string
*/
function gte(a, b, extraMessageOrFn) {
if (!(a >= b)) {
const message = _appendExtraMessage(`A.gte(...): !(${inspect(a)} >= ${inspect(b)})`, extraMessageOrFn);
throw new AssertionError(message, A.gte);
}
}
A.gte = gte;
;
})(A || (A = {}));