@ls-stack/utils 3.39.0 → 3.41.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/dist/arrayUtils.d.cts +70 -57
- package/dist/arrayUtils.d.ts +70 -57
- package/dist/assertions.d.cts +128 -117
- package/dist/assertions.d.ts +128 -117
- package/dist/asyncQueue.cjs +117 -112
- package/dist/asyncQueue.d.cts +217 -215
- package/dist/asyncQueue.d.ts +217 -215
- package/dist/asyncQueue.js +117 -112
- package/dist/awaitDebounce.d.cts +26 -23
- package/dist/awaitDebounce.d.ts +26 -23
- package/dist/cache.cjs +2 -1
- package/dist/cache.d.cts +15 -9
- package/dist/cache.d.ts +15 -9
- package/dist/cache.js +2 -1
- package/dist/{chunk-GHAQOUA6.js → chunk-23KPGKDT.js} +22 -1
- package/dist/{chunk-ADM37GSC.js → chunk-7L4KCZJJ.js} +12 -10
- package/dist/{chunk-KW55OTUG.js → chunk-B3KFV2MH.js} +2 -2
- package/dist/chunk-DTE2QMWE.js +48 -0
- package/dist/concurrentCalls.d.cts +8 -4
- package/dist/concurrentCalls.d.ts +8 -4
- package/dist/concurrentCalls.js +1 -1
- package/dist/consoleFmt.cjs +1 -0
- package/dist/consoleFmt.d.cts +6 -0
- package/dist/consoleFmt.d.ts +6 -0
- package/dist/consoleFmt.js +1 -0
- package/dist/debounce.d.cts +12 -20
- package/dist/debounce.d.ts +12 -20
- package/dist/dedent.d.cts +31 -31
- package/dist/dedent.d.ts +31 -31
- package/dist/deepEqual.d.cts +10 -9
- package/dist/deepEqual.d.ts +10 -9
- package/dist/filterObjectOrArrayKeys.d.cts +44 -19
- package/dist/filterObjectOrArrayKeys.d.ts +44 -19
- package/dist/getAutoIncrementId.d.cts +25 -23
- package/dist/getAutoIncrementId.d.ts +25 -23
- package/dist/getCompositeKey.d.cts +2 -1
- package/dist/getCompositeKey.d.ts +2 -1
- package/dist/getValueStableKey.d.cts +4 -3
- package/dist/getValueStableKey.d.ts +4 -3
- package/dist/mutationUtils.d.cts +2 -2
- package/dist/mutationUtils.d.ts +2 -2
- package/dist/objUtils.cjs +51 -2
- package/dist/objUtils.d.cts +8 -3
- package/dist/objUtils.d.ts +8 -3
- package/dist/objUtils.js +11 -3
- package/dist/parallelAsyncCalls.cjs +2 -1
- package/dist/parallelAsyncCalls.d.cts +5 -4
- package/dist/parallelAsyncCalls.d.ts +5 -4
- package/dist/parallelAsyncCalls.js +2 -1
- package/dist/retryOnError.d.cts +23 -20
- package/dist/retryOnError.d.ts +23 -20
- package/dist/runShellCmd.d.cts +40 -40
- package/dist/runShellCmd.d.ts +40 -40
- package/dist/safeJson.d.cts +6 -2
- package/dist/safeJson.d.ts +6 -2
- package/dist/saferTyping.d.cts +21 -14
- package/dist/saferTyping.d.ts +21 -14
- package/dist/shallowEqual.d.cts +1 -1
- package/dist/shallowEqual.d.ts +1 -1
- package/dist/stringUtils.cjs +2 -2
- package/dist/stringUtils.d.cts +6 -7
- package/dist/stringUtils.d.ts +6 -7
- package/dist/stringUtils.js +1 -1
- package/dist/testUtils.cjs +11 -9
- package/dist/testUtils.d.cts +32 -14
- package/dist/testUtils.d.ts +32 -14
- package/dist/testUtils.js +4 -3
- package/dist/throttle.d.cts +57 -48
- package/dist/throttle.d.ts +57 -48
- package/dist/timers.d.cts +68 -63
- package/dist/timers.d.ts +68 -63
- package/dist/tsResult.d.cts +7 -6
- package/dist/tsResult.d.ts +7 -6
- package/dist/typeGuards.d.cts +65 -64
- package/dist/typeGuards.d.ts +65 -64
- package/dist/typeUtils.d.cts +18 -4
- package/dist/typeUtils.d.ts +18 -4
- package/dist/typedStrings.d.cts +68 -57
- package/dist/typedStrings.d.ts +68 -57
- package/dist/typingFnUtils.d.cts +12 -6
- package/dist/typingFnUtils.d.ts +12 -6
- package/dist/typingFnUtils.js +12 -35
- package/dist/typingTestUtils.cjs +7 -1
- package/dist/typingTestUtils.d.cts +52 -11
- package/dist/typingTestUtils.d.ts +52 -11
- package/dist/typingTestUtils.js +7 -1
- package/dist/typingUtils.d.cts +4 -1
- package/dist/typingUtils.d.ts +4 -1
- package/dist/yamlStringify.cjs +11 -9
- package/dist/yamlStringify.js +2 -2
- package/package.json +6 -2
- package/docs/README.md +0 -72
- package/docs/_media/modules.md +0 -58
- package/docs/arrayUtils/-internal-.md +0 -179
- package/docs/arrayUtils/README.md +0 -550
- package/docs/assertions/-internal-.md +0 -63
- package/docs/assertions/README.md +0 -565
- package/docs/asyncQueue/-internal-.md +0 -297
- package/docs/asyncQueue/README.md +0 -1485
- package/docs/awaitDebounce.md +0 -66
- package/docs/cache/-internal-.md +0 -168
- package/docs/cache/README.md +0 -360
- package/docs/castValues.md +0 -47
- package/docs/concurrentCalls/-internal-.md +0 -490
- package/docs/concurrentCalls/README.md +0 -299
- package/docs/consoleFmt.md +0 -91
- package/docs/conversions.md +0 -27
- package/docs/createThrottleController/-internal-.md +0 -73
- package/docs/createThrottleController/README.md +0 -31
- package/docs/debounce.md +0 -188
- package/docs/dedent/-internal-.md +0 -17
- package/docs/dedent/README.md +0 -204
- package/docs/deepEqual.md +0 -94
- package/docs/enhancedMap.md +0 -358
- package/docs/exhaustiveMatch/-internal-.md +0 -39
- package/docs/exhaustiveMatch/README.md +0 -146
- package/docs/filterObjectOrArrayKeys.md +0 -109
- package/docs/getAutoIncrementId.md +0 -93
- package/docs/getCompositeKey.md +0 -39
- package/docs/getValueStableKey.md +0 -57
- package/docs/hash.md +0 -31
- package/docs/interpolate/-internal-.md +0 -61
- package/docs/interpolate/README.md +0 -62
- package/docs/keepPrevIfUnchanged.md +0 -43
- package/docs/levenshtein.md +0 -93
- package/docs/main.md +0 -21
- package/docs/mathUtils.md +0 -137
- package/docs/modules.md +0 -58
- package/docs/mutationUtils.md +0 -44
- package/docs/objUtils.md +0 -237
- package/docs/parallelAsyncCalls/-internal-.md +0 -347
- package/docs/parallelAsyncCalls/README.md +0 -45
- package/docs/promiseUtils/-internal-.md +0 -69
- package/docs/promiseUtils/README.md +0 -31
- package/docs/retryOnError/-internal-.md +0 -111
- package/docs/retryOnError/README.md +0 -168
- package/docs/runShellCmd/-internal-.md +0 -111
- package/docs/runShellCmd/README.md +0 -201
- package/docs/safeJson.md +0 -51
- package/docs/saferTyping.md +0 -228
- package/docs/serializeXML.md +0 -100
- package/docs/shallowEqual.md +0 -33
- package/docs/sleep.md +0 -27
- package/docs/stringUtils/-internal-.md +0 -17
- package/docs/stringUtils/README.md +0 -270
- package/docs/testUtils.md +0 -382
- package/docs/throttle/-internal-.md +0 -47
- package/docs/throttle/README.md +0 -178
- package/docs/time.md +0 -274
- package/docs/timers.md +0 -256
- package/docs/tsResult/-internal-.md +0 -327
- package/docs/tsResult/README.md +0 -702
- package/docs/typeGuards.md +0 -399
- package/docs/typeUtils/-internal-.md +0 -99
- package/docs/typeUtils/README.md +0 -195
- package/docs/typeUtils.typesTest.md +0 -7
- package/docs/typedStrings.md +0 -458
- package/docs/typingFnUtils/-internal-.md +0 -43
- package/docs/typingFnUtils/README.md +0 -317
- package/docs/typingTestUtils.md +0 -172
- package/docs/typingUtils.md +0 -135
- package/docs/yamlStringify.md +0 -83
package/dist/assertions.d.cts
CHANGED
|
@@ -5,177 +5,188 @@ type StrictNonUndefined<T, N = unknown> = undefined extends T ? NotUndefined<T>
|
|
|
5
5
|
/**
|
|
6
6
|
* Ensures a value is not undefined and returns it with the correct type.
|
|
7
7
|
*
|
|
8
|
-
* Throws an error if the value is undefined. Use it instead of `!` operator for
|
|
8
|
+
* Throws an error if the value is undefined. Use it instead of `!` operator for
|
|
9
|
+
* better type safety.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const maybeString: string | undefined = getValue();
|
|
14
|
+
* const definiteString = notUndefined(maybeString); // Type: string
|
|
15
|
+
*
|
|
16
|
+
* // With custom error message
|
|
17
|
+
* const value = notUndefined(maybeValue, 'Value must be defined');
|
|
18
|
+
*
|
|
19
|
+
* // With custom error function
|
|
20
|
+
* const value = notUndefined(maybeValue, () => new ValidationError('Required field'));
|
|
21
|
+
* ```;
|
|
9
22
|
*
|
|
10
23
|
* @template T - The type of the input value
|
|
11
24
|
* @param value - The value to check for undefined
|
|
12
|
-
* @param error - Error message string or function that returns an Error to
|
|
25
|
+
* @param error - Error message string or function that returns an Error to
|
|
26
|
+
* throw if value is undefined
|
|
13
27
|
* @returns The input value with undefined excluded from its type
|
|
14
28
|
* @throws {Error} When the value is undefined
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* const maybeString: string | undefined = getValue();
|
|
19
|
-
* const definiteString = notUndefined(maybeString); // Type: string
|
|
20
|
-
*
|
|
21
|
-
* // With custom error message
|
|
22
|
-
* const value = notUndefined(maybeValue, 'Value must be defined');
|
|
23
|
-
*
|
|
24
|
-
* // With custom error function
|
|
25
|
-
* const value = notUndefined(maybeValue, () => new ValidationError('Required field'));
|
|
26
|
-
* ```
|
|
27
29
|
*/
|
|
28
30
|
declare function notUndefined<T>(value: T, error?: string | (() => Error)): StrictNonUndefined<T>;
|
|
29
31
|
type StrictNonNullable<T, N = unknown> = undefined extends T ? NonNullable<T> : null extends T ? NonNullable<T> : N;
|
|
30
32
|
/**
|
|
31
|
-
* Ensures a value is not null or undefined and returns it with the correct
|
|
33
|
+
* Ensures a value is not null or undefined and returns it with the correct
|
|
34
|
+
* type.
|
|
35
|
+
*
|
|
36
|
+
* Throws an error if the value is null or undefined. Use it instead of `!`
|
|
37
|
+
* operator for better type safety.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const maybeString: string | null | undefined = getValue();
|
|
42
|
+
* const definiteString = notNullish(maybeString); // Type: string
|
|
43
|
+
*
|
|
44
|
+
* // With custom error message
|
|
45
|
+
* const value = notNullish(maybeValue, 'Value cannot be null or undefined');
|
|
32
46
|
*
|
|
33
|
-
*
|
|
47
|
+
* // With custom error function
|
|
48
|
+
* const value = notNullish(maybeValue, () => new ValidationError('Required field'));
|
|
49
|
+
* ```;
|
|
34
50
|
*
|
|
35
51
|
* @template T - The type of the input value
|
|
36
52
|
* @param value - The value to check for null or undefined
|
|
37
|
-
* @param error - Error message string or function that returns an Error to
|
|
53
|
+
* @param error - Error message string or function that returns an Error to
|
|
54
|
+
* throw if value is nullish
|
|
38
55
|
* @returns The input value with null and undefined excluded from its type
|
|
39
56
|
* @throws {Error} When the value is null or undefined
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ```typescript
|
|
43
|
-
* const maybeString: string | null | undefined = getValue();
|
|
44
|
-
* const definiteString = notNullish(maybeString); // Type: string
|
|
45
|
-
*
|
|
46
|
-
* // With custom error message
|
|
47
|
-
* const value = notNullish(maybeValue, 'Value cannot be null or undefined');
|
|
48
|
-
*
|
|
49
|
-
* // With custom error function
|
|
50
|
-
* const value = notNullish(maybeValue, () => new ValidationError('Required field'));
|
|
51
|
-
* ```
|
|
52
57
|
*/
|
|
53
58
|
declare function notNullish<T>(value: T, error?: string | (() => Error)): StrictNonNullable<T>;
|
|
54
59
|
/**
|
|
55
|
-
* Asserts that a value is not null or undefined using TypeScript's assertion
|
|
60
|
+
* Asserts that a value is not null or undefined using TypeScript's assertion
|
|
61
|
+
* signature.
|
|
56
62
|
*
|
|
57
|
-
* Throws an error if the value is null or undefined. Use it instead of `!`
|
|
63
|
+
* Throws an error if the value is null or undefined. Use it instead of `!`
|
|
64
|
+
* operator for better type safety.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* function processValue(input: string | null | undefined) {
|
|
69
|
+
* assertIsNotNullish(input);
|
|
70
|
+
* // TypeScript now knows input is string
|
|
71
|
+
* console.log(input.toUpperCase());
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* // With custom error
|
|
75
|
+
* assertIsNotNullish(value, 'Value is required for processing');
|
|
76
|
+
* ```;
|
|
58
77
|
*
|
|
59
78
|
* @template T - The type of the input value
|
|
60
79
|
* @param value - The value to assert is not null or undefined
|
|
61
|
-
* @param error - Error message string or function that returns an Error to
|
|
80
|
+
* @param error - Error message string or function that returns an Error to
|
|
81
|
+
* throw if value is nullish
|
|
62
82
|
* @throws {Error} When the value is null or undefined
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* ```typescript
|
|
66
|
-
* function processValue(input: string | null | undefined) {
|
|
67
|
-
* assertIsNotNullish(input);
|
|
68
|
-
* // TypeScript now knows input is string
|
|
69
|
-
* console.log(input.toUpperCase());
|
|
70
|
-
* }
|
|
71
|
-
*
|
|
72
|
-
* // With custom error
|
|
73
|
-
* assertIsNotNullish(value, 'Value is required for processing');
|
|
74
|
-
* ```
|
|
75
83
|
*/
|
|
76
84
|
declare function assertIsNotNullish<T>(value: T, error?: string | (() => Error)): asserts value is StrictNonNullable<T, never>;
|
|
77
85
|
/**
|
|
78
86
|
* Asserts that a value is not undefined using TypeScript's assertion signature.
|
|
79
87
|
*
|
|
80
|
-
* Throws an error if the value is undefined. Use it instead of `!` operator for
|
|
88
|
+
* Throws an error if the value is undefined. Use it instead of `!` operator for
|
|
89
|
+
* better type safety.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* function processValue(input: string | undefined) {
|
|
94
|
+
* assertIsNotUndefined(input);
|
|
95
|
+
* // TypeScript now knows input is string
|
|
96
|
+
* console.log(input.toUpperCase());
|
|
97
|
+
* }
|
|
98
|
+
*
|
|
99
|
+
* // With custom error
|
|
100
|
+
* assertIsNotUndefined(value, 'Value must be defined');
|
|
101
|
+
* ```;
|
|
81
102
|
*
|
|
82
103
|
* @template T - The type of the input value
|
|
83
104
|
* @param value - The value to assert is not undefined
|
|
84
|
-
* @param error - Error message string or function that returns an Error to
|
|
105
|
+
* @param error - Error message string or function that returns an Error to
|
|
106
|
+
* throw if value is undefined
|
|
85
107
|
* @throws {Error} When the value is undefined
|
|
86
|
-
*
|
|
87
|
-
* @example
|
|
88
|
-
* ```typescript
|
|
89
|
-
* function processValue(input: string | undefined) {
|
|
90
|
-
* assertIsNotUndefined(input);
|
|
91
|
-
* // TypeScript now knows input is string
|
|
92
|
-
* console.log(input.toUpperCase());
|
|
93
|
-
* }
|
|
94
|
-
*
|
|
95
|
-
* // With custom error
|
|
96
|
-
* assertIsNotUndefined(value, 'Value must be defined');
|
|
97
|
-
* ```
|
|
98
108
|
*/
|
|
99
109
|
declare function assertIsNotUndefined<T>(value: T, error?: string | (() => Error)): asserts value is StrictNonUndefined<T, never>;
|
|
100
110
|
/**
|
|
101
111
|
* Asserts that a condition is always true, throwing an error if it's falsy.
|
|
102
112
|
*
|
|
103
|
-
* This function is useful for enforcing invariants in your code - conditions
|
|
104
|
-
* should always be true. It uses TypeScript's assertion signature to
|
|
105
|
-
* based on the condition.
|
|
113
|
+
* This function is useful for enforcing invariants in your code - conditions
|
|
114
|
+
* that should always be true. It uses TypeScript's assertion signature to
|
|
115
|
+
* narrow types based on the condition.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* function divide(a: number, b: number) {
|
|
120
|
+
* invariant(b !== 0, 'Division by zero is not allowed');
|
|
121
|
+
* return a / b;
|
|
122
|
+
* }
|
|
123
|
+
*
|
|
124
|
+
* // Type narrowing example
|
|
125
|
+
* function processUser(user: User | null) {
|
|
126
|
+
* invariant(user, 'User must be logged in');
|
|
127
|
+
* // TypeScript now knows user is User, not null
|
|
128
|
+
* console.log(user.name);
|
|
129
|
+
* }
|
|
130
|
+
*
|
|
131
|
+
* // With custom error function
|
|
132
|
+
* invariant(isValid, () => new ValidationError('Invalid state detected'));
|
|
133
|
+
* ```;
|
|
106
134
|
*
|
|
107
135
|
* @param condition - The condition to check (any truthy/falsy value)
|
|
108
|
-
* @param error - Error message string or function that returns an Error to
|
|
136
|
+
* @param error - Error message string or function that returns an Error to
|
|
137
|
+
* throw if condition is falsy
|
|
109
138
|
* @throws {Error} When the condition is falsy
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* ```typescript
|
|
113
|
-
* function divide(a: number, b: number) {
|
|
114
|
-
* invariant(b !== 0, 'Division by zero is not allowed');
|
|
115
|
-
* return a / b;
|
|
116
|
-
* }
|
|
117
|
-
*
|
|
118
|
-
* // Type narrowing example
|
|
119
|
-
* function processUser(user: User | null) {
|
|
120
|
-
* invariant(user, 'User must be logged in');
|
|
121
|
-
* // TypeScript now knows user is User, not null
|
|
122
|
-
* console.log(user.name);
|
|
123
|
-
* }
|
|
124
|
-
*
|
|
125
|
-
* // With custom error function
|
|
126
|
-
* invariant(isValid, () => new ValidationError('Invalid state detected'));
|
|
127
|
-
* ```
|
|
128
139
|
*/
|
|
129
140
|
declare function invariant(condition: any, error?: string | (() => Error)): asserts condition;
|
|
130
141
|
/**
|
|
131
142
|
* Ensures exhaustive type checking in switch statements or conditional logic.
|
|
132
143
|
*
|
|
133
|
-
* This function should be used in the default case of switch statements or
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
* @template Except - The type that should never be reached
|
|
139
|
-
* @param narrowedType - The value that should never exist at runtime
|
|
140
|
-
* @returns An Error object (this function should never actually execute)
|
|
144
|
+
* This function should be used in the default case of switch statements or the
|
|
145
|
+
* final else branch of conditional logic to ensure all possible cases are
|
|
146
|
+
* handled. It helps catch missing cases at compile time when new union members
|
|
147
|
+
* are added.
|
|
141
148
|
*
|
|
142
149
|
* @example
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
150
|
+
* ```typescript
|
|
151
|
+
* type Status = 'pending' | 'success' | 'error';
|
|
152
|
+
*
|
|
153
|
+
* function handleStatus(status: Status) {
|
|
154
|
+
* switch (status) {
|
|
155
|
+
* case 'pending':
|
|
156
|
+
* return 'Loading...';
|
|
157
|
+
* case 'success':
|
|
158
|
+
* return 'Done!';
|
|
159
|
+
* case 'error':
|
|
160
|
+
* return 'Failed!';
|
|
161
|
+
* default:
|
|
162
|
+
* throw exhaustiveCheck(status); // TypeScript error if Status gains new members
|
|
163
|
+
* }
|
|
156
164
|
* }
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
165
|
+
*
|
|
166
|
+
* // In conditional logic
|
|
167
|
+
* function processValue(value: string | number) {
|
|
168
|
+
* if (typeof value === 'string') {
|
|
169
|
+
* return value.toUpperCase();
|
|
170
|
+
* } else if (typeof value === 'number') {
|
|
171
|
+
* return value.toString();
|
|
172
|
+
* } else {
|
|
173
|
+
* throw exhaustiveCheck(value); // Ensures all cases are covered
|
|
174
|
+
* }
|
|
167
175
|
* }
|
|
168
|
-
*
|
|
169
|
-
*
|
|
176
|
+
* ```;
|
|
177
|
+
*
|
|
178
|
+
* @template Except - The type that should never be reached
|
|
179
|
+
* @param narrowedType - The value that should never exist at runtime
|
|
180
|
+
* @returns An Error object (this function should never actually execute)
|
|
170
181
|
*/
|
|
171
182
|
declare function exhaustiveCheck<Except = never>(narrowedType: NoInfer<Except>): Error;
|
|
172
|
-
/** @deprecated
|
|
183
|
+
/** @deprecated Use import from `@ls-stack/typeGuards` instead */
|
|
173
184
|
declare const isFunction: typeof isFunction$1;
|
|
174
|
-
/** @deprecated
|
|
185
|
+
/** @deprecated Use import from `@ls-stack/typeGuards` instead */
|
|
175
186
|
declare const isObject: typeof isObject$1;
|
|
176
|
-
/** @deprecated
|
|
187
|
+
/** @deprecated Use import from `@ls-stack/typeGuards` instead */
|
|
177
188
|
declare const isPlainObject: typeof isPlainObject$1;
|
|
178
|
-
/** @deprecated
|
|
189
|
+
/** @deprecated Use import from `@ls-stack/typeGuards` instead */
|
|
179
190
|
declare const isPromise: typeof isPromise$1;
|
|
180
191
|
|
|
181
192
|
export { assertIsNotNullish, assertIsNotUndefined, exhaustiveCheck, invariant, isFunction, isObject, isPlainObject, isPromise, notNullish, notUndefined };
|
package/dist/assertions.d.ts
CHANGED
|
@@ -5,177 +5,188 @@ type StrictNonUndefined<T, N = unknown> = undefined extends T ? NotUndefined<T>
|
|
|
5
5
|
/**
|
|
6
6
|
* Ensures a value is not undefined and returns it with the correct type.
|
|
7
7
|
*
|
|
8
|
-
* Throws an error if the value is undefined. Use it instead of `!` operator for
|
|
8
|
+
* Throws an error if the value is undefined. Use it instead of `!` operator for
|
|
9
|
+
* better type safety.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const maybeString: string | undefined = getValue();
|
|
14
|
+
* const definiteString = notUndefined(maybeString); // Type: string
|
|
15
|
+
*
|
|
16
|
+
* // With custom error message
|
|
17
|
+
* const value = notUndefined(maybeValue, 'Value must be defined');
|
|
18
|
+
*
|
|
19
|
+
* // With custom error function
|
|
20
|
+
* const value = notUndefined(maybeValue, () => new ValidationError('Required field'));
|
|
21
|
+
* ```;
|
|
9
22
|
*
|
|
10
23
|
* @template T - The type of the input value
|
|
11
24
|
* @param value - The value to check for undefined
|
|
12
|
-
* @param error - Error message string or function that returns an Error to
|
|
25
|
+
* @param error - Error message string or function that returns an Error to
|
|
26
|
+
* throw if value is undefined
|
|
13
27
|
* @returns The input value with undefined excluded from its type
|
|
14
28
|
* @throws {Error} When the value is undefined
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* const maybeString: string | undefined = getValue();
|
|
19
|
-
* const definiteString = notUndefined(maybeString); // Type: string
|
|
20
|
-
*
|
|
21
|
-
* // With custom error message
|
|
22
|
-
* const value = notUndefined(maybeValue, 'Value must be defined');
|
|
23
|
-
*
|
|
24
|
-
* // With custom error function
|
|
25
|
-
* const value = notUndefined(maybeValue, () => new ValidationError('Required field'));
|
|
26
|
-
* ```
|
|
27
29
|
*/
|
|
28
30
|
declare function notUndefined<T>(value: T, error?: string | (() => Error)): StrictNonUndefined<T>;
|
|
29
31
|
type StrictNonNullable<T, N = unknown> = undefined extends T ? NonNullable<T> : null extends T ? NonNullable<T> : N;
|
|
30
32
|
/**
|
|
31
|
-
* Ensures a value is not null or undefined and returns it with the correct
|
|
33
|
+
* Ensures a value is not null or undefined and returns it with the correct
|
|
34
|
+
* type.
|
|
35
|
+
*
|
|
36
|
+
* Throws an error if the value is null or undefined. Use it instead of `!`
|
|
37
|
+
* operator for better type safety.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const maybeString: string | null | undefined = getValue();
|
|
42
|
+
* const definiteString = notNullish(maybeString); // Type: string
|
|
43
|
+
*
|
|
44
|
+
* // With custom error message
|
|
45
|
+
* const value = notNullish(maybeValue, 'Value cannot be null or undefined');
|
|
32
46
|
*
|
|
33
|
-
*
|
|
47
|
+
* // With custom error function
|
|
48
|
+
* const value = notNullish(maybeValue, () => new ValidationError('Required field'));
|
|
49
|
+
* ```;
|
|
34
50
|
*
|
|
35
51
|
* @template T - The type of the input value
|
|
36
52
|
* @param value - The value to check for null or undefined
|
|
37
|
-
* @param error - Error message string or function that returns an Error to
|
|
53
|
+
* @param error - Error message string or function that returns an Error to
|
|
54
|
+
* throw if value is nullish
|
|
38
55
|
* @returns The input value with null and undefined excluded from its type
|
|
39
56
|
* @throws {Error} When the value is null or undefined
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ```typescript
|
|
43
|
-
* const maybeString: string | null | undefined = getValue();
|
|
44
|
-
* const definiteString = notNullish(maybeString); // Type: string
|
|
45
|
-
*
|
|
46
|
-
* // With custom error message
|
|
47
|
-
* const value = notNullish(maybeValue, 'Value cannot be null or undefined');
|
|
48
|
-
*
|
|
49
|
-
* // With custom error function
|
|
50
|
-
* const value = notNullish(maybeValue, () => new ValidationError('Required field'));
|
|
51
|
-
* ```
|
|
52
57
|
*/
|
|
53
58
|
declare function notNullish<T>(value: T, error?: string | (() => Error)): StrictNonNullable<T>;
|
|
54
59
|
/**
|
|
55
|
-
* Asserts that a value is not null or undefined using TypeScript's assertion
|
|
60
|
+
* Asserts that a value is not null or undefined using TypeScript's assertion
|
|
61
|
+
* signature.
|
|
56
62
|
*
|
|
57
|
-
* Throws an error if the value is null or undefined. Use it instead of `!`
|
|
63
|
+
* Throws an error if the value is null or undefined. Use it instead of `!`
|
|
64
|
+
* operator for better type safety.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* function processValue(input: string | null | undefined) {
|
|
69
|
+
* assertIsNotNullish(input);
|
|
70
|
+
* // TypeScript now knows input is string
|
|
71
|
+
* console.log(input.toUpperCase());
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* // With custom error
|
|
75
|
+
* assertIsNotNullish(value, 'Value is required for processing');
|
|
76
|
+
* ```;
|
|
58
77
|
*
|
|
59
78
|
* @template T - The type of the input value
|
|
60
79
|
* @param value - The value to assert is not null or undefined
|
|
61
|
-
* @param error - Error message string or function that returns an Error to
|
|
80
|
+
* @param error - Error message string or function that returns an Error to
|
|
81
|
+
* throw if value is nullish
|
|
62
82
|
* @throws {Error} When the value is null or undefined
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* ```typescript
|
|
66
|
-
* function processValue(input: string | null | undefined) {
|
|
67
|
-
* assertIsNotNullish(input);
|
|
68
|
-
* // TypeScript now knows input is string
|
|
69
|
-
* console.log(input.toUpperCase());
|
|
70
|
-
* }
|
|
71
|
-
*
|
|
72
|
-
* // With custom error
|
|
73
|
-
* assertIsNotNullish(value, 'Value is required for processing');
|
|
74
|
-
* ```
|
|
75
83
|
*/
|
|
76
84
|
declare function assertIsNotNullish<T>(value: T, error?: string | (() => Error)): asserts value is StrictNonNullable<T, never>;
|
|
77
85
|
/**
|
|
78
86
|
* Asserts that a value is not undefined using TypeScript's assertion signature.
|
|
79
87
|
*
|
|
80
|
-
* Throws an error if the value is undefined. Use it instead of `!` operator for
|
|
88
|
+
* Throws an error if the value is undefined. Use it instead of `!` operator for
|
|
89
|
+
* better type safety.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* function processValue(input: string | undefined) {
|
|
94
|
+
* assertIsNotUndefined(input);
|
|
95
|
+
* // TypeScript now knows input is string
|
|
96
|
+
* console.log(input.toUpperCase());
|
|
97
|
+
* }
|
|
98
|
+
*
|
|
99
|
+
* // With custom error
|
|
100
|
+
* assertIsNotUndefined(value, 'Value must be defined');
|
|
101
|
+
* ```;
|
|
81
102
|
*
|
|
82
103
|
* @template T - The type of the input value
|
|
83
104
|
* @param value - The value to assert is not undefined
|
|
84
|
-
* @param error - Error message string or function that returns an Error to
|
|
105
|
+
* @param error - Error message string or function that returns an Error to
|
|
106
|
+
* throw if value is undefined
|
|
85
107
|
* @throws {Error} When the value is undefined
|
|
86
|
-
*
|
|
87
|
-
* @example
|
|
88
|
-
* ```typescript
|
|
89
|
-
* function processValue(input: string | undefined) {
|
|
90
|
-
* assertIsNotUndefined(input);
|
|
91
|
-
* // TypeScript now knows input is string
|
|
92
|
-
* console.log(input.toUpperCase());
|
|
93
|
-
* }
|
|
94
|
-
*
|
|
95
|
-
* // With custom error
|
|
96
|
-
* assertIsNotUndefined(value, 'Value must be defined');
|
|
97
|
-
* ```
|
|
98
108
|
*/
|
|
99
109
|
declare function assertIsNotUndefined<T>(value: T, error?: string | (() => Error)): asserts value is StrictNonUndefined<T, never>;
|
|
100
110
|
/**
|
|
101
111
|
* Asserts that a condition is always true, throwing an error if it's falsy.
|
|
102
112
|
*
|
|
103
|
-
* This function is useful for enforcing invariants in your code - conditions
|
|
104
|
-
* should always be true. It uses TypeScript's assertion signature to
|
|
105
|
-
* based on the condition.
|
|
113
|
+
* This function is useful for enforcing invariants in your code - conditions
|
|
114
|
+
* that should always be true. It uses TypeScript's assertion signature to
|
|
115
|
+
* narrow types based on the condition.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* function divide(a: number, b: number) {
|
|
120
|
+
* invariant(b !== 0, 'Division by zero is not allowed');
|
|
121
|
+
* return a / b;
|
|
122
|
+
* }
|
|
123
|
+
*
|
|
124
|
+
* // Type narrowing example
|
|
125
|
+
* function processUser(user: User | null) {
|
|
126
|
+
* invariant(user, 'User must be logged in');
|
|
127
|
+
* // TypeScript now knows user is User, not null
|
|
128
|
+
* console.log(user.name);
|
|
129
|
+
* }
|
|
130
|
+
*
|
|
131
|
+
* // With custom error function
|
|
132
|
+
* invariant(isValid, () => new ValidationError('Invalid state detected'));
|
|
133
|
+
* ```;
|
|
106
134
|
*
|
|
107
135
|
* @param condition - The condition to check (any truthy/falsy value)
|
|
108
|
-
* @param error - Error message string or function that returns an Error to
|
|
136
|
+
* @param error - Error message string or function that returns an Error to
|
|
137
|
+
* throw if condition is falsy
|
|
109
138
|
* @throws {Error} When the condition is falsy
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* ```typescript
|
|
113
|
-
* function divide(a: number, b: number) {
|
|
114
|
-
* invariant(b !== 0, 'Division by zero is not allowed');
|
|
115
|
-
* return a / b;
|
|
116
|
-
* }
|
|
117
|
-
*
|
|
118
|
-
* // Type narrowing example
|
|
119
|
-
* function processUser(user: User | null) {
|
|
120
|
-
* invariant(user, 'User must be logged in');
|
|
121
|
-
* // TypeScript now knows user is User, not null
|
|
122
|
-
* console.log(user.name);
|
|
123
|
-
* }
|
|
124
|
-
*
|
|
125
|
-
* // With custom error function
|
|
126
|
-
* invariant(isValid, () => new ValidationError('Invalid state detected'));
|
|
127
|
-
* ```
|
|
128
139
|
*/
|
|
129
140
|
declare function invariant(condition: any, error?: string | (() => Error)): asserts condition;
|
|
130
141
|
/**
|
|
131
142
|
* Ensures exhaustive type checking in switch statements or conditional logic.
|
|
132
143
|
*
|
|
133
|
-
* This function should be used in the default case of switch statements or
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
* @template Except - The type that should never be reached
|
|
139
|
-
* @param narrowedType - The value that should never exist at runtime
|
|
140
|
-
* @returns An Error object (this function should never actually execute)
|
|
144
|
+
* This function should be used in the default case of switch statements or the
|
|
145
|
+
* final else branch of conditional logic to ensure all possible cases are
|
|
146
|
+
* handled. It helps catch missing cases at compile time when new union members
|
|
147
|
+
* are added.
|
|
141
148
|
*
|
|
142
149
|
* @example
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
150
|
+
* ```typescript
|
|
151
|
+
* type Status = 'pending' | 'success' | 'error';
|
|
152
|
+
*
|
|
153
|
+
* function handleStatus(status: Status) {
|
|
154
|
+
* switch (status) {
|
|
155
|
+
* case 'pending':
|
|
156
|
+
* return 'Loading...';
|
|
157
|
+
* case 'success':
|
|
158
|
+
* return 'Done!';
|
|
159
|
+
* case 'error':
|
|
160
|
+
* return 'Failed!';
|
|
161
|
+
* default:
|
|
162
|
+
* throw exhaustiveCheck(status); // TypeScript error if Status gains new members
|
|
163
|
+
* }
|
|
156
164
|
* }
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
165
|
+
*
|
|
166
|
+
* // In conditional logic
|
|
167
|
+
* function processValue(value: string | number) {
|
|
168
|
+
* if (typeof value === 'string') {
|
|
169
|
+
* return value.toUpperCase();
|
|
170
|
+
* } else if (typeof value === 'number') {
|
|
171
|
+
* return value.toString();
|
|
172
|
+
* } else {
|
|
173
|
+
* throw exhaustiveCheck(value); // Ensures all cases are covered
|
|
174
|
+
* }
|
|
167
175
|
* }
|
|
168
|
-
*
|
|
169
|
-
*
|
|
176
|
+
* ```;
|
|
177
|
+
*
|
|
178
|
+
* @template Except - The type that should never be reached
|
|
179
|
+
* @param narrowedType - The value that should never exist at runtime
|
|
180
|
+
* @returns An Error object (this function should never actually execute)
|
|
170
181
|
*/
|
|
171
182
|
declare function exhaustiveCheck<Except = never>(narrowedType: NoInfer<Except>): Error;
|
|
172
|
-
/** @deprecated
|
|
183
|
+
/** @deprecated Use import from `@ls-stack/typeGuards` instead */
|
|
173
184
|
declare const isFunction: typeof isFunction$1;
|
|
174
|
-
/** @deprecated
|
|
185
|
+
/** @deprecated Use import from `@ls-stack/typeGuards` instead */
|
|
175
186
|
declare const isObject: typeof isObject$1;
|
|
176
|
-
/** @deprecated
|
|
187
|
+
/** @deprecated Use import from `@ls-stack/typeGuards` instead */
|
|
177
188
|
declare const isPlainObject: typeof isPlainObject$1;
|
|
178
|
-
/** @deprecated
|
|
189
|
+
/** @deprecated Use import from `@ls-stack/typeGuards` instead */
|
|
179
190
|
declare const isPromise: typeof isPromise$1;
|
|
180
191
|
|
|
181
192
|
export { assertIsNotNullish, assertIsNotUndefined, exhaustiveCheck, invariant, isFunction, isObject, isPlainObject, isPromise, notNullish, notUndefined };
|