@augment-vir/assert 30.8.4 → 31.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assertions/boolean.d.ts +20 -26
- package/dist/assertions/boolean.js +185 -41
- package/dist/assertions/boundary.d.ts +40 -256
- package/dist/assertions/boundary.js +265 -229
- package/dist/assertions/enum.d.ts +12 -13
- package/dist/assertions/enum.js +98 -20
- package/dist/assertions/equality/entry-equality.d.ts +11 -15
- package/dist/assertions/equality/entry-equality.js +210 -43
- package/dist/assertions/equality/json-equality.d.ts +11 -15
- package/dist/assertions/equality/json-equality.js +144 -43
- package/dist/assertions/equality/simple-equality.d.ts +39 -46
- package/dist/assertions/equality/simple-equality.js +316 -61
- package/dist/assertions/extendable-assertions.d.ts +0 -12
- package/dist/assertions/extendable-assertions.js +0 -12
- package/dist/assertions/http.d.ts +10 -14
- package/dist/assertions/http.js +96 -28
- package/dist/assertions/instance.d.ts +10 -18
- package/dist/assertions/instance.js +92 -26
- package/dist/assertions/keys.d.ts +59 -138
- package/dist/assertions/keys.js +279 -163
- package/dist/assertions/length.d.ts +30 -212
- package/dist/assertions/length.js +117 -175
- package/dist/assertions/nullish.d.ts +8 -20
- package/dist/assertions/nullish.js +85 -27
- package/dist/assertions/numeric.d.ts +67 -81
- package/dist/assertions/numeric.js +564 -133
- package/dist/assertions/output.d.ts +2 -3
- package/dist/assertions/output.js +1 -7
- package/dist/assertions/primitive.d.ts +33 -40
- package/dist/assertions/primitive.js +232 -66
- package/dist/assertions/promise.d.ts +20 -30
- package/dist/assertions/promise.js +244 -53
- package/dist/assertions/regexp.d.ts +12 -14
- package/dist/assertions/regexp.js +84 -21
- package/dist/assertions/runtime-type.d.ts +99 -207
- package/dist/assertions/runtime-type.js +805 -276
- package/dist/assertions/throws.d.ts +24 -25
- package/dist/assertions/throws.js +43 -5
- package/dist/assertions/uuid.d.ts +11 -16
- package/dist/assertions/uuid.js +91 -22
- package/dist/assertions/values.d.ts +81 -210
- package/dist/assertions/values.js +627 -234
- package/dist/augments/assertion-exports.d.ts +0 -1
- package/dist/augments/assertion-exports.js +1 -1
- package/dist/augments/guards/assert-wrap.d.ts +7 -4
- package/dist/augments/guards/assert-wrap.js +5 -4
- package/dist/augments/guards/check-wrap.d.ts +7 -5
- package/dist/augments/guards/check-wrap.js +5 -4
- package/dist/augments/guards/check.d.ts +5 -5
- package/dist/augments/guards/check.js +5 -4
- package/dist/augments/guards/wait-until.d.ts +8 -4
- package/dist/augments/guards/wait-until.js +7 -8
- package/dist/guard-types/guard-group.d.ts +5 -2
- package/dist/guard-types/wait-until-function.d.ts +2 -10
- package/dist/guard-types/wait-until-function.js +1 -9
- package/dist/index.d.ts +1 -0
- package/package.json +2 -2
- package/dist/guard-types/assert-wrap-function.d.ts +0 -12
- package/dist/guard-types/assert-wrap-function.js +0 -14
- package/dist/guard-types/check-function.d.ts +0 -14
- package/dist/guard-types/check-function.js +0 -22
- package/dist/guard-types/check-wrap-wrapper-function.d.ts +0 -12
- package/dist/guard-types/check-wrap-wrapper-function.js +0 -19
- package/dist/guard-types/guard-override.d.ts +0 -4
- package/dist/guard-types/guard-override.js +0 -10
package/dist/assertions/http.js
CHANGED
|
@@ -1,28 +1,65 @@
|
|
|
1
1
|
import { HttpStatus, httpStatusByCategory, stringify, } from '@augment-vir/core';
|
|
2
2
|
import { AssertionError } from '../augments/assertion.error.js';
|
|
3
|
-
import {
|
|
3
|
+
import { createWaitUntil } from '../guard-types/wait-until-function.js';
|
|
4
4
|
import { isEnumValue } from './enum.js';
|
|
5
5
|
import { isIn } from './values.js';
|
|
6
|
-
function isHttpStatus(actual, failureMessage) {
|
|
7
|
-
try {
|
|
8
|
-
isEnumValue(actual, HttpStatus);
|
|
9
|
-
}
|
|
10
|
-
catch {
|
|
11
|
-
throw new AssertionError(`${stringify(actual)} is not a valid http status.`, failureMessage);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
function isHttpStatusCategory(actual, category, failureMessage) {
|
|
15
|
-
try {
|
|
16
|
-
isEnumValue(actual, HttpStatus);
|
|
17
|
-
isIn(actual, httpStatusByCategory[category]);
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
20
|
-
throw new AssertionError(`${stringify(actual)} is not a '${category}' http status.`, failureMessage);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
6
|
const assertions = {
|
|
24
|
-
|
|
25
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Asserts that a value is an {@link HttpStatus}.
|
|
9
|
+
*
|
|
10
|
+
* Type guards the value.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import {assert} from '@augment-vir/assert';
|
|
16
|
+
*
|
|
17
|
+
* assert.isHttpStatus(400); // passes
|
|
18
|
+
* assert.isHttpStatus(500); // passes
|
|
19
|
+
* assert.isHttpStatus(99); // fails
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @throws {@link AssertionError} If the value is not an {@link HttpStatus}.
|
|
23
|
+
* @see
|
|
24
|
+
* - {@link assert.isHttpStatusCategory} : the category assertion.
|
|
25
|
+
* - {@link HttpStatus} : all included statuses.
|
|
26
|
+
* - {@link HttpStatusCategory} : all status categories.
|
|
27
|
+
*/
|
|
28
|
+
isHttpStatus(actual, failureMessage) {
|
|
29
|
+
if (!isEnumValue(actual, HttpStatus)) {
|
|
30
|
+
throw new AssertionError(`${stringify(actual)} is not a valid HTTP status.`, failureMessage);
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
/**
|
|
34
|
+
* Asserts that a value is an {@link HttpStatus} within a specific {@link HttpStatusCategory}.
|
|
35
|
+
*
|
|
36
|
+
* Type guards the value.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* import {assert} from '@augment-vir/assert';
|
|
42
|
+
*
|
|
43
|
+
* assert.isHttpStatusCategory(400, HttpStatusCategory.ClientError); // passes
|
|
44
|
+
* assert.isHttpStatusCategory(500, HttpStatusCategory.Success); // fails
|
|
45
|
+
* assert.isHttpStatusCategory(99, HttpStatusCategory.Information); // fails
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @throws {@link AssertionError} If the value is not an {@link HttpStatus} within the
|
|
49
|
+
* {@link HttpStatusCategory}.
|
|
50
|
+
* @see
|
|
51
|
+
* - {@link assert.isHttpStatus} : the status assertion.
|
|
52
|
+
* - {@link HttpStatus} : all included statuses.
|
|
53
|
+
* - {@link HttpStatusCategory} : all status categories.
|
|
54
|
+
*/
|
|
55
|
+
isHttpStatusCategory(actual, category, failureMessage) {
|
|
56
|
+
if (!isEnumValue(actual, HttpStatus)) {
|
|
57
|
+
throw new AssertionError(`${stringify(actual)} is not a valid HTTP status.`, failureMessage);
|
|
58
|
+
}
|
|
59
|
+
else if (!isIn(actual, httpStatusByCategory[category])) {
|
|
60
|
+
throw new AssertionError(`${stringify(actual)} is not a '${category}' HTTP status.`, failureMessage);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
26
63
|
};
|
|
27
64
|
export const httpGuards = {
|
|
28
65
|
assert: assertions,
|
|
@@ -47,7 +84,9 @@ export const httpGuards = {
|
|
|
47
84
|
* - {@link HttpStatus} : all included statuses.
|
|
48
85
|
* - {@link HttpStatusCategory} : all status categories.
|
|
49
86
|
*/
|
|
50
|
-
isHttpStatus
|
|
87
|
+
isHttpStatus(actual) {
|
|
88
|
+
return isEnumValue(actual, HttpStatus);
|
|
89
|
+
},
|
|
51
90
|
/**
|
|
52
91
|
* Checks that a value is an {@link HttpStatus} within a specific {@link HttpStatusCategory}.
|
|
53
92
|
*
|
|
@@ -68,7 +107,9 @@ export const httpGuards = {
|
|
|
68
107
|
* - {@link HttpStatus} : all included statuses.
|
|
69
108
|
* - {@link HttpStatusCategory} : all status categories.
|
|
70
109
|
*/
|
|
71
|
-
isHttpStatusCategory
|
|
110
|
+
isHttpStatusCategory(actual, category) {
|
|
111
|
+
return isEnumValue(actual, HttpStatus) && isIn(actual, httpStatusByCategory[category]);
|
|
112
|
+
},
|
|
72
113
|
},
|
|
73
114
|
assertWrap: {
|
|
74
115
|
/**
|
|
@@ -92,7 +133,12 @@ export const httpGuards = {
|
|
|
92
133
|
* - {@link HttpStatus} : all included statuses.
|
|
93
134
|
* - {@link HttpStatusCategory} : all status categories.
|
|
94
135
|
*/
|
|
95
|
-
isHttpStatus
|
|
136
|
+
isHttpStatus(actual, failureMessage) {
|
|
137
|
+
if (!isEnumValue(actual, HttpStatus)) {
|
|
138
|
+
throw new AssertionError(`${stringify(actual)} is not a valid HTTP status.`, failureMessage);
|
|
139
|
+
}
|
|
140
|
+
return actual;
|
|
141
|
+
},
|
|
96
142
|
/**
|
|
97
143
|
* Checks that a value is an {@link HttpStatus} within a specific {@link HttpStatusCategory}.
|
|
98
144
|
* Returns the value if the assertion passes.
|
|
@@ -114,7 +160,15 @@ export const httpGuards = {
|
|
|
114
160
|
* - {@link HttpStatus} : all included statuses.
|
|
115
161
|
* - {@link HttpStatusCategory} : all status categories.
|
|
116
162
|
*/
|
|
117
|
-
isHttpStatusCategory
|
|
163
|
+
isHttpStatusCategory(actual, category, failureMessage) {
|
|
164
|
+
if (!isEnumValue(actual, HttpStatus)) {
|
|
165
|
+
throw new AssertionError(`${stringify(actual)} is not a valid HTTP status.`, failureMessage);
|
|
166
|
+
}
|
|
167
|
+
else if (!isIn(actual, httpStatusByCategory[category])) {
|
|
168
|
+
throw new AssertionError(`${stringify(actual)} is not a '${category}' HTTP status.`, failureMessage);
|
|
169
|
+
}
|
|
170
|
+
return actual;
|
|
171
|
+
},
|
|
118
172
|
},
|
|
119
173
|
checkWrap: {
|
|
120
174
|
/**
|
|
@@ -139,7 +193,14 @@ export const httpGuards = {
|
|
|
139
193
|
* - {@link HttpStatus} : all included statuses.
|
|
140
194
|
* - {@link HttpStatusCategory} : all status categories.
|
|
141
195
|
*/
|
|
142
|
-
isHttpStatus
|
|
196
|
+
isHttpStatus(actual) {
|
|
197
|
+
if (isEnumValue(actual, HttpStatus)) {
|
|
198
|
+
return actual;
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
},
|
|
143
204
|
/**
|
|
144
205
|
* Checks that a value is an {@link HttpStatus} within a specific {@link HttpStatusCategory}.
|
|
145
206
|
* Returns the value if the check passes, otherwise `undefined`.
|
|
@@ -161,7 +222,14 @@ export const httpGuards = {
|
|
|
161
222
|
* - {@link HttpStatus} : all included statuses.
|
|
162
223
|
* - {@link HttpStatusCategory} : all status categories.
|
|
163
224
|
*/
|
|
164
|
-
isHttpStatusCategory
|
|
225
|
+
isHttpStatusCategory(actual, category) {
|
|
226
|
+
if (isEnumValue(actual, HttpStatus) && isIn(actual, httpStatusByCategory[category])) {
|
|
227
|
+
return actual;
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
},
|
|
165
233
|
},
|
|
166
234
|
waitUntil: {
|
|
167
235
|
/**
|
|
@@ -187,7 +255,7 @@ export const httpGuards = {
|
|
|
187
255
|
* - {@link HttpStatus} : all included statuses.
|
|
188
256
|
* - {@link HttpStatusCategory} : all status categories.
|
|
189
257
|
*/
|
|
190
|
-
isHttpStatus:
|
|
258
|
+
isHttpStatus: createWaitUntil(assertions.isHttpStatus),
|
|
191
259
|
/**
|
|
192
260
|
* Repeatedly calls a callback until its output is an {@link HttpStatus} within a specific
|
|
193
261
|
* {@link HttpStatusCategory}. Once the callback output passes, it is returned. If the
|
|
@@ -212,6 +280,6 @@ export const httpGuards = {
|
|
|
212
280
|
* - {@link HttpStatus} : all included statuses.
|
|
213
281
|
* - {@link HttpStatusCategory} : all status categories.
|
|
214
282
|
*/
|
|
215
|
-
isHttpStatusCategory:
|
|
283
|
+
isHttpStatusCategory: createWaitUntil(assertions.isHttpStatusCategory),
|
|
216
284
|
},
|
|
217
285
|
};
|
|
@@ -1,13 +1,6 @@
|
|
|
1
1
|
import { type MaybePromise } from '@augment-vir/core';
|
|
2
2
|
import { type Constructor } from 'type-fest';
|
|
3
3
|
import { type WaitUntilOptions } from '../guard-types/wait-until-function.js';
|
|
4
|
-
/** Wraps the JavaScript built-in "instanceof" in a type guard assertion. */
|
|
5
|
-
declare function instanceOf<const Instance>(instance: unknown,
|
|
6
|
-
/** The constructor that the "instance" input will be checked against. */
|
|
7
|
-
constructor: Constructor<Instance>,
|
|
8
|
-
/** Message to include in error message if this assertion fails. */
|
|
9
|
-
failureMessage?: string | undefined): asserts instance is Instance;
|
|
10
|
-
declare function notInstanceOf<const Actual, const Instance>(instance: Actual, constructor: Constructor<Instance>, failureMessage?: string | undefined): asserts instance is Exclude<Actual, Instance>;
|
|
11
4
|
export declare const instanceGuards: {
|
|
12
5
|
assert: {
|
|
13
6
|
/**
|
|
@@ -29,7 +22,7 @@ export declare const instanceGuards: {
|
|
|
29
22
|
* @see
|
|
30
23
|
* - {@link assert.notInstanceOf} : the opposite assertion.
|
|
31
24
|
*/
|
|
32
|
-
instanceOf:
|
|
25
|
+
instanceOf<const Instance>(this: void, instance: unknown, constructor: Constructor<Instance>, failureMessage?: string | undefined): asserts instance is Instance;
|
|
33
26
|
/**
|
|
34
27
|
* Asserts that a value is _not_ an instance of the given class constructor.
|
|
35
28
|
*
|
|
@@ -48,7 +41,7 @@ export declare const instanceGuards: {
|
|
|
48
41
|
* @see
|
|
49
42
|
* - {@link assert.instanceOf} : the opposite assertion.
|
|
50
43
|
*/
|
|
51
|
-
notInstanceOf:
|
|
44
|
+
notInstanceOf<const Actual, const Instance>(this: void, instance: Actual, constructor: Constructor<Instance>, failureMessage?: string | undefined): asserts instance is Exclude<Actual, Instance>;
|
|
52
45
|
};
|
|
53
46
|
check: {
|
|
54
47
|
/**
|
|
@@ -68,7 +61,7 @@ export declare const instanceGuards: {
|
|
|
68
61
|
* @see
|
|
69
62
|
* - {@link check.notInstanceOf} : the opposite check.
|
|
70
63
|
*/
|
|
71
|
-
instanceOf
|
|
64
|
+
instanceOf<const Instance>(this: void, instance: unknown, constructor: Constructor<Instance>): instance is Instance;
|
|
72
65
|
/**
|
|
73
66
|
* Checks that a value is _not_ an instance of the given class constructor.
|
|
74
67
|
*
|
|
@@ -86,7 +79,7 @@ export declare const instanceGuards: {
|
|
|
86
79
|
* @see
|
|
87
80
|
* - {@link check.instanceOf} : the opposite check.
|
|
88
81
|
*/
|
|
89
|
-
notInstanceOf
|
|
82
|
+
notInstanceOf<const Actual, const Instance>(this: void, instance: Actual, constructor: Constructor<Instance>): instance is Exclude<Actual, Instance>;
|
|
90
83
|
};
|
|
91
84
|
assertWrap: {
|
|
92
85
|
/**
|
|
@@ -109,7 +102,7 @@ export declare const instanceGuards: {
|
|
|
109
102
|
* @see
|
|
110
103
|
* - {@link assertWrap.notInstanceOf} : the opposite assertion.
|
|
111
104
|
*/
|
|
112
|
-
instanceOf
|
|
105
|
+
instanceOf<const Instance>(this: void, instance: unknown, constructor: Constructor<Instance>, failureMessage?: string | undefined): Instance;
|
|
113
106
|
/**
|
|
114
107
|
* Asserts that a value is _not_ an instance of the given class constructor. Returns the
|
|
115
108
|
* value if the assertion passes.
|
|
@@ -130,7 +123,7 @@ export declare const instanceGuards: {
|
|
|
130
123
|
* @see
|
|
131
124
|
* - {@link assertWrap.instanceOf} : the opposite assertion.
|
|
132
125
|
*/
|
|
133
|
-
notInstanceOf
|
|
126
|
+
notInstanceOf<const Actual, const Instance>(this: void, instance: Actual, constructor: Constructor<Instance>, failureMessage?: string | undefined): Exclude<Actual, Instance>;
|
|
134
127
|
};
|
|
135
128
|
checkWrap: {
|
|
136
129
|
/**
|
|
@@ -152,7 +145,7 @@ export declare const instanceGuards: {
|
|
|
152
145
|
* @see
|
|
153
146
|
* - {@link checkWrap.notInstanceOf} : the opposite check.
|
|
154
147
|
*/
|
|
155
|
-
instanceOf
|
|
148
|
+
instanceOf<const Instance>(this: void, instance: unknown, constructor: Constructor<Instance>): Instance | undefined;
|
|
156
149
|
/**
|
|
157
150
|
* Checks that a value is _not_ an instance of the given class constructor. Returns the
|
|
158
151
|
* value if the check passes, otherwise `undefined`.
|
|
@@ -172,7 +165,7 @@ export declare const instanceGuards: {
|
|
|
172
165
|
* @see
|
|
173
166
|
* - {@link checkWrap.instanceOf} : the opposite check.
|
|
174
167
|
*/
|
|
175
|
-
notInstanceOf
|
|
168
|
+
notInstanceOf<const Actual, const Instance>(this: void, instance: Actual, constructor: Constructor<Instance>): Exclude<Actual, Instance> | undefined;
|
|
176
169
|
};
|
|
177
170
|
waitUntil: {
|
|
178
171
|
/**
|
|
@@ -196,7 +189,7 @@ export declare const instanceGuards: {
|
|
|
196
189
|
* @see
|
|
197
190
|
* - {@link waitUntil.notInstanceOf} : the opposite assertion.
|
|
198
191
|
*/
|
|
199
|
-
instanceOf: <const Instance>(constructor: Constructor<Instance>, callback: () => MaybePromise<unknown>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Instance>;
|
|
192
|
+
instanceOf: <const Instance>(this: void, constructor: Constructor<Instance>, callback: () => MaybePromise<unknown>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Instance>;
|
|
200
193
|
/**
|
|
201
194
|
* Repeatedly calls a callback until its output is not an instance of the given class
|
|
202
195
|
* constructor. Once the callback output passes, it is returned. If the attempts time out,
|
|
@@ -218,7 +211,6 @@ export declare const instanceGuards: {
|
|
|
218
211
|
* @see
|
|
219
212
|
* - {@link waitUntil.instanceOf} : the opposite assertion.
|
|
220
213
|
*/
|
|
221
|
-
notInstanceOf: <const Actual, const Instance>(constructor: Constructor<Instance>, callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, Instance>>;
|
|
214
|
+
notInstanceOf: <const Actual, const Instance>(this: void, constructor: Constructor<Instance>, callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, Instance>>;
|
|
222
215
|
};
|
|
223
216
|
};
|
|
224
|
-
export {};
|
|
@@ -1,24 +1,58 @@
|
|
|
1
1
|
import { stringify } from '@augment-vir/core';
|
|
2
2
|
import { AssertionError } from '../augments/assertion.error.js';
|
|
3
|
-
import {
|
|
4
|
-
/** Wraps the JavaScript built-in "instanceof" in a type guard assertion. */
|
|
5
|
-
function instanceOf(instance,
|
|
6
|
-
/** The constructor that the "instance" input will be checked against. */
|
|
7
|
-
constructor,
|
|
8
|
-
/** Message to include in error message if this assertion fails. */
|
|
9
|
-
failureMessage) {
|
|
10
|
-
if (!(instance instanceof constructor)) {
|
|
11
|
-
throw new AssertionError(`'${stringify(instance)}' is not an instance of '${constructor.name}'`, failureMessage);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
function notInstanceOf(instance, constructor, failureMessage) {
|
|
15
|
-
if (instance instanceof constructor) {
|
|
16
|
-
throw new AssertionError(`'${stringify(instance)}' is an instance of '${constructor.name}'`, failureMessage);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
3
|
+
import { createWaitUntil } from '../guard-types/wait-until-function.js';
|
|
19
4
|
const assertions = {
|
|
20
|
-
|
|
21
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Asserts that a value is an instance of the given class constructor.
|
|
7
|
+
*
|
|
8
|
+
* Type guards the value.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import {assert} from '@augment-vir/assert';
|
|
14
|
+
*
|
|
15
|
+
* assert.instanceOf(/abc/, RegExp); // passes
|
|
16
|
+
* assert.instanceOf('abc', RegExp); // fails
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @throws {@link AssertionError} If the value is not an instance of the given class
|
|
20
|
+
* constructor.
|
|
21
|
+
* @see
|
|
22
|
+
* - {@link assert.notInstanceOf} : the opposite assertion.
|
|
23
|
+
*/
|
|
24
|
+
instanceOf(instance,
|
|
25
|
+
/** The constructor that the "instance" input will be checked against. */
|
|
26
|
+
constructor,
|
|
27
|
+
/** Message to include in error message if this assertion fails. */
|
|
28
|
+
failureMessage) {
|
|
29
|
+
if (!(instance instanceof constructor)) {
|
|
30
|
+
throw new AssertionError(`'${stringify(instance)}' is not an instance of '${constructor.name}'`, failureMessage);
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
/**
|
|
34
|
+
* Asserts that a value is _not_ an instance of the given class constructor.
|
|
35
|
+
*
|
|
36
|
+
* Type guards the value.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* import {assert} from '@augment-vir/assert';
|
|
42
|
+
*
|
|
43
|
+
* assert.notInstanceOf(/abc/, RegExp); // fails
|
|
44
|
+
* assert.notInstanceOf('abc', RegExp); // passes
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @throws {@link AssertionError} If the value is an instance of the given class constructor.
|
|
48
|
+
* @see
|
|
49
|
+
* - {@link assert.instanceOf} : the opposite assertion.
|
|
50
|
+
*/
|
|
51
|
+
notInstanceOf(instance, constructor, failureMessage) {
|
|
52
|
+
if (instance instanceof constructor) {
|
|
53
|
+
throw new AssertionError(`'${stringify(instance)}' is an instance of '${constructor.name}'`, failureMessage);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
22
56
|
};
|
|
23
57
|
export const instanceGuards = {
|
|
24
58
|
assert: assertions,
|
|
@@ -40,7 +74,9 @@ export const instanceGuards = {
|
|
|
40
74
|
* @see
|
|
41
75
|
* - {@link check.notInstanceOf} : the opposite check.
|
|
42
76
|
*/
|
|
43
|
-
instanceOf
|
|
77
|
+
instanceOf(instance, constructor) {
|
|
78
|
+
return instance instanceof constructor;
|
|
79
|
+
},
|
|
44
80
|
/**
|
|
45
81
|
* Checks that a value is _not_ an instance of the given class constructor.
|
|
46
82
|
*
|
|
@@ -58,7 +94,9 @@ export const instanceGuards = {
|
|
|
58
94
|
* @see
|
|
59
95
|
* - {@link check.instanceOf} : the opposite check.
|
|
60
96
|
*/
|
|
61
|
-
notInstanceOf
|
|
97
|
+
notInstanceOf(instance, constructor) {
|
|
98
|
+
return !(instance instanceof constructor);
|
|
99
|
+
},
|
|
62
100
|
},
|
|
63
101
|
assertWrap: {
|
|
64
102
|
/**
|
|
@@ -81,7 +119,14 @@ export const instanceGuards = {
|
|
|
81
119
|
* @see
|
|
82
120
|
* - {@link assertWrap.notInstanceOf} : the opposite assertion.
|
|
83
121
|
*/
|
|
84
|
-
instanceOf
|
|
122
|
+
instanceOf(instance, constructor, failureMessage) {
|
|
123
|
+
if (instance instanceof constructor) {
|
|
124
|
+
return instance;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
throw new AssertionError(`'${stringify(instance)}' is not an instance of '${constructor.name}'`, failureMessage);
|
|
128
|
+
}
|
|
129
|
+
},
|
|
85
130
|
/**
|
|
86
131
|
* Asserts that a value is _not_ an instance of the given class constructor. Returns the
|
|
87
132
|
* value if the assertion passes.
|
|
@@ -102,7 +147,14 @@ export const instanceGuards = {
|
|
|
102
147
|
* @see
|
|
103
148
|
* - {@link assertWrap.instanceOf} : the opposite assertion.
|
|
104
149
|
*/
|
|
105
|
-
notInstanceOf
|
|
150
|
+
notInstanceOf(instance, constructor, failureMessage) {
|
|
151
|
+
if (instance instanceof constructor) {
|
|
152
|
+
throw new AssertionError(`'${stringify(instance)}' is an instance of '${constructor.name}'`, failureMessage);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
return instance;
|
|
156
|
+
}
|
|
157
|
+
},
|
|
106
158
|
},
|
|
107
159
|
checkWrap: {
|
|
108
160
|
/**
|
|
@@ -124,7 +176,14 @@ export const instanceGuards = {
|
|
|
124
176
|
* @see
|
|
125
177
|
* - {@link checkWrap.notInstanceOf} : the opposite check.
|
|
126
178
|
*/
|
|
127
|
-
instanceOf
|
|
179
|
+
instanceOf(instance, constructor) {
|
|
180
|
+
if (instance instanceof constructor) {
|
|
181
|
+
return instance;
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
},
|
|
128
187
|
/**
|
|
129
188
|
* Checks that a value is _not_ an instance of the given class constructor. Returns the
|
|
130
189
|
* value if the check passes, otherwise `undefined`.
|
|
@@ -144,7 +203,14 @@ export const instanceGuards = {
|
|
|
144
203
|
* @see
|
|
145
204
|
* - {@link checkWrap.instanceOf} : the opposite check.
|
|
146
205
|
*/
|
|
147
|
-
notInstanceOf
|
|
206
|
+
notInstanceOf(instance, constructor) {
|
|
207
|
+
if (instance instanceof constructor) {
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
return instance;
|
|
212
|
+
}
|
|
213
|
+
},
|
|
148
214
|
},
|
|
149
215
|
waitUntil: {
|
|
150
216
|
/**
|
|
@@ -168,7 +234,7 @@ export const instanceGuards = {
|
|
|
168
234
|
* @see
|
|
169
235
|
* - {@link waitUntil.notInstanceOf} : the opposite assertion.
|
|
170
236
|
*/
|
|
171
|
-
instanceOf:
|
|
237
|
+
instanceOf: createWaitUntil(assertions.instanceOf),
|
|
172
238
|
/**
|
|
173
239
|
* Repeatedly calls a callback until its output is not an instance of the given class
|
|
174
240
|
* constructor. Once the callback output passes, it is returned. If the attempts time out,
|
|
@@ -190,6 +256,6 @@ export const instanceGuards = {
|
|
|
190
256
|
* @see
|
|
191
257
|
* - {@link waitUntil.instanceOf} : the opposite assertion.
|
|
192
258
|
*/
|
|
193
|
-
notInstanceOf:
|
|
259
|
+
notInstanceOf: createWaitUntil(assertions.notInstanceOf),
|
|
194
260
|
},
|
|
195
261
|
};
|