@augment-vir/assert 31.0.0 → 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.
Files changed (63) hide show
  1. package/dist/assertions/boolean.d.ts +20 -26
  2. package/dist/assertions/boolean.js +185 -41
  3. package/dist/assertions/boundary.d.ts +40 -256
  4. package/dist/assertions/boundary.js +265 -229
  5. package/dist/assertions/enum.d.ts +12 -13
  6. package/dist/assertions/enum.js +98 -20
  7. package/dist/assertions/equality/entry-equality.d.ts +11 -15
  8. package/dist/assertions/equality/entry-equality.js +210 -43
  9. package/dist/assertions/equality/json-equality.d.ts +11 -15
  10. package/dist/assertions/equality/json-equality.js +144 -43
  11. package/dist/assertions/equality/simple-equality.d.ts +39 -46
  12. package/dist/assertions/equality/simple-equality.js +316 -61
  13. package/dist/assertions/extendable-assertions.d.ts +0 -12
  14. package/dist/assertions/extendable-assertions.js +0 -12
  15. package/dist/assertions/http.d.ts +10 -14
  16. package/dist/assertions/http.js +96 -28
  17. package/dist/assertions/instance.d.ts +10 -18
  18. package/dist/assertions/instance.js +92 -26
  19. package/dist/assertions/keys.d.ts +59 -138
  20. package/dist/assertions/keys.js +279 -162
  21. package/dist/assertions/length.d.ts +30 -212
  22. package/dist/assertions/length.js +117 -175
  23. package/dist/assertions/nullish.d.ts +8 -20
  24. package/dist/assertions/nullish.js +85 -27
  25. package/dist/assertions/numeric.d.ts +67 -81
  26. package/dist/assertions/numeric.js +564 -133
  27. package/dist/assertions/output.d.ts +2 -3
  28. package/dist/assertions/output.js +1 -7
  29. package/dist/assertions/primitive.d.ts +33 -40
  30. package/dist/assertions/primitive.js +232 -66
  31. package/dist/assertions/promise.d.ts +20 -30
  32. package/dist/assertions/promise.js +244 -53
  33. package/dist/assertions/regexp.d.ts +12 -14
  34. package/dist/assertions/regexp.js +84 -21
  35. package/dist/assertions/runtime-type.d.ts +99 -150
  36. package/dist/assertions/runtime-type.js +805 -229
  37. package/dist/assertions/throws.d.ts +24 -25
  38. package/dist/assertions/throws.js +43 -5
  39. package/dist/assertions/uuid.d.ts +11 -16
  40. package/dist/assertions/uuid.js +91 -22
  41. package/dist/assertions/values.d.ts +81 -210
  42. package/dist/assertions/values.js +627 -234
  43. package/dist/augments/guards/assert-wrap.d.ts +7 -4
  44. package/dist/augments/guards/assert-wrap.js +5 -4
  45. package/dist/augments/guards/check-wrap.d.ts +7 -5
  46. package/dist/augments/guards/check-wrap.js +5 -4
  47. package/dist/augments/guards/check.d.ts +5 -5
  48. package/dist/augments/guards/check.js +5 -4
  49. package/dist/augments/guards/wait-until.d.ts +8 -4
  50. package/dist/augments/guards/wait-until.js +7 -8
  51. package/dist/guard-types/guard-group.d.ts +5 -2
  52. package/dist/guard-types/wait-until-function.d.ts +2 -10
  53. package/dist/guard-types/wait-until-function.js +1 -9
  54. package/dist/index.d.ts +1 -0
  55. package/package.json +2 -2
  56. package/dist/guard-types/assert-wrap-function.d.ts +0 -12
  57. package/dist/guard-types/assert-wrap-function.js +0 -14
  58. package/dist/guard-types/check-function.d.ts +0 -14
  59. package/dist/guard-types/check-function.js +0 -22
  60. package/dist/guard-types/check-wrap-wrapper-function.d.ts +0 -12
  61. package/dist/guard-types/check-wrap-wrapper-function.js +0 -19
  62. package/dist/guard-types/guard-override.d.ts +0 -4
  63. package/dist/guard-types/guard-override.js +0 -10
@@ -1,7 +1,6 @@
1
- import { type MaybePromise, type PartialWithNullable, type TypedFunction } from '@augment-vir/core';
2
- import { autoGuardSymbol } from '../guard-types/guard-override.js';
1
+ import { type MaybePromise, type NarrowToExpected, type PartialWithNullable, type TypedFunction } from '@augment-vir/core';
3
2
  import { type WaitUntilOptions } from '../guard-types/wait-until-function.js';
4
- declare function isError(actual: unknown, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): asserts actual is Error;
3
+ declare function isError(this: void, actual: unknown, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): asserts actual is Error;
5
4
  /**
6
5
  * A type that represents possible error matching patterns. This is used by the `.throws` and
7
6
  * `isError`, guards in `@augment-vir/assert` as well as `itCases` in `@augment-vir/test`. Each
@@ -50,24 +49,24 @@ export type ErrorMatchOptions = PartialWithNullable<{
50
49
  */
51
50
  matchConstructor: ErrorConstructor | (new (...args: any[]) => Error);
52
51
  }>;
53
- declare function throws(callbackOrPromise: TypedFunction<void, never>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): void;
54
- declare function throws(callbackOrPromise: TypedFunction<void, Promise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Promise<void>;
55
- declare function throws(callback: TypedFunction<void, any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): void;
56
- declare function throws(callback: TypedFunction<void, MaybePromise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): MaybePromise<void>;
57
- declare function throwsCheck(callbackOrPromise: TypedFunction<void, never>, matchOptions?: ErrorMatchOptions | undefined): boolean;
58
- declare function throwsCheck(callbackOrPromise: TypedFunction<void, Promise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined): Promise<boolean>;
59
- declare function throwsCheck(callback: TypedFunction<void, any>, matchOptions?: ErrorMatchOptions | undefined): boolean;
60
- declare function throwsCheck(callback: TypedFunction<void, MaybePromise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined): MaybePromise<boolean>;
61
- declare function throwsAssertWrap(callbackOrPromise: TypedFunction<void, never>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Error;
62
- declare function throwsAssertWrap(callbackOrPromise: TypedFunction<void, Promise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Promise<Error>;
63
- declare function throwsAssertWrap(callback: TypedFunction<void, any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Error;
64
- declare function throwsAssertWrap(callback: TypedFunction<void, MaybePromise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): MaybePromise<Error>;
65
- declare function throwsCheckWrap(callbackOrPromise: TypedFunction<void, never>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Error | undefined;
66
- declare function throwsCheckWrap(callbackOrPromise: TypedFunction<void, Promise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Promise<Error | undefined>;
67
- declare function throwsCheckWrap(callback: TypedFunction<void, any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Error | undefined;
68
- declare function throwsCheckWrap(callback: TypedFunction<void, MaybePromise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): MaybePromise<Error | undefined>;
69
- declare function throwsWaitUntil(callback: TypedFunction<void, any>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined): Promise<Error>;
70
- declare function throwsWaitUntil(matchOptions: ErrorMatchOptions, callback: TypedFunction<void, any>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined): Promise<Error>;
52
+ declare function throws(this: void, callbackOrPromise: TypedFunction<void, never>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): void;
53
+ declare function throws(this: void, callbackOrPromise: TypedFunction<void, Promise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Promise<void>;
54
+ declare function throws(this: void, callback: TypedFunction<void, any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): void;
55
+ declare function throws(this: void, callback: TypedFunction<void, MaybePromise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): MaybePromise<void>;
56
+ declare function throwsCheck(this: void, callbackOrPromise: TypedFunction<void, never>, matchOptions?: ErrorMatchOptions | undefined): boolean;
57
+ declare function throwsCheck(this: void, callbackOrPromise: TypedFunction<void, Promise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined): Promise<boolean>;
58
+ declare function throwsCheck(this: void, callback: TypedFunction<void, any>, matchOptions?: ErrorMatchOptions | undefined): boolean;
59
+ declare function throwsCheck(this: void, callback: TypedFunction<void, MaybePromise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined): MaybePromise<boolean>;
60
+ declare function throwsAssertWrap(this: void, callbackOrPromise: TypedFunction<void, never>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Error;
61
+ declare function throwsAssertWrap(this: void, callbackOrPromise: TypedFunction<void, Promise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Promise<Error>;
62
+ declare function throwsAssertWrap(this: void, callback: TypedFunction<void, any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Error;
63
+ declare function throwsAssertWrap(this: void, callback: TypedFunction<void, MaybePromise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): MaybePromise<Error>;
64
+ declare function throwsCheckWrap(this: void, callbackOrPromise: TypedFunction<void, never>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Error | undefined;
65
+ declare function throwsCheckWrap(this: void, callbackOrPromise: TypedFunction<void, Promise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Promise<Error | undefined>;
66
+ declare function throwsCheckWrap(this: void, callback: TypedFunction<void, any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Error | undefined;
67
+ declare function throwsCheckWrap(this: void, callback: TypedFunction<void, MaybePromise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): MaybePromise<Error | undefined>;
68
+ declare function throwsWaitUntil(this: void, callback: TypedFunction<void, any>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined): Promise<Error>;
69
+ declare function throwsWaitUntil(this: void, matchOptions: ErrorMatchOptions, callback: TypedFunction<void, any>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined): Promise<Error>;
71
70
  export declare const throwGuards: {
72
71
  assert: {
73
72
  /**
@@ -179,7 +178,7 @@ export declare const throwGuards: {
179
178
  * check.isError({message: 'not an error'}); // returns `false`
180
179
  * ```
181
180
  */
182
- isError: typeof autoGuardSymbol;
181
+ isError(this: void, actual: unknown, matchOptions?: ErrorMatchOptions | undefined): actual is Error;
183
182
  };
184
183
  assertWrap: {
185
184
  /**
@@ -241,7 +240,7 @@ export declare const throwGuards: {
241
240
  * @returns The value if the assertion passes.
242
241
  * @throws {@link AssertionError} If the assertion fails.
243
242
  */
244
- isError: typeof autoGuardSymbol;
243
+ isError<Actual>(this: void, actual: Actual, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): NarrowToExpected<Actual, Error>;
245
244
  };
246
245
  checkWrap: {
247
246
  /**
@@ -296,7 +295,7 @@ export declare const throwGuards: {
296
295
  *
297
296
  * @returns The Error if the check passes, otherwise `undefined`.
298
297
  */
299
- isError: typeof autoGuardSymbol;
298
+ isError<Actual>(this: void, actual: Actual, matchOptions?: ErrorMatchOptions | undefined): NarrowToExpected<Actual, Error> | undefined;
300
299
  };
301
300
  waitUntil: {
302
301
  /**
@@ -351,7 +350,7 @@ export declare const throwGuards: {
351
350
  * @returns The callback output once it passes.
352
351
  * @throws {@link AssertionError} On timeout.
353
352
  */
354
- isError: typeof autoGuardSymbol;
353
+ isError: <Actual>(this: void, matchOptions: ErrorMatchOptions | undefined, callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<NarrowToExpected<Actual, Error>>;
355
354
  };
356
355
  };
357
356
  export {};
@@ -1,6 +1,5 @@
1
1
  import { ensureError, extractErrorMessage, stringify, } from '@augment-vir/core';
2
2
  import { AssertionError } from '../augments/assertion.error.js';
3
- import { autoGuardSymbol } from '../guard-types/guard-override.js';
4
3
  import { createWaitUntil } from '../guard-types/wait-until-function.js';
5
4
  var ThrowsCheckType;
6
5
  (function (ThrowsCheckType) {
@@ -45,6 +44,30 @@ function internalAssertError(actual, errorMessages, matchOptions, failureMessage
45
44
  }
46
45
  }
47
46
  }
47
+ function internalCheckError(actual, matchOptions) {
48
+ if (!actual) {
49
+ return false;
50
+ }
51
+ else if (!(actual instanceof Error)) {
52
+ return false;
53
+ }
54
+ else if (matchOptions?.matchConstructor &&
55
+ !(actual instanceof matchOptions.matchConstructor)) {
56
+ return false;
57
+ }
58
+ else if (matchOptions?.matchMessage) {
59
+ const message = extractErrorMessage(actual);
60
+ if (typeof matchOptions.matchMessage === 'string') {
61
+ if (!message.includes(matchOptions.matchMessage)) {
62
+ return false;
63
+ }
64
+ }
65
+ else if (!message.match(matchOptions.matchMessage)) {
66
+ return false;
67
+ }
68
+ }
69
+ return true;
70
+ }
48
71
  function internalThrowsCheck(checkType, callbackOrPromise, matchOptions, failureMessage) {
49
72
  let caughtError = undefined;
50
73
  try {
@@ -201,7 +224,9 @@ export const throwGuards = {
201
224
  * check.isError({message: 'not an error'}); // returns `false`
202
225
  * ```
203
226
  */
204
- isError: autoGuardSymbol,
227
+ isError(actual, matchOptions) {
228
+ return internalCheckError(actual, matchOptions);
229
+ },
205
230
  },
206
231
  assertWrap: {
207
232
  /**
@@ -263,7 +288,13 @@ export const throwGuards = {
263
288
  * @returns The value if the assertion passes.
264
289
  * @throws {@link AssertionError} If the assertion fails.
265
290
  */
266
- isError: autoGuardSymbol,
291
+ isError(actual, matchOptions, failureMessage) {
292
+ internalAssertError(actual, {
293
+ noError: 'No error.',
294
+ notInstance: `'${stringify(actual)}' is not an error instance.`,
295
+ }, matchOptions, failureMessage);
296
+ return actual;
297
+ },
267
298
  },
268
299
  checkWrap: {
269
300
  /**
@@ -318,7 +349,14 @@ export const throwGuards = {
318
349
  *
319
350
  * @returns The Error if the check passes, otherwise `undefined`.
320
351
  */
321
- isError: autoGuardSymbol,
352
+ isError(actual, matchOptions) {
353
+ if (internalCheckError(actual, matchOptions)) {
354
+ return actual;
355
+ }
356
+ else {
357
+ return undefined;
358
+ }
359
+ },
322
360
  },
323
361
  waitUntil: {
324
362
  /**
@@ -373,6 +411,6 @@ export const throwGuards = {
373
411
  * @returns The callback output once it passes.
374
412
  * @throws {@link AssertionError} On timeout.
375
413
  */
376
- isError: autoGuardSymbol,
414
+ isError: createWaitUntil(isError),
377
415
  },
378
416
  };
@@ -1,9 +1,5 @@
1
- import { type MaybePromise, type Uuid } from '@augment-vir/core';
2
- import { autoGuardSymbol } from '../guard-types/guard-override.js';
1
+ import { type MaybePromise, type NarrowToExpected, type Uuid } from '@augment-vir/core';
3
2
  import { type WaitUntilOptions } from '../guard-types/wait-until-function.js';
4
- /** Checks if the input string is a valid v4 UUID. */
5
- declare function isUuid(actual: unknown, failureMessage?: string | undefined): asserts actual is Uuid;
6
- declare function isNotUuid<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, Uuid>;
7
3
  export declare const uuidGuards: {
8
4
  assert: {
9
5
  /**
@@ -28,7 +24,7 @@ export declare const uuidGuards: {
28
24
  * @see
29
25
  * - {@link assert.isNotUuid} : the opposite assertion.
30
26
  */
31
- isUuid: typeof isUuid;
27
+ isUuid(this: void, actual: unknown, failureMessage?: string | undefined): asserts actual is Uuid;
32
28
  /**
33
29
  * Asserts that a value is _not_ a valid UUID. The nil or max UUIDs are included as _not_ valid.
34
30
  *
@@ -51,7 +47,7 @@ export declare const uuidGuards: {
51
47
  * @see
52
48
  * - {@link assert.isUuid} : the opposite assertion.
53
49
  */
54
- isNotUuid: typeof isNotUuid;
50
+ isNotUuid<const Actual>(this: void, actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, Uuid>;
55
51
  };
56
52
  check: {
57
53
  /**
@@ -75,7 +71,7 @@ export declare const uuidGuards: {
75
71
  * @see
76
72
  * - {@link check.isNotUuid} : the opposite check.
77
73
  */
78
- isUuid: typeof autoGuardSymbol;
74
+ isUuid(this: void, actual: unknown): actual is Uuid;
79
75
  /**
80
76
  * Checks that a value is _not_ a valid UUID. The nil or max UUIDs are included as _not_
81
77
  * valid.
@@ -98,7 +94,7 @@ export declare const uuidGuards: {
98
94
  * @see
99
95
  * - {@link check.isUuid} : the opposite check.
100
96
  */
101
- isNotUuid: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, Uuid>;
97
+ isNotUuid<Actual>(this: void, actual: Actual): actual is Exclude<Actual, Uuid>;
102
98
  };
103
99
  assertWrap: {
104
100
  /**
@@ -124,7 +120,7 @@ export declare const uuidGuards: {
124
120
  * @see
125
121
  * - {@link assertWrap.isNotUuid} : the opposite assertion.
126
122
  */
127
- isUuid: typeof autoGuardSymbol;
123
+ isUuid<Actual>(this: void, actual: unknown, failureMessage?: string | undefined): NarrowToExpected<Actual, Uuid>;
128
124
  /**
129
125
  * Asserts that a value is _not_ a valid UUID. The nil or max UUIDs are included as _not_
130
126
  * valid. Returns the value if the assertion passes.
@@ -148,7 +144,7 @@ export declare const uuidGuards: {
148
144
  * @see
149
145
  * - {@link assertWrap.isUuid} : the opposite assertion.
150
146
  */
151
- isNotUuid: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, Uuid>;
147
+ isNotUuid<const Actual>(this: void, actual: Actual, failureMessage?: string | undefined): Exclude<Actual, Uuid>;
152
148
  };
153
149
  checkWrap: {
154
150
  /**
@@ -174,7 +170,7 @@ export declare const uuidGuards: {
174
170
  * @see
175
171
  * - {@link checkWrap.isNotUuid} : the opposite check.
176
172
  */
177
- isUuid: typeof autoGuardSymbol;
173
+ isUuid<Actual>(this: void, actual: Actual): NarrowToExpected<Actual, Uuid> | undefined;
178
174
  /**
179
175
  * Checks that a value is _not_ a valid UUID. The nil or max UUIDs are included as _not_
180
176
  * valid. Returns the value if the check passes, otherwise `undefined`.
@@ -198,7 +194,7 @@ export declare const uuidGuards: {
198
194
  * @see
199
195
  * - {@link checkWrap.isUuid} : the opposite check.
200
196
  */
201
- isNotUuid: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, Uuid> | undefined;
197
+ isNotUuid<Actual>(this: void, actual: Actual): Exclude<Actual, Uuid> | undefined;
202
198
  };
203
199
  waitUntil: {
204
200
  /**
@@ -226,7 +222,7 @@ export declare const uuidGuards: {
226
222
  * @see
227
223
  * - {@link waitUntil.isNotUuid} : the opposite assertion.
228
224
  */
229
- isUuid: typeof autoGuardSymbol;
225
+ isUuid: <const Actual>(this: void, callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<NarrowToExpected<Actual, Uuid>>;
230
226
  /**
231
227
  * Repeatedly calls a callback until its output is _not_ a valid UUID. The nil or max UUIDs
232
228
  * are included as _not_ valid. Returns the value if the assertion passes. Once the callback
@@ -252,7 +248,6 @@ export declare const uuidGuards: {
252
248
  * @see
253
249
  * - {@link waitUntil.isUuid} : the opposite assertion.
254
250
  */
255
- isNotUuid: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, Uuid>>;
251
+ isNotUuid: <const Actual>(this: void, callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, Uuid>>;
256
252
  };
257
253
  };
258
- export {};
@@ -1,20 +1,61 @@
1
1
  import { AssertionError } from '../augments/assertion.error.js';
2
- import { autoGuard, autoGuardSymbol } from '../guard-types/guard-override.js';
2
+ import { createWaitUntil } from '../guard-types/wait-until-function.js';
3
3
  const uuidRegExp = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
4
- /** Checks if the input string is a valid v4 UUID. */
5
- function isUuid(actual, failureMessage) {
6
- if (!String(actual).match(uuidRegExp)) {
7
- throw new AssertionError(`'${String(actual)}' is not a UUID.`, failureMessage);
8
- }
9
- }
10
- function isNotUuid(actual, failureMessage) {
11
- if (String(actual).match(uuidRegExp)) {
12
- throw new AssertionError(`'${String(actual)}' is a UUID.`, failureMessage);
13
- }
14
- }
15
4
  const assertions = {
16
- isUuid,
17
- isNotUuid,
5
+ /**
6
+ * Asserts that a value is a valid UUID. Does not accept the nil or max UUIDs.
7
+ *
8
+ * Type guards the value.
9
+ *
10
+ * @example
11
+ *
12
+ * ```ts
13
+ * import {assert} from '@augment-vir/assert';
14
+ * import {createUuidV4} from '@augment-vir/common';
15
+ *
16
+ * assert.isUuid(createUuidV4()); // passes
17
+ * assert.isUuid('29e0f18e-6115-4982-8342-0afcadf5d611'); // passes
18
+ * assert.isUuid('00000000-0000-0000-0000-000000000000'); // fails
19
+ * assert.isUuid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'); // fails
20
+ * assert.isUuid('not-a-uuid'); // fails
21
+ * ```
22
+ *
23
+ * @throws {@link AssertionError} If the assertion fails.
24
+ * @see
25
+ * - {@link assert.isNotUuid} : the opposite assertion.
26
+ */
27
+ isUuid(actual, failureMessage) {
28
+ if (!String(actual).match(uuidRegExp)) {
29
+ throw new AssertionError(`'${String(actual)}' is not a UUID.`, failureMessage);
30
+ }
31
+ },
32
+ /**
33
+ * Asserts that a value is _not_ a valid UUID. The nil or max UUIDs are included as _not_ valid.
34
+ *
35
+ * Type guards the value.
36
+ *
37
+ * @example
38
+ *
39
+ * ```ts
40
+ * import {assert} from '@augment-vir/assert';
41
+ * import {createUuidV4} from '@augment-vir/common';
42
+ *
43
+ * assert.isNotUuid(createUuidV4()); // fails
44
+ * assert.isNotUuid('29e0f18e-6115-4982-8342-0afcadf5d611'); // fails
45
+ * assert.isNotUuid('00000000-0000-0000-0000-000000000000'); // passes
46
+ * assert.isNotUuid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'); // passes
47
+ * assert.isNotUuid('not-a-uuid'); // passes
48
+ * ```
49
+ *
50
+ * @throws {@link AssertionError} If the assertion fails.
51
+ * @see
52
+ * - {@link assert.isUuid} : the opposite assertion.
53
+ */
54
+ isNotUuid(actual, failureMessage) {
55
+ if (String(actual).match(uuidRegExp)) {
56
+ throw new AssertionError(`'${String(actual)}' is a UUID.`, failureMessage);
57
+ }
58
+ },
18
59
  };
19
60
  export const uuidGuards = {
20
61
  assert: assertions,
@@ -40,7 +81,9 @@ export const uuidGuards = {
40
81
  * @see
41
82
  * - {@link check.isNotUuid} : the opposite check.
42
83
  */
43
- isUuid: autoGuardSymbol,
84
+ isUuid(actual) {
85
+ return !!String(actual).match(uuidRegExp);
86
+ },
44
87
  /**
45
88
  * Checks that a value is _not_ a valid UUID. The nil or max UUIDs are included as _not_
46
89
  * valid.
@@ -63,7 +106,9 @@ export const uuidGuards = {
63
106
  * @see
64
107
  * - {@link check.isUuid} : the opposite check.
65
108
  */
66
- isNotUuid: autoGuard(),
109
+ isNotUuid(actual) {
110
+ return !String(actual).match(uuidRegExp);
111
+ },
67
112
  },
68
113
  assertWrap: {
69
114
  /**
@@ -89,7 +134,12 @@ export const uuidGuards = {
89
134
  * @see
90
135
  * - {@link assertWrap.isNotUuid} : the opposite assertion.
91
136
  */
92
- isUuid: autoGuardSymbol,
137
+ isUuid(actual, failureMessage) {
138
+ if (!String(actual).match(uuidRegExp)) {
139
+ throw new AssertionError(`'${String(actual)}' is not a UUID.`, failureMessage);
140
+ }
141
+ return actual;
142
+ },
93
143
  /**
94
144
  * Asserts that a value is _not_ a valid UUID. The nil or max UUIDs are included as _not_
95
145
  * valid. Returns the value if the assertion passes.
@@ -113,7 +163,12 @@ export const uuidGuards = {
113
163
  * @see
114
164
  * - {@link assertWrap.isUuid} : the opposite assertion.
115
165
  */
116
- isNotUuid: autoGuard(),
166
+ isNotUuid(actual, failureMessage) {
167
+ if (String(actual).match(uuidRegExp)) {
168
+ throw new AssertionError(`'${String(actual)}' is a UUID.`, failureMessage);
169
+ }
170
+ return actual;
171
+ },
117
172
  },
118
173
  checkWrap: {
119
174
  /**
@@ -139,7 +194,14 @@ export const uuidGuards = {
139
194
  * @see
140
195
  * - {@link checkWrap.isNotUuid} : the opposite check.
141
196
  */
142
- isUuid: autoGuardSymbol,
197
+ isUuid(actual) {
198
+ if (String(actual).match(uuidRegExp)) {
199
+ return actual;
200
+ }
201
+ else {
202
+ return undefined;
203
+ }
204
+ },
143
205
  /**
144
206
  * Checks that a value is _not_ a valid UUID. The nil or max UUIDs are included as _not_
145
207
  * valid. Returns the value if the check passes, otherwise `undefined`.
@@ -163,7 +225,14 @@ export const uuidGuards = {
163
225
  * @see
164
226
  * - {@link checkWrap.isUuid} : the opposite check.
165
227
  */
166
- isNotUuid: autoGuard(),
228
+ isNotUuid(actual) {
229
+ if (String(actual).match(uuidRegExp)) {
230
+ return undefined;
231
+ }
232
+ else {
233
+ return actual;
234
+ }
235
+ },
167
236
  },
168
237
  waitUntil: {
169
238
  /**
@@ -191,7 +260,7 @@ export const uuidGuards = {
191
260
  * @see
192
261
  * - {@link waitUntil.isNotUuid} : the opposite assertion.
193
262
  */
194
- isUuid: autoGuardSymbol,
263
+ isUuid: createWaitUntil(assertions.isUuid),
195
264
  /**
196
265
  * Repeatedly calls a callback until its output is _not_ a valid UUID. The nil or max UUIDs
197
266
  * are included as _not_ valid. Returns the value if the assertion passes. Once the callback
@@ -217,6 +286,6 @@ export const uuidGuards = {
217
286
  * @see
218
287
  * - {@link waitUntil.isUuid} : the opposite assertion.
219
288
  */
220
- isNotUuid: autoGuard(),
289
+ isNotUuid: createWaitUntil(assertions.isNotUuid),
221
290
  },
222
291
  };