@augment-vir/assert 30.0.0
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/LICENSE-CC0 +121 -0
- package/LICENSE-MIT +21 -0
- package/dist/assertions/boolean.d.ts +97 -0
- package/dist/assertions/boolean.js +47 -0
- package/dist/assertions/boundary.d.ts +83 -0
- package/dist/assertions/boundary.js +104 -0
- package/dist/assertions/enum.d.ts +38 -0
- package/dist/assertions/enum.js +42 -0
- package/dist/assertions/equality/entry-equality.d.ts +40 -0
- package/dist/assertions/equality/entry-equality.js +56 -0
- package/dist/assertions/equality/json-equality.d.ts +42 -0
- package/dist/assertions/equality/json-equality.js +75 -0
- package/dist/assertions/equality/simple-equality.d.ts +85 -0
- package/dist/assertions/equality/simple-equality.js +61 -0
- package/dist/assertions/equality/ts-type-equality.d.ts +42 -0
- package/dist/assertions/equality/ts-type-equality.js +18 -0
- package/dist/assertions/extendable-assertions.d.ts +762 -0
- package/dist/assertions/extendable-assertions.js +108 -0
- package/dist/assertions/http.d.ts +36 -0
- package/dist/assertions/http.js +41 -0
- package/dist/assertions/instance.d.ts +43 -0
- package/dist/assertions/instance.js +41 -0
- package/dist/assertions/keys.d.ts +90 -0
- package/dist/assertions/keys.js +111 -0
- package/dist/assertions/length.d.ts +56 -0
- package/dist/assertions/length.js +68 -0
- package/dist/assertions/nullish.d.ts +43 -0
- package/dist/assertions/nullish.js +42 -0
- package/dist/assertions/numeric.d.ts +69 -0
- package/dist/assertions/numeric.js +60 -0
- package/dist/assertions/output.d.ts +83 -0
- package/dist/assertions/output.example.d.ts +1 -0
- package/dist/assertions/output.example.js +13 -0
- package/dist/assertions/output.js +173 -0
- package/dist/assertions/primitive.d.ts +58 -0
- package/dist/assertions/primitive.js +62 -0
- package/dist/assertions/promise.d.ts +65 -0
- package/dist/assertions/promise.js +69 -0
- package/dist/assertions/regexp.d.ts +19 -0
- package/dist/assertions/regexp.js +18 -0
- package/dist/assertions/runtime-type.d.ts +274 -0
- package/dist/assertions/runtime-type.example.d.ts +1 -0
- package/dist/assertions/runtime-type.example.js +3 -0
- package/dist/assertions/runtime-type.js +242 -0
- package/dist/assertions/throws.d.ts +109 -0
- package/dist/assertions/throws.example.d.ts +1 -0
- package/dist/assertions/throws.example.js +11 -0
- package/dist/assertions/throws.js +166 -0
- package/dist/assertions/uuid.d.ts +35 -0
- package/dist/assertions/uuid.js +33 -0
- package/dist/assertions/values.d.ts +106 -0
- package/dist/assertions/values.js +113 -0
- package/dist/augments/assertion-exports.d.ts +5 -0
- package/dist/augments/assertion-exports.js +1 -0
- package/dist/augments/assertion.error.d.ts +23 -0
- package/dist/augments/assertion.error.example.d.ts +1 -0
- package/dist/augments/assertion.error.example.js +3 -0
- package/dist/augments/assertion.error.js +26 -0
- package/dist/augments/guards/assert-wrap.d.ts +244 -0
- package/dist/augments/guards/assert-wrap.example.d.ts +1 -0
- package/dist/augments/guards/assert-wrap.example.js +9 -0
- package/dist/augments/guards/assert-wrap.js +32 -0
- package/dist/augments/guards/assert.d.ts +177 -0
- package/dist/augments/guards/assert.example.d.ts +1 -0
- package/dist/augments/guards/assert.example.js +4 -0
- package/dist/augments/guards/assert.js +32 -0
- package/dist/augments/guards/check-wrap.d.ts +247 -0
- package/dist/augments/guards/check-wrap.example.d.ts +1 -0
- package/dist/augments/guards/check-wrap.example.js +9 -0
- package/dist/augments/guards/check-wrap.js +34 -0
- package/dist/augments/guards/check.d.ts +229 -0
- package/dist/augments/guards/check.example.d.ts +1 -0
- package/dist/augments/guards/check.example.js +5 -0
- package/dist/augments/guards/check.js +21 -0
- package/dist/augments/guards/wait-until.d.ts +262 -0
- package/dist/augments/guards/wait-until.example.d.ts +1 -0
- package/dist/augments/guards/wait-until.example.js +13 -0
- package/dist/augments/guards/wait-until.js +34 -0
- package/dist/augments/if-equals.d.ts +17 -0
- package/dist/augments/if-equals.js +1 -0
- package/dist/guard-types/assert-function.d.ts +2 -0
- package/dist/guard-types/assert-function.js +3 -0
- package/dist/guard-types/assert-wrap-function.d.ts +9 -0
- package/dist/guard-types/assert-wrap-function.js +14 -0
- package/dist/guard-types/check-function.d.ts +11 -0
- package/dist/guard-types/check-function.js +22 -0
- package/dist/guard-types/check-wrap-wrapper-function.d.ts +9 -0
- package/dist/guard-types/check-wrap-wrapper-function.js +19 -0
- package/dist/guard-types/guard-group.d.ts +13 -0
- package/dist/guard-types/guard-group.js +3 -0
- package/dist/guard-types/guard-override.d.ts +4 -0
- package/dist/guard-types/guard-override.js +10 -0
- package/dist/guard-types/wait-until-function.d.ts +63 -0
- package/dist/guard-types/wait-until-function.js +95 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +8 -0
- package/dist/test-timeout.mock.d.ts +3 -0
- package/dist/test-timeout.mock.js +8 -0
- package/package.json +54 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { stringify } from '@augment-vir/core';
|
|
2
|
+
import { AssertionError } from '../augments/assertion.error.js';
|
|
3
|
+
import { autoGuard } from '../guard-types/guard-override.js';
|
|
4
|
+
import { createWaitUntil } from '../guard-types/wait-until-function.js';
|
|
5
|
+
function isPromiseLike(actual, failureMessage) {
|
|
6
|
+
if (!(actual instanceof Promise) &&
|
|
7
|
+
!(actual &&
|
|
8
|
+
typeof actual === 'object' &&
|
|
9
|
+
'then' in actual &&
|
|
10
|
+
typeof actual.then === 'function')) {
|
|
11
|
+
throw new AssertionError(`'${stringify(actual)}' is not a PromiseLike.`, failureMessage);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function isNotPromiseLike(actual, failureMessage) {
|
|
15
|
+
try {
|
|
16
|
+
isPromiseLike(actual);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
throw new AssertionError(`'${stringify(actual)}' is a PromiseLike.`, failureMessage);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Checks if a value is an actual `Promise` object. In reality this is just a simple wrapper for
|
|
25
|
+
* `instanceof Promise`, but it makes checking a bit more ergonomic.
|
|
26
|
+
*/
|
|
27
|
+
function isPromise(actual, failureMessage) {
|
|
28
|
+
if (!(actual instanceof Promise)) {
|
|
29
|
+
throw new AssertionError(`'${stringify(actual)}' is not a Promise.`, failureMessage);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function isNotPromise(actual, failureMessage) {
|
|
33
|
+
if (actual instanceof Promise) {
|
|
34
|
+
throw new AssertionError(`'${stringify(actual)}' is a Promise.`, failureMessage);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const assertions = {
|
|
38
|
+
isPromiseLike,
|
|
39
|
+
isNotPromiseLike,
|
|
40
|
+
isPromise,
|
|
41
|
+
isNotPromise,
|
|
42
|
+
};
|
|
43
|
+
export const promiseGuards = {
|
|
44
|
+
assertions,
|
|
45
|
+
checkOverrides: {
|
|
46
|
+
isNotPromise: autoGuard(),
|
|
47
|
+
isNotPromiseLike: autoGuard(),
|
|
48
|
+
},
|
|
49
|
+
assertWrapOverrides: {
|
|
50
|
+
isPromise: autoGuard(),
|
|
51
|
+
isNotPromise: autoGuard(),
|
|
52
|
+
isPromiseLike: autoGuard(),
|
|
53
|
+
isNotPromiseLike: autoGuard(),
|
|
54
|
+
},
|
|
55
|
+
checkWrapOverrides: {
|
|
56
|
+
isNotPromise: autoGuard(),
|
|
57
|
+
isNotPromiseLike: autoGuard(),
|
|
58
|
+
},
|
|
59
|
+
/**
|
|
60
|
+
* These overrides must explicitly use `createWaitUntil` so they can pass in `true` for the
|
|
61
|
+
* second parameter, `requireSynchronousResult`.
|
|
62
|
+
*/
|
|
63
|
+
waitUntilOverrides: {
|
|
64
|
+
isPromise: createWaitUntil(isPromise, true),
|
|
65
|
+
isNotPromise: createWaitUntil(isNotPromise, true),
|
|
66
|
+
isPromiseLike: createWaitUntil(isPromiseLike, true),
|
|
67
|
+
isNotPromiseLike: createWaitUntil(isNotPromiseLike, true),
|
|
68
|
+
},
|
|
69
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
declare function matches(actual: string, expected: RegExp, failureMessage?: string | undefined): void;
|
|
2
|
+
declare function mismatches(actual: string, expected: RegExp, failureMessage?: string | undefined): void;
|
|
3
|
+
export declare const regexpGuards: {
|
|
4
|
+
assertions: {
|
|
5
|
+
/**
|
|
6
|
+
* Checks if a string (`actual`) matches a RegExp (`expected`).
|
|
7
|
+
*
|
|
8
|
+
* Performs no type guarding.
|
|
9
|
+
*/
|
|
10
|
+
matches: typeof matches;
|
|
11
|
+
/**
|
|
12
|
+
* Checks if a string (`actual`) does _not_ match a RegExp (`expected`).
|
|
13
|
+
*
|
|
14
|
+
* Performs no type guarding.
|
|
15
|
+
*/
|
|
16
|
+
mismatches: typeof mismatches;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AssertionError } from '../augments/assertion.error.js';
|
|
2
|
+
function matches(actual, expected, failureMessage) {
|
|
3
|
+
if (!expected.test(actual)) {
|
|
4
|
+
throw new AssertionError(`'${actual}' does not match ${expected}`, failureMessage);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
function mismatches(actual, expected, failureMessage) {
|
|
8
|
+
if (expected.test(actual)) {
|
|
9
|
+
throw new AssertionError(`'${actual}' matches ${expected}`, failureMessage);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
const assertions = {
|
|
13
|
+
matches,
|
|
14
|
+
mismatches,
|
|
15
|
+
};
|
|
16
|
+
export const regexpGuards = {
|
|
17
|
+
assertions,
|
|
18
|
+
};
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { AnyFunction, MaybePromise, NarrowToActual, NarrowToExpected, UnknownObject } from '@augment-vir/core';
|
|
2
|
+
import { WaitUntilOptions } from '../guard-types/wait-until-function.js';
|
|
3
|
+
declare function isNotArray<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, ReadonlyArray<unknown>>;
|
|
4
|
+
declare function isNotBigInt<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, bigint>;
|
|
5
|
+
declare function isNotBoolean<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, boolean>;
|
|
6
|
+
declare function isNotFunction<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, AnyFunction>;
|
|
7
|
+
declare function isNotNumber<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, number>;
|
|
8
|
+
declare function isNotObject<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, UnknownObject>;
|
|
9
|
+
declare function isNotString<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, string>;
|
|
10
|
+
declare function isNotSymbol<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, symbol>;
|
|
11
|
+
declare function isNotUndefined<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, undefined>;
|
|
12
|
+
declare function isNotNull<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, null>;
|
|
13
|
+
declare function isArray(actual: unknown, failureMessage?: string | undefined): asserts actual is unknown[];
|
|
14
|
+
declare function isBigInt(actual: unknown, failureMessage?: string | undefined): asserts actual is bigint;
|
|
15
|
+
declare function isBoolean(actual: unknown, failureMessage?: string | undefined): asserts actual is boolean;
|
|
16
|
+
declare function isFunction<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is NarrowToActual<Actual, AnyFunction>;
|
|
17
|
+
declare function isNumber(actual: unknown, failureMessage?: string | undefined): asserts actual is number;
|
|
18
|
+
declare function isObject(actual: unknown, failureMessage?: string | undefined): asserts actual is UnknownObject;
|
|
19
|
+
declare function isString(actual: unknown, failureMessage?: string | undefined): asserts actual is string;
|
|
20
|
+
declare function isSymbol(actual: unknown, failureMessage?: string | undefined): asserts actual is symbol;
|
|
21
|
+
declare function isUndefined(actual: unknown, failureMessage?: string | undefined): asserts actual is undefined;
|
|
22
|
+
declare function isNull(actual: unknown, failureMessage?: string | undefined): asserts actual is null;
|
|
23
|
+
export declare const runtimeTypeGuards: {
|
|
24
|
+
assertions: {
|
|
25
|
+
/**
|
|
26
|
+
* Check if a value is an array.
|
|
27
|
+
*
|
|
28
|
+
* Type guards the value.
|
|
29
|
+
*/
|
|
30
|
+
isArray: typeof isArray;
|
|
31
|
+
/**
|
|
32
|
+
* Check if a value is a bigint.
|
|
33
|
+
*
|
|
34
|
+
* Type guards the value.
|
|
35
|
+
*/
|
|
36
|
+
isBigInt: typeof isBigInt;
|
|
37
|
+
/**
|
|
38
|
+
* Check if a value is a boolean.
|
|
39
|
+
*
|
|
40
|
+
* Type guards the value.
|
|
41
|
+
*/
|
|
42
|
+
isBoolean: typeof isBoolean;
|
|
43
|
+
/**
|
|
44
|
+
* Check if a value is a function.
|
|
45
|
+
*
|
|
46
|
+
* Type guards the value.
|
|
47
|
+
*/
|
|
48
|
+
isFunction: typeof isFunction;
|
|
49
|
+
/**
|
|
50
|
+
* Check if a value is null.
|
|
51
|
+
*
|
|
52
|
+
* Type guards the value.
|
|
53
|
+
*/
|
|
54
|
+
isNull: typeof isNull;
|
|
55
|
+
/**
|
|
56
|
+
* Check if a value is a number.
|
|
57
|
+
*
|
|
58
|
+
* Type guards the value.
|
|
59
|
+
*/
|
|
60
|
+
isNumber: typeof isNumber;
|
|
61
|
+
/**
|
|
62
|
+
* Check if a value is an object.
|
|
63
|
+
*
|
|
64
|
+
* Type guards the value.
|
|
65
|
+
*/
|
|
66
|
+
isObject: typeof isObject;
|
|
67
|
+
/**
|
|
68
|
+
* Check if a value is a string.
|
|
69
|
+
*
|
|
70
|
+
* Type guards the value.
|
|
71
|
+
*/
|
|
72
|
+
isString: typeof isString;
|
|
73
|
+
/**
|
|
74
|
+
* Check if a value is a symbol.
|
|
75
|
+
*
|
|
76
|
+
* Type guards the value.
|
|
77
|
+
*/
|
|
78
|
+
isSymbol: typeof isSymbol;
|
|
79
|
+
/**
|
|
80
|
+
* Check if a value is undefined.
|
|
81
|
+
*
|
|
82
|
+
* Type guards the value.
|
|
83
|
+
*/
|
|
84
|
+
isUndefined: typeof isUndefined;
|
|
85
|
+
/**
|
|
86
|
+
* Check if a value not an array.
|
|
87
|
+
*
|
|
88
|
+
* Type guards the value.
|
|
89
|
+
*/
|
|
90
|
+
isNotArray: typeof isNotArray;
|
|
91
|
+
/**
|
|
92
|
+
* Check if a value is not a bigint.
|
|
93
|
+
*
|
|
94
|
+
* Type guards the value.
|
|
95
|
+
*/
|
|
96
|
+
isNotBigInt: typeof isNotBigInt;
|
|
97
|
+
/**
|
|
98
|
+
* Check if a value is not a boolean.
|
|
99
|
+
*
|
|
100
|
+
* Type guards the value.
|
|
101
|
+
*/
|
|
102
|
+
isNotBoolean: typeof isNotBoolean;
|
|
103
|
+
/**
|
|
104
|
+
* Check if a value is not a function.
|
|
105
|
+
*
|
|
106
|
+
* Type guards the value.
|
|
107
|
+
*/
|
|
108
|
+
isNotFunction: typeof isNotFunction;
|
|
109
|
+
/**
|
|
110
|
+
* Check if a value is not a number.
|
|
111
|
+
*
|
|
112
|
+
* Type guards the value.
|
|
113
|
+
*/
|
|
114
|
+
isNotNumber: typeof isNotNumber;
|
|
115
|
+
/**
|
|
116
|
+
* Check if a value is not an object.
|
|
117
|
+
*
|
|
118
|
+
* Type guards the value.
|
|
119
|
+
*/
|
|
120
|
+
isNotObject: typeof isNotObject;
|
|
121
|
+
/**
|
|
122
|
+
* Check if a value is not a string.
|
|
123
|
+
*
|
|
124
|
+
* Type guards the value.
|
|
125
|
+
*/
|
|
126
|
+
isNotString: typeof isNotString;
|
|
127
|
+
/**
|
|
128
|
+
* Check if a value is not a symbol.
|
|
129
|
+
*
|
|
130
|
+
* Type guards the value.
|
|
131
|
+
*/
|
|
132
|
+
isNotSymbol: typeof isNotSymbol;
|
|
133
|
+
/**
|
|
134
|
+
* Check if a value is not undefined.
|
|
135
|
+
*
|
|
136
|
+
* Type guards the value.
|
|
137
|
+
*/
|
|
138
|
+
isNotUndefined: typeof isNotUndefined;
|
|
139
|
+
/**
|
|
140
|
+
* Check if a value not Null.
|
|
141
|
+
*
|
|
142
|
+
* Type guards the value.
|
|
143
|
+
*/
|
|
144
|
+
isNotNull: typeof isNotNull;
|
|
145
|
+
};
|
|
146
|
+
checkOverrides: {
|
|
147
|
+
isFunction: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is NarrowToActual<Actual, AnyFunction>;
|
|
148
|
+
isNotArray: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, ReadonlyArray<unknown>>;
|
|
149
|
+
isNotBigInt: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, bigint>;
|
|
150
|
+
isNotBoolean: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, boolean>;
|
|
151
|
+
isNotFunction: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, AnyFunction>;
|
|
152
|
+
isNotNull: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, null>;
|
|
153
|
+
isNotNumber: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, number>;
|
|
154
|
+
isNotObject: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, UnknownObject>;
|
|
155
|
+
isNotString: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, string>;
|
|
156
|
+
isNotUndefined: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, undefined>;
|
|
157
|
+
isNotSymbol: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, symbol>;
|
|
158
|
+
};
|
|
159
|
+
assertWrapOverrides: {
|
|
160
|
+
isFunction: <const Actual>(actual: Actual, failureMessage?: string | undefined) => NarrowToActual<Actual, AnyFunction>;
|
|
161
|
+
/**
|
|
162
|
+
* Trying to assign a unique symbol to another variable kills the `unique` part of the
|
|
163
|
+
* symbol. this seems to be a bug with TypeScript itself.
|
|
164
|
+
*
|
|
165
|
+
* For some reason `checkWrap` does not suffer from this issue though.
|
|
166
|
+
*
|
|
167
|
+
* @example Const mySymbol = Symbol('mine'); const mySymbol2 = mySymbol; // this is no
|
|
168
|
+
* longer `unique symbol`
|
|
169
|
+
*/
|
|
170
|
+
isSymbol: <const Actual>(actual: Actual, failureMessage?: string | undefined) => NarrowToExpected<Actual, symbol>;
|
|
171
|
+
isNotArray: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, ReadonlyArray<unknown>>;
|
|
172
|
+
isNotBigInt: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, bigint>;
|
|
173
|
+
isNotBoolean: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, boolean>;
|
|
174
|
+
isNotFunction: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, AnyFunction>;
|
|
175
|
+
isNotNull: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, null>;
|
|
176
|
+
isNotNumber: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, number>;
|
|
177
|
+
isNotObject: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, UnknownObject>;
|
|
178
|
+
isNotString: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, string>;
|
|
179
|
+
isNotUndefined: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, undefined>;
|
|
180
|
+
isNotSymbol: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, symbol>;
|
|
181
|
+
};
|
|
182
|
+
checkWrapOverrides: {
|
|
183
|
+
isFunction: <const Actual>(actual: Actual) => NarrowToActual<Actual, AnyFunction> | undefined;
|
|
184
|
+
/**
|
|
185
|
+
* It doesn't make any sense for `checkWrap.isUndefined` to exist. If the input is
|
|
186
|
+
* `undefined`, it returns `undefined`. If the input isn't `undefined`, it still returns
|
|
187
|
+
* `undefined`.
|
|
188
|
+
*/
|
|
189
|
+
isUndefined: undefined;
|
|
190
|
+
/**
|
|
191
|
+
* It doesn't make any sense for `checkWrap.isNotUndefined` to exist. If the input is not
|
|
192
|
+
* `undefined`, then it still returns `undefined`.
|
|
193
|
+
*/
|
|
194
|
+
isNotUndefined: undefined;
|
|
195
|
+
isNotArray: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, ReadonlyArray<unknown>> | undefined;
|
|
196
|
+
isNotBigInt: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, bigint> | undefined;
|
|
197
|
+
isNotBoolean: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, boolean> | undefined;
|
|
198
|
+
isNotFunction: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, AnyFunction> | undefined;
|
|
199
|
+
isNotNull: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, null> | undefined;
|
|
200
|
+
isNotNumber: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, number> | undefined;
|
|
201
|
+
isNotObject: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, UnknownObject> | undefined;
|
|
202
|
+
isNotString: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, string> | undefined;
|
|
203
|
+
isNotSymbol: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, symbol> | undefined;
|
|
204
|
+
};
|
|
205
|
+
waitUntilOverrides: {
|
|
206
|
+
isFunction: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<NarrowToActual<Actual, AnyFunction>>;
|
|
207
|
+
isNotArray: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, ReadonlyArray<unknown>>>;
|
|
208
|
+
isNotBigInt: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, bigint>>;
|
|
209
|
+
isNotBoolean: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, boolean>>;
|
|
210
|
+
isNotFunction: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, AnyFunction>>;
|
|
211
|
+
isNotNull: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, null>>;
|
|
212
|
+
isNotNumber: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, number>>;
|
|
213
|
+
isNotObject: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, UnknownObject>>;
|
|
214
|
+
isNotString: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, string>>;
|
|
215
|
+
isNotUndefined: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, undefined>>;
|
|
216
|
+
isNotSymbol: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, symbol>>;
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
/**
|
|
220
|
+
* An enum representing the possible values returned by {@link getRuntimeType}. These values are
|
|
221
|
+
* similar to the output of the built-in
|
|
222
|
+
* [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof) operator
|
|
223
|
+
* except that this includes new types `array` and `null`, that `typeof` does not have, which are
|
|
224
|
+
* both distinct from `object`.
|
|
225
|
+
*
|
|
226
|
+
* @category Assert : Util
|
|
227
|
+
* @package @augment-vir/assert
|
|
228
|
+
*/
|
|
229
|
+
export declare enum RuntimeType {
|
|
230
|
+
String = "string",
|
|
231
|
+
Number = "number",
|
|
232
|
+
Bigint = "bigint",
|
|
233
|
+
Boolean = "boolean",
|
|
234
|
+
Symbol = "symbol",
|
|
235
|
+
Undefined = "undefined",
|
|
236
|
+
Object = "object",
|
|
237
|
+
Function = "function",
|
|
238
|
+
/**
|
|
239
|
+
* This is not included in {@link RuntimeType.Object}. (Compared to
|
|
240
|
+
* [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof)
|
|
241
|
+
* which _does_ include `null` in the `'object'` type.)
|
|
242
|
+
*/
|
|
243
|
+
Array = "array",
|
|
244
|
+
/**
|
|
245
|
+
* This is not included in {@link RuntimeType.Object}. (Compared to
|
|
246
|
+
* [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof)
|
|
247
|
+
* which _does_ include `null` in the `'object'` type.)
|
|
248
|
+
*/
|
|
249
|
+
Null = "null"
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Determines the {@link RuntimeType} of a variable. This is similar to the built-in
|
|
253
|
+
* [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof) operator
|
|
254
|
+
* except in the following ways:
|
|
255
|
+
*
|
|
256
|
+
* - This returns an enum value ({@link RuntimeType}) rather than just a string (though the enum values
|
|
257
|
+
* are strings anyway).
|
|
258
|
+
* - This includes new types `array` and `null`, that `typeof` does not have, which are both distinct
|
|
259
|
+
* from `object`.
|
|
260
|
+
*
|
|
261
|
+
* @category Assert : Util
|
|
262
|
+
* @example
|
|
263
|
+
*
|
|
264
|
+
* ```ts
|
|
265
|
+
* import {getRuntimeType} from '@augment-vir/assert';
|
|
266
|
+
*
|
|
267
|
+
* getRuntimeType(['a']); // RuntimeType.Array
|
|
268
|
+
* getRuntimeType({a: 'a'}); // RuntimeType.Object
|
|
269
|
+
* ```
|
|
270
|
+
*
|
|
271
|
+
* @package @augment-vir/assert
|
|
272
|
+
*/
|
|
273
|
+
export declare function getRuntimeType(actual: unknown): RuntimeType;
|
|
274
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { stringify, } from '@augment-vir/core';
|
|
2
|
+
import { AssertionError } from '../augments/assertion.error.js';
|
|
3
|
+
import { autoGuard } from '../guard-types/guard-override.js';
|
|
4
|
+
function isNotArray(actual, failureMessage) {
|
|
5
|
+
assertNotRuntimeType(actual, 'array', failureMessage);
|
|
6
|
+
}
|
|
7
|
+
function isNotBigInt(actual, failureMessage) {
|
|
8
|
+
assertNotRuntimeType(actual, 'bigint', failureMessage);
|
|
9
|
+
}
|
|
10
|
+
function isNotBoolean(actual, failureMessage) {
|
|
11
|
+
assertNotRuntimeType(actual, 'boolean', failureMessage);
|
|
12
|
+
}
|
|
13
|
+
function isNotFunction(actual, failureMessage) {
|
|
14
|
+
assertNotRuntimeType(actual, 'function', failureMessage);
|
|
15
|
+
}
|
|
16
|
+
function isNotNumber(actual, failureMessage) {
|
|
17
|
+
assertNotRuntimeType(actual, 'number', failureMessage);
|
|
18
|
+
}
|
|
19
|
+
function isNotObject(actual, failureMessage) {
|
|
20
|
+
assertNotRuntimeType(actual, 'object', failureMessage);
|
|
21
|
+
}
|
|
22
|
+
function isNotString(actual, failureMessage) {
|
|
23
|
+
assertNotRuntimeType(actual, 'string', failureMessage);
|
|
24
|
+
}
|
|
25
|
+
function isNotSymbol(actual, failureMessage) {
|
|
26
|
+
assertNotRuntimeType(actual, 'symbol', failureMessage);
|
|
27
|
+
}
|
|
28
|
+
function isNotUndefined(actual, failureMessage) {
|
|
29
|
+
assertNotRuntimeType(actual, 'undefined', failureMessage);
|
|
30
|
+
}
|
|
31
|
+
function isNotNull(actual, failureMessage) {
|
|
32
|
+
assertNotRuntimeType(actual, 'null', failureMessage);
|
|
33
|
+
}
|
|
34
|
+
function isArray(actual, failureMessage) {
|
|
35
|
+
assertRuntimeType(actual, 'array', failureMessage);
|
|
36
|
+
}
|
|
37
|
+
function isBigInt(actual, failureMessage) {
|
|
38
|
+
assertRuntimeType(actual, 'bigint', failureMessage);
|
|
39
|
+
}
|
|
40
|
+
function isBoolean(actual, failureMessage) {
|
|
41
|
+
assertRuntimeType(actual, 'boolean', failureMessage);
|
|
42
|
+
}
|
|
43
|
+
function isFunction(actual, failureMessage) {
|
|
44
|
+
assertRuntimeType(actual, 'function', failureMessage);
|
|
45
|
+
}
|
|
46
|
+
function isNumber(actual, failureMessage) {
|
|
47
|
+
assertRuntimeType(actual, 'number', failureMessage);
|
|
48
|
+
}
|
|
49
|
+
function isObject(actual, failureMessage) {
|
|
50
|
+
assertRuntimeType(actual, 'object', failureMessage);
|
|
51
|
+
}
|
|
52
|
+
function isString(actual, failureMessage) {
|
|
53
|
+
assertRuntimeType(actual, 'string', failureMessage);
|
|
54
|
+
}
|
|
55
|
+
function isSymbol(actual, failureMessage) {
|
|
56
|
+
assertRuntimeType(actual, 'symbol', failureMessage);
|
|
57
|
+
}
|
|
58
|
+
function isUndefined(actual, failureMessage) {
|
|
59
|
+
assertRuntimeType(actual, 'undefined', failureMessage);
|
|
60
|
+
}
|
|
61
|
+
function isNull(actual, failureMessage) {
|
|
62
|
+
assertRuntimeType(actual, 'null', failureMessage);
|
|
63
|
+
}
|
|
64
|
+
const assertions = {
|
|
65
|
+
isArray,
|
|
66
|
+
isBigInt,
|
|
67
|
+
isBoolean,
|
|
68
|
+
isFunction,
|
|
69
|
+
isNull,
|
|
70
|
+
isNumber,
|
|
71
|
+
isObject,
|
|
72
|
+
isString,
|
|
73
|
+
isSymbol,
|
|
74
|
+
isUndefined,
|
|
75
|
+
isNotArray,
|
|
76
|
+
isNotBigInt,
|
|
77
|
+
isNotBoolean,
|
|
78
|
+
isNotFunction,
|
|
79
|
+
isNotNumber,
|
|
80
|
+
isNotObject,
|
|
81
|
+
isNotString,
|
|
82
|
+
isNotSymbol,
|
|
83
|
+
isNotUndefined,
|
|
84
|
+
isNotNull,
|
|
85
|
+
};
|
|
86
|
+
export const runtimeTypeGuards = {
|
|
87
|
+
assertions,
|
|
88
|
+
checkOverrides: {
|
|
89
|
+
isFunction: autoGuard(),
|
|
90
|
+
isNotArray: autoGuard(),
|
|
91
|
+
isNotBigInt: autoGuard(),
|
|
92
|
+
isNotBoolean: autoGuard(),
|
|
93
|
+
isNotFunction: autoGuard(),
|
|
94
|
+
isNotNull: autoGuard(),
|
|
95
|
+
isNotNumber: autoGuard(),
|
|
96
|
+
isNotObject: autoGuard(),
|
|
97
|
+
isNotString: autoGuard(),
|
|
98
|
+
isNotUndefined: autoGuard(),
|
|
99
|
+
isNotSymbol: autoGuard(),
|
|
100
|
+
},
|
|
101
|
+
assertWrapOverrides: {
|
|
102
|
+
isFunction: autoGuard(),
|
|
103
|
+
/**
|
|
104
|
+
* Trying to assign a unique symbol to another variable kills the `unique` part of the
|
|
105
|
+
* symbol. this seems to be a bug with TypeScript itself.
|
|
106
|
+
*
|
|
107
|
+
* For some reason `checkWrap` does not suffer from this issue though.
|
|
108
|
+
*
|
|
109
|
+
* @example Const mySymbol = Symbol('mine'); const mySymbol2 = mySymbol; // this is no
|
|
110
|
+
* longer `unique symbol`
|
|
111
|
+
*/
|
|
112
|
+
isSymbol: autoGuard(),
|
|
113
|
+
isNotArray: autoGuard(),
|
|
114
|
+
isNotBigInt: autoGuard(),
|
|
115
|
+
isNotBoolean: autoGuard(),
|
|
116
|
+
isNotFunction: autoGuard(),
|
|
117
|
+
isNotNull: autoGuard(),
|
|
118
|
+
isNotNumber: autoGuard(),
|
|
119
|
+
isNotObject: autoGuard(),
|
|
120
|
+
isNotString: autoGuard(),
|
|
121
|
+
isNotUndefined: autoGuard(),
|
|
122
|
+
isNotSymbol: autoGuard(),
|
|
123
|
+
},
|
|
124
|
+
checkWrapOverrides: {
|
|
125
|
+
isFunction: autoGuard(),
|
|
126
|
+
/**
|
|
127
|
+
* It doesn't make any sense for `checkWrap.isUndefined` to exist. If the input is
|
|
128
|
+
* `undefined`, it returns `undefined`. If the input isn't `undefined`, it still returns
|
|
129
|
+
* `undefined`.
|
|
130
|
+
*/
|
|
131
|
+
isUndefined: undefined,
|
|
132
|
+
/**
|
|
133
|
+
* It doesn't make any sense for `checkWrap.isNotUndefined` to exist. If the input is not
|
|
134
|
+
* `undefined`, then it still returns `undefined`.
|
|
135
|
+
*/
|
|
136
|
+
isNotUndefined: undefined,
|
|
137
|
+
isNotArray: autoGuard(),
|
|
138
|
+
isNotBigInt: autoGuard(),
|
|
139
|
+
isNotBoolean: autoGuard(),
|
|
140
|
+
isNotFunction: autoGuard(),
|
|
141
|
+
isNotNull: autoGuard(),
|
|
142
|
+
isNotNumber: autoGuard(),
|
|
143
|
+
isNotObject: autoGuard(),
|
|
144
|
+
isNotString: autoGuard(),
|
|
145
|
+
isNotSymbol: autoGuard(),
|
|
146
|
+
},
|
|
147
|
+
waitUntilOverrides: {
|
|
148
|
+
isFunction: autoGuard(),
|
|
149
|
+
isNotArray: autoGuard(),
|
|
150
|
+
isNotBigInt: autoGuard(),
|
|
151
|
+
isNotBoolean: autoGuard(),
|
|
152
|
+
isNotFunction: autoGuard(),
|
|
153
|
+
isNotNull: autoGuard(),
|
|
154
|
+
isNotNumber: autoGuard(),
|
|
155
|
+
isNotObject: autoGuard(),
|
|
156
|
+
isNotString: autoGuard(),
|
|
157
|
+
isNotUndefined: autoGuard(),
|
|
158
|
+
isNotSymbol: autoGuard(),
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
/**
|
|
162
|
+
* An enum representing the possible values returned by {@link getRuntimeType}. These values are
|
|
163
|
+
* similar to the output of the built-in
|
|
164
|
+
* [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof) operator
|
|
165
|
+
* except that this includes new types `array` and `null`, that `typeof` does not have, which are
|
|
166
|
+
* both distinct from `object`.
|
|
167
|
+
*
|
|
168
|
+
* @category Assert : Util
|
|
169
|
+
* @package @augment-vir/assert
|
|
170
|
+
*/
|
|
171
|
+
export var RuntimeType;
|
|
172
|
+
(function (RuntimeType) {
|
|
173
|
+
RuntimeType["String"] = "string";
|
|
174
|
+
RuntimeType["Number"] = "number";
|
|
175
|
+
RuntimeType["Bigint"] = "bigint";
|
|
176
|
+
RuntimeType["Boolean"] = "boolean";
|
|
177
|
+
RuntimeType["Symbol"] = "symbol";
|
|
178
|
+
RuntimeType["Undefined"] = "undefined";
|
|
179
|
+
RuntimeType["Object"] = "object";
|
|
180
|
+
RuntimeType["Function"] = "function";
|
|
181
|
+
/**
|
|
182
|
+
* This is not included in {@link RuntimeType.Object}. (Compared to
|
|
183
|
+
* [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof)
|
|
184
|
+
* which _does_ include `null` in the `'object'` type.)
|
|
185
|
+
*/
|
|
186
|
+
RuntimeType["Array"] = "array";
|
|
187
|
+
/**
|
|
188
|
+
* This is not included in {@link RuntimeType.Object}. (Compared to
|
|
189
|
+
* [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof)
|
|
190
|
+
* which _does_ include `null` in the `'object'` type.)
|
|
191
|
+
*/
|
|
192
|
+
RuntimeType["Null"] = "null";
|
|
193
|
+
})(RuntimeType || (RuntimeType = {}));
|
|
194
|
+
/**
|
|
195
|
+
* Determines the {@link RuntimeType} of a variable. This is similar to the built-in
|
|
196
|
+
* [`typeof`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof) operator
|
|
197
|
+
* except in the following ways:
|
|
198
|
+
*
|
|
199
|
+
* - This returns an enum value ({@link RuntimeType}) rather than just a string (though the enum values
|
|
200
|
+
* are strings anyway).
|
|
201
|
+
* - This includes new types `array` and `null`, that `typeof` does not have, which are both distinct
|
|
202
|
+
* from `object`.
|
|
203
|
+
*
|
|
204
|
+
* @category Assert : Util
|
|
205
|
+
* @example
|
|
206
|
+
*
|
|
207
|
+
* ```ts
|
|
208
|
+
* import {getRuntimeType} from '@augment-vir/assert';
|
|
209
|
+
*
|
|
210
|
+
* getRuntimeType(['a']); // RuntimeType.Array
|
|
211
|
+
* getRuntimeType({a: 'a'}); // RuntimeType.Object
|
|
212
|
+
* ```
|
|
213
|
+
*
|
|
214
|
+
* @package @augment-vir/assert
|
|
215
|
+
*/
|
|
216
|
+
export function getRuntimeType(actual) {
|
|
217
|
+
if (actual === null) {
|
|
218
|
+
return RuntimeType.Null;
|
|
219
|
+
}
|
|
220
|
+
else if (Array.isArray(actual)) {
|
|
221
|
+
return RuntimeType.Array;
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
return typeof actual;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Asserts that the given actual matches the given test type. Note that an name for the actual must
|
|
229
|
+
* be provided for error messaging purposes.
|
|
230
|
+
*/
|
|
231
|
+
function assertRuntimeType(actual, testType, failureMessage) {
|
|
232
|
+
const actualType = getRuntimeType(actual);
|
|
233
|
+
if (actualType !== testType) {
|
|
234
|
+
throw new AssertionError(`'${stringify(actual)}' is '${actualType}', not '${testType}'.`, failureMessage);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function assertNotRuntimeType(actual, testType, failureMessage) {
|
|
238
|
+
const actualType = getRuntimeType(actual);
|
|
239
|
+
if (actualType === testType) {
|
|
240
|
+
throw new AssertionError(`'${stringify(actual)}' is '${actualType}'.`, failureMessage);
|
|
241
|
+
}
|
|
242
|
+
}
|