@ls-stack/utils 3.13.0 → 3.15.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/lib/arrayUtils.cjs +5 -2
- package/lib/arrayUtils.js +3 -2
- package/lib/assertions.cjs +42 -34
- package/lib/assertions.d.cts +175 -11
- package/lib/assertions.d.ts +175 -11
- package/lib/assertions.js +2 -1
- package/lib/cache.cjs +7 -4
- package/lib/cache.js +2 -1
- package/lib/{chunk-UTFE4P3P.js → chunk-DMW5Q4T2.js} +1 -1
- package/lib/{chunk-OHHF4CJZ.js → chunk-GKOTKAIV.js} +1 -1
- package/lib/{chunk-U44EKR2F.js → chunk-NH2LCAQS.js} +1 -1
- package/lib/chunk-SSKW673U.js +36 -0
- package/lib/chunk-WS4WEVHU.js +57 -0
- package/lib/concurrentCalls.cjs +20 -14
- package/lib/concurrentCalls.d.cts +2 -0
- package/lib/concurrentCalls.d.ts +2 -0
- package/lib/concurrentCalls.js +5 -2
- package/lib/createThrottleController.cjs +5 -2
- package/lib/createThrottleController.js +3 -2
- package/lib/enhancedMap.cjs +5 -2
- package/lib/enhancedMap.js +3 -2
- package/lib/getCompositeKey.cjs +5 -2
- package/lib/getCompositeKey.js +3 -2
- package/lib/getValueStableKey.cjs +5 -2
- package/lib/getValueStableKey.js +3 -2
- package/lib/interpolate.cjs +2 -2
- package/lib/interpolate.js +2 -1
- package/lib/parallelAsyncCalls.cjs +10 -7
- package/lib/parallelAsyncCalls.js +2 -1
- package/lib/serializeXML.js +3 -2
- package/lib/testUtils.cjs +5 -2
- package/lib/testUtils.js +3 -2
- package/lib/tsResult.cjs +9 -4
- package/lib/tsResult.js +2 -1
- package/lib/typeGuards.cjs +65 -0
- package/lib/typeGuards.d.cts +109 -0
- package/lib/typeGuards.d.ts +109 -0
- package/lib/typeGuards.js +16 -0
- package/lib/typingFnUtils.cjs +10 -3
- package/lib/typingFnUtils.d.cts +25 -2
- package/lib/typingFnUtils.d.ts +25 -2
- package/lib/typingFnUtils.js +7 -2
- package/lib/yamlStringify.cjs +17 -9
- package/lib/yamlStringify.js +7 -2
- package/package.json +5 -1
- package/lib/chunk-3XCS7FVO.js +0 -66
package/lib/arrayUtils.cjs
CHANGED
|
@@ -34,11 +34,14 @@ __export(arrayUtils_exports, {
|
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(arrayUtils_exports);
|
|
36
36
|
|
|
37
|
-
// src/
|
|
37
|
+
// src/typeGuards.ts
|
|
38
38
|
function isFunction(value) {
|
|
39
39
|
return typeof value === "function";
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
// src/assertions.ts
|
|
43
|
+
var isFunction2 = isFunction;
|
|
44
|
+
|
|
42
45
|
// src/arrayUtils.ts
|
|
43
46
|
function filterAndMap(array, mapFilter) {
|
|
44
47
|
const result = [];
|
|
@@ -145,7 +148,7 @@ function truncateArray(array, maxLength, appendIfTruncated) {
|
|
|
145
148
|
const truncate = array.length > maxLength;
|
|
146
149
|
const result = truncate ? [...array.slice(0, maxLength)] : array;
|
|
147
150
|
if (truncate && appendIfTruncated) {
|
|
148
|
-
if (
|
|
151
|
+
if (isFunction2(appendIfTruncated)) {
|
|
149
152
|
return [...result, appendIfTruncated(array.length - maxLength)];
|
|
150
153
|
}
|
|
151
154
|
return [...result, appendIfTruncated];
|
package/lib/arrayUtils.js
CHANGED
|
@@ -10,8 +10,9 @@ import {
|
|
|
10
10
|
rejectDuplicates,
|
|
11
11
|
sortBy,
|
|
12
12
|
truncateArray
|
|
13
|
-
} from "./chunk-
|
|
14
|
-
import "./chunk-
|
|
13
|
+
} from "./chunk-DMW5Q4T2.js";
|
|
14
|
+
import "./chunk-WS4WEVHU.js";
|
|
15
|
+
import "./chunk-SSKW673U.js";
|
|
15
16
|
export {
|
|
16
17
|
arrayWithPrev,
|
|
17
18
|
arrayWithPrevAndIndex,
|
package/lib/assertions.cjs
CHANGED
|
@@ -24,66 +24,74 @@ __export(assertions_exports, {
|
|
|
24
24
|
assertIsNotUndefined: () => assertIsNotUndefined,
|
|
25
25
|
exhaustiveCheck: () => exhaustiveCheck,
|
|
26
26
|
invariant: () => invariant,
|
|
27
|
-
isFunction: () =>
|
|
28
|
-
isObject: () =>
|
|
29
|
-
isPlainObject: () =>
|
|
30
|
-
isPromise: () =>
|
|
27
|
+
isFunction: () => isFunction2,
|
|
28
|
+
isObject: () => isObject2,
|
|
29
|
+
isPlainObject: () => isPlainObject2,
|
|
30
|
+
isPromise: () => isPromise2,
|
|
31
31
|
notNullish: () => notNullish,
|
|
32
32
|
notUndefined: () => notUndefined
|
|
33
33
|
});
|
|
34
34
|
module.exports = __toCommonJS(assertions_exports);
|
|
35
|
+
|
|
36
|
+
// src/typeGuards.ts
|
|
37
|
+
function isObject(value) {
|
|
38
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
39
|
+
}
|
|
40
|
+
function isFunction(value) {
|
|
41
|
+
return typeof value === "function";
|
|
42
|
+
}
|
|
43
|
+
function isPromise(value) {
|
|
44
|
+
return isObject(value) && "then" in value && isFunction(value.then);
|
|
45
|
+
}
|
|
46
|
+
function isPlainObject(value) {
|
|
47
|
+
if (!value || typeof value !== "object") return false;
|
|
48
|
+
const proto = Object.getPrototypeOf(value);
|
|
49
|
+
if (proto === null) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
const Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
|
|
53
|
+
if (Ctor === Object) return true;
|
|
54
|
+
const objectCtorString = Object.prototype.constructor.toString();
|
|
55
|
+
return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/assertions.ts
|
|
35
59
|
var undefErrMsg = "not undefined assertion failed";
|
|
36
60
|
var nullishErrMsg = "not nullish assertion failed";
|
|
37
|
-
function notUndefined(value,
|
|
61
|
+
function notUndefined(value, error = undefErrMsg) {
|
|
38
62
|
if (value === void 0) {
|
|
39
|
-
throw new Error(
|
|
63
|
+
throw typeof error === "function" ? error() : new Error(error);
|
|
40
64
|
}
|
|
41
65
|
return value;
|
|
42
66
|
}
|
|
43
|
-
function notNullish(value,
|
|
67
|
+
function notNullish(value, error = nullishErrMsg) {
|
|
44
68
|
if (value === void 0 || value === null) {
|
|
45
|
-
throw new Error(
|
|
69
|
+
throw typeof error === "function" ? error() : new Error(error);
|
|
46
70
|
}
|
|
47
71
|
return value;
|
|
48
72
|
}
|
|
49
|
-
function assertIsNotNullish(value,
|
|
73
|
+
function assertIsNotNullish(value, error = nullishErrMsg) {
|
|
50
74
|
if (value === void 0 || value === null) {
|
|
51
|
-
throw new Error(
|
|
75
|
+
throw typeof error === "function" ? error() : new Error(error);
|
|
52
76
|
}
|
|
53
77
|
}
|
|
54
|
-
function assertIsNotUndefined(value,
|
|
78
|
+
function assertIsNotUndefined(value, error = undefErrMsg) {
|
|
55
79
|
if (value === void 0) {
|
|
56
|
-
throw new Error(
|
|
80
|
+
throw typeof error === "function" ? error() : new Error(error);
|
|
57
81
|
}
|
|
58
82
|
}
|
|
59
|
-
function invariant(condition,
|
|
83
|
+
function invariant(condition, error = "Invariant violation") {
|
|
60
84
|
if (!condition) {
|
|
61
|
-
throw new Error(`Invariant violation: ${
|
|
85
|
+
throw typeof error === "function" ? error() : new Error(`Invariant violation: ${error}`);
|
|
62
86
|
}
|
|
63
87
|
}
|
|
64
88
|
function exhaustiveCheck(narrowedType) {
|
|
65
89
|
return new Error("This should never happen");
|
|
66
90
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return typeof value === "function";
|
|
72
|
-
}
|
|
73
|
-
function isPromise(value) {
|
|
74
|
-
return isObject(value) && "then" in value && isFunction(value.then);
|
|
75
|
-
}
|
|
76
|
-
function isPlainObject(value) {
|
|
77
|
-
if (!value || typeof value !== "object") return false;
|
|
78
|
-
const proto = Object.getPrototypeOf(value);
|
|
79
|
-
if (proto === null) {
|
|
80
|
-
return true;
|
|
81
|
-
}
|
|
82
|
-
const Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
|
|
83
|
-
if (Ctor === Object) return true;
|
|
84
|
-
const objectCtorString = Object.prototype.constructor.toString();
|
|
85
|
-
return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
|
|
86
|
-
}
|
|
91
|
+
var isFunction2 = isFunction;
|
|
92
|
+
var isObject2 = isObject;
|
|
93
|
+
var isPlainObject2 = isPlainObject;
|
|
94
|
+
var isPromise2 = isPromise;
|
|
87
95
|
// Annotate the CommonJS export names for ESM import in node:
|
|
88
96
|
0 && (module.exports = {
|
|
89
97
|
assertIsNotNullish,
|
package/lib/assertions.d.cts
CHANGED
|
@@ -1,17 +1,181 @@
|
|
|
1
|
+
import { isFunction as isFunction$1, isObject as isObject$1, isPlainObject as isPlainObject$1, isPromise as isPromise$1 } from './typeGuards.cjs';
|
|
2
|
+
|
|
1
3
|
type NotUndefined<T> = T extends undefined ? never : T;
|
|
2
4
|
type StrictNonUndefined<T, N = unknown> = undefined extends T ? NotUndefined<T> : N;
|
|
3
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Ensures a value is not undefined and returns it with the correct type.
|
|
7
|
+
*
|
|
8
|
+
* Throws an error if the value is undefined. Use it instead of `!` operator for better type safety.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of the input value
|
|
11
|
+
* @param value - The value to check for undefined
|
|
12
|
+
* @param error - Error message string or function that returns an Error to throw if value is undefined
|
|
13
|
+
* @returns The input value with undefined excluded from its type
|
|
14
|
+
* @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
|
+
*/
|
|
28
|
+
declare function notUndefined<T>(value: T, error?: string | (() => Error)): StrictNonUndefined<T>;
|
|
4
29
|
type StrictNonNullable<T, N = unknown> = undefined extends T ? NonNullable<T> : null extends T ? NonNullable<T> : N;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Ensures a value is not null or undefined and returns it with the correct type.
|
|
32
|
+
*
|
|
33
|
+
* Throws an error if the value is null or undefined. Use it instead of `!` operator for better type safety.
|
|
34
|
+
*
|
|
35
|
+
* @template T - The type of the input value
|
|
36
|
+
* @param value - The value to check for null or undefined
|
|
37
|
+
* @param error - Error message string or function that returns an Error to throw if value is nullish
|
|
38
|
+
* @returns The input value with null and undefined excluded from its type
|
|
39
|
+
* @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
|
+
*/
|
|
53
|
+
declare function notNullish<T>(value: T, error?: string | (() => Error)): StrictNonNullable<T>;
|
|
54
|
+
/**
|
|
55
|
+
* Asserts that a value is not null or undefined using TypeScript's assertion signature.
|
|
56
|
+
*
|
|
57
|
+
* Throws an error if the value is null or undefined. Use it instead of `!` operator for better type safety.
|
|
58
|
+
*
|
|
59
|
+
* @template T - The type of the input value
|
|
60
|
+
* @param value - The value to assert is not null or undefined
|
|
61
|
+
* @param error - Error message string or function that returns an Error to throw if value is nullish
|
|
62
|
+
* @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
|
+
*/
|
|
76
|
+
declare function assertIsNotNullish<T>(value: T, error?: string | (() => Error)): asserts value is StrictNonNullable<T, never>;
|
|
77
|
+
/**
|
|
78
|
+
* Asserts that a value is not undefined using TypeScript's assertion signature.
|
|
79
|
+
*
|
|
80
|
+
* Throws an error if the value is undefined. Use it instead of `!` operator for better type safety.
|
|
81
|
+
*
|
|
82
|
+
* @template T - The type of the input value
|
|
83
|
+
* @param value - The value to assert is not undefined
|
|
84
|
+
* @param error - Error message string or function that returns an Error to throw if value is undefined
|
|
85
|
+
* @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
|
+
*/
|
|
99
|
+
declare function assertIsNotUndefined<T>(value: T, error?: string | (() => Error)): asserts value is StrictNonUndefined<T, never>;
|
|
100
|
+
/**
|
|
101
|
+
* Asserts that a condition is always true, throwing an error if it's falsy.
|
|
102
|
+
*
|
|
103
|
+
* This function is useful for enforcing invariants in your code - conditions that
|
|
104
|
+
* should always be true. It uses TypeScript's assertion signature to narrow types
|
|
105
|
+
* based on the condition.
|
|
106
|
+
*
|
|
107
|
+
* @param condition - The condition to check (any truthy/falsy value)
|
|
108
|
+
* @param error - Error message string or function that returns an Error to throw if condition is falsy
|
|
109
|
+
* @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
|
+
*/
|
|
129
|
+
declare function invariant(condition: any, error?: string | (() => Error)): asserts condition;
|
|
130
|
+
/**
|
|
131
|
+
* Ensures exhaustive type checking in switch statements or conditional logic.
|
|
132
|
+
*
|
|
133
|
+
* This function should be used in the default case of switch statements or
|
|
134
|
+
* the final else branch of conditional logic to ensure all possible cases
|
|
135
|
+
* are handled. It helps catch missing cases at compile time when new union
|
|
136
|
+
* members are added.
|
|
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)
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* type Status = 'pending' | 'success' | 'error';
|
|
145
|
+
*
|
|
146
|
+
* function handleStatus(status: Status) {
|
|
147
|
+
* switch (status) {
|
|
148
|
+
* case 'pending':
|
|
149
|
+
* return 'Loading...';
|
|
150
|
+
* case 'success':
|
|
151
|
+
* return 'Done!';
|
|
152
|
+
* case 'error':
|
|
153
|
+
* return 'Failed!';
|
|
154
|
+
* default:
|
|
155
|
+
* throw exhaustiveCheck(status); // TypeScript error if Status gains new members
|
|
156
|
+
* }
|
|
157
|
+
* }
|
|
158
|
+
*
|
|
159
|
+
* // In conditional logic
|
|
160
|
+
* function processValue(value: string | number) {
|
|
161
|
+
* if (typeof value === 'string') {
|
|
162
|
+
* return value.toUpperCase();
|
|
163
|
+
* } else if (typeof value === 'number') {
|
|
164
|
+
* return value.toString();
|
|
165
|
+
* } else {
|
|
166
|
+
* throw exhaustiveCheck(value); // Ensures all cases are covered
|
|
167
|
+
* }
|
|
168
|
+
* }
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
11
171
|
declare function exhaustiveCheck<Except = never>(narrowedType: NoInfer<Except>): Error;
|
|
12
|
-
|
|
13
|
-
declare
|
|
14
|
-
|
|
15
|
-
declare
|
|
172
|
+
/** @deprecated use import from `@ls-stack/typeGuards` instead */
|
|
173
|
+
declare const isFunction: typeof isFunction$1;
|
|
174
|
+
/** @deprecated use import from `@ls-stack/typeGuards` instead */
|
|
175
|
+
declare const isObject: typeof isObject$1;
|
|
176
|
+
/** @deprecated use import from `@ls-stack/typeGuards` instead */
|
|
177
|
+
declare const isPlainObject: typeof isPlainObject$1;
|
|
178
|
+
/** @deprecated use import from `@ls-stack/typeGuards` instead */
|
|
179
|
+
declare const isPromise: typeof isPromise$1;
|
|
16
180
|
|
|
17
181
|
export { assertIsNotNullish, assertIsNotUndefined, exhaustiveCheck, invariant, isFunction, isObject, isPlainObject, isPromise, notNullish, notUndefined };
|
package/lib/assertions.d.ts
CHANGED
|
@@ -1,17 +1,181 @@
|
|
|
1
|
+
import { isFunction as isFunction$1, isObject as isObject$1, isPlainObject as isPlainObject$1, isPromise as isPromise$1 } from './typeGuards.js';
|
|
2
|
+
|
|
1
3
|
type NotUndefined<T> = T extends undefined ? never : T;
|
|
2
4
|
type StrictNonUndefined<T, N = unknown> = undefined extends T ? NotUndefined<T> : N;
|
|
3
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Ensures a value is not undefined and returns it with the correct type.
|
|
7
|
+
*
|
|
8
|
+
* Throws an error if the value is undefined. Use it instead of `!` operator for better type safety.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of the input value
|
|
11
|
+
* @param value - The value to check for undefined
|
|
12
|
+
* @param error - Error message string or function that returns an Error to throw if value is undefined
|
|
13
|
+
* @returns The input value with undefined excluded from its type
|
|
14
|
+
* @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
|
+
*/
|
|
28
|
+
declare function notUndefined<T>(value: T, error?: string | (() => Error)): StrictNonUndefined<T>;
|
|
4
29
|
type StrictNonNullable<T, N = unknown> = undefined extends T ? NonNullable<T> : null extends T ? NonNullable<T> : N;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Ensures a value is not null or undefined and returns it with the correct type.
|
|
32
|
+
*
|
|
33
|
+
* Throws an error if the value is null or undefined. Use it instead of `!` operator for better type safety.
|
|
34
|
+
*
|
|
35
|
+
* @template T - The type of the input value
|
|
36
|
+
* @param value - The value to check for null or undefined
|
|
37
|
+
* @param error - Error message string or function that returns an Error to throw if value is nullish
|
|
38
|
+
* @returns The input value with null and undefined excluded from its type
|
|
39
|
+
* @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
|
+
*/
|
|
53
|
+
declare function notNullish<T>(value: T, error?: string | (() => Error)): StrictNonNullable<T>;
|
|
54
|
+
/**
|
|
55
|
+
* Asserts that a value is not null or undefined using TypeScript's assertion signature.
|
|
56
|
+
*
|
|
57
|
+
* Throws an error if the value is null or undefined. Use it instead of `!` operator for better type safety.
|
|
58
|
+
*
|
|
59
|
+
* @template T - The type of the input value
|
|
60
|
+
* @param value - The value to assert is not null or undefined
|
|
61
|
+
* @param error - Error message string or function that returns an Error to throw if value is nullish
|
|
62
|
+
* @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
|
+
*/
|
|
76
|
+
declare function assertIsNotNullish<T>(value: T, error?: string | (() => Error)): asserts value is StrictNonNullable<T, never>;
|
|
77
|
+
/**
|
|
78
|
+
* Asserts that a value is not undefined using TypeScript's assertion signature.
|
|
79
|
+
*
|
|
80
|
+
* Throws an error if the value is undefined. Use it instead of `!` operator for better type safety.
|
|
81
|
+
*
|
|
82
|
+
* @template T - The type of the input value
|
|
83
|
+
* @param value - The value to assert is not undefined
|
|
84
|
+
* @param error - Error message string or function that returns an Error to throw if value is undefined
|
|
85
|
+
* @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
|
+
*/
|
|
99
|
+
declare function assertIsNotUndefined<T>(value: T, error?: string | (() => Error)): asserts value is StrictNonUndefined<T, never>;
|
|
100
|
+
/**
|
|
101
|
+
* Asserts that a condition is always true, throwing an error if it's falsy.
|
|
102
|
+
*
|
|
103
|
+
* This function is useful for enforcing invariants in your code - conditions that
|
|
104
|
+
* should always be true. It uses TypeScript's assertion signature to narrow types
|
|
105
|
+
* based on the condition.
|
|
106
|
+
*
|
|
107
|
+
* @param condition - The condition to check (any truthy/falsy value)
|
|
108
|
+
* @param error - Error message string or function that returns an Error to throw if condition is falsy
|
|
109
|
+
* @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
|
+
*/
|
|
129
|
+
declare function invariant(condition: any, error?: string | (() => Error)): asserts condition;
|
|
130
|
+
/**
|
|
131
|
+
* Ensures exhaustive type checking in switch statements or conditional logic.
|
|
132
|
+
*
|
|
133
|
+
* This function should be used in the default case of switch statements or
|
|
134
|
+
* the final else branch of conditional logic to ensure all possible cases
|
|
135
|
+
* are handled. It helps catch missing cases at compile time when new union
|
|
136
|
+
* members are added.
|
|
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)
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* type Status = 'pending' | 'success' | 'error';
|
|
145
|
+
*
|
|
146
|
+
* function handleStatus(status: Status) {
|
|
147
|
+
* switch (status) {
|
|
148
|
+
* case 'pending':
|
|
149
|
+
* return 'Loading...';
|
|
150
|
+
* case 'success':
|
|
151
|
+
* return 'Done!';
|
|
152
|
+
* case 'error':
|
|
153
|
+
* return 'Failed!';
|
|
154
|
+
* default:
|
|
155
|
+
* throw exhaustiveCheck(status); // TypeScript error if Status gains new members
|
|
156
|
+
* }
|
|
157
|
+
* }
|
|
158
|
+
*
|
|
159
|
+
* // In conditional logic
|
|
160
|
+
* function processValue(value: string | number) {
|
|
161
|
+
* if (typeof value === 'string') {
|
|
162
|
+
* return value.toUpperCase();
|
|
163
|
+
* } else if (typeof value === 'number') {
|
|
164
|
+
* return value.toString();
|
|
165
|
+
* } else {
|
|
166
|
+
* throw exhaustiveCheck(value); // Ensures all cases are covered
|
|
167
|
+
* }
|
|
168
|
+
* }
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
11
171
|
declare function exhaustiveCheck<Except = never>(narrowedType: NoInfer<Except>): Error;
|
|
12
|
-
|
|
13
|
-
declare
|
|
14
|
-
|
|
15
|
-
declare
|
|
172
|
+
/** @deprecated use import from `@ls-stack/typeGuards` instead */
|
|
173
|
+
declare const isFunction: typeof isFunction$1;
|
|
174
|
+
/** @deprecated use import from `@ls-stack/typeGuards` instead */
|
|
175
|
+
declare const isObject: typeof isObject$1;
|
|
176
|
+
/** @deprecated use import from `@ls-stack/typeGuards` instead */
|
|
177
|
+
declare const isPlainObject: typeof isPlainObject$1;
|
|
178
|
+
/** @deprecated use import from `@ls-stack/typeGuards` instead */
|
|
179
|
+
declare const isPromise: typeof isPromise$1;
|
|
16
180
|
|
|
17
181
|
export { assertIsNotNullish, assertIsNotUndefined, exhaustiveCheck, invariant, isFunction, isObject, isPlainObject, isPromise, notNullish, notUndefined };
|
package/lib/assertions.js
CHANGED
package/lib/cache.cjs
CHANGED
|
@@ -27,7 +27,7 @@ __export(cache_exports, {
|
|
|
27
27
|
});
|
|
28
28
|
module.exports = __toCommonJS(cache_exports);
|
|
29
29
|
|
|
30
|
-
// src/
|
|
30
|
+
// src/typeGuards.ts
|
|
31
31
|
function isObject(value) {
|
|
32
32
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
33
33
|
}
|
|
@@ -38,6 +38,9 @@ function isPromise(value) {
|
|
|
38
38
|
return isObject(value) && "then" in value && isFunction(value.then);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
// src/assertions.ts
|
|
42
|
+
var isPromise2 = isPromise;
|
|
43
|
+
|
|
41
44
|
// src/time.ts
|
|
42
45
|
var MINUTE_AS_MS = 60 * 1e3;
|
|
43
46
|
var HOUR_AS_MS = 60 * MINUTE_AS_MS;
|
|
@@ -152,7 +155,7 @@ function createCache({
|
|
|
152
155
|
cleanExpiredItems();
|
|
153
156
|
return unwrappedValue.value;
|
|
154
157
|
}
|
|
155
|
-
if (
|
|
158
|
+
if (isPromise2(entry.value)) {
|
|
156
159
|
throw new Error(
|
|
157
160
|
"Cache value is a promise, use getOrInsertAsync instead"
|
|
158
161
|
);
|
|
@@ -161,7 +164,7 @@ function createCache({
|
|
|
161
164
|
},
|
|
162
165
|
async getOrInsertAsync(cacheKey, val, options) {
|
|
163
166
|
const entry = cache.get(cacheKey);
|
|
164
|
-
if (entry &&
|
|
167
|
+
if (entry && isPromise2(entry.value)) {
|
|
165
168
|
return entry.value;
|
|
166
169
|
}
|
|
167
170
|
const now = Date.now();
|
|
@@ -207,7 +210,7 @@ function createCache({
|
|
|
207
210
|
if (!entry || isExpired(entry, Date.now())) {
|
|
208
211
|
return void 0;
|
|
209
212
|
}
|
|
210
|
-
if (
|
|
213
|
+
if (isPromise2(entry.value)) {
|
|
211
214
|
throw new Error("Cache value is a promise, use getAsync instead");
|
|
212
215
|
}
|
|
213
216
|
return entry.value;
|
package/lib/cache.js
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/typeGuards.ts
|
|
2
|
+
function isObject(value) {
|
|
3
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
function isFunction(value) {
|
|
6
|
+
return typeof value === "function";
|
|
7
|
+
}
|
|
8
|
+
function isPromise(value) {
|
|
9
|
+
return isObject(value) && "then" in value && isFunction(value.then);
|
|
10
|
+
}
|
|
11
|
+
function isPlainObject(value) {
|
|
12
|
+
if (!value || typeof value !== "object") return false;
|
|
13
|
+
const proto = Object.getPrototypeOf(value);
|
|
14
|
+
if (proto === null) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
const Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
|
|
18
|
+
if (Ctor === Object) return true;
|
|
19
|
+
const objectCtorString = Object.prototype.constructor.toString();
|
|
20
|
+
return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
|
|
21
|
+
}
|
|
22
|
+
function isNonEmptyArray(value) {
|
|
23
|
+
return value.length > 0;
|
|
24
|
+
}
|
|
25
|
+
function arrayHasAtLeastXItems(array, minLength) {
|
|
26
|
+
return array.length >= minLength;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export {
|
|
30
|
+
isObject,
|
|
31
|
+
isFunction,
|
|
32
|
+
isPromise,
|
|
33
|
+
isPlainObject,
|
|
34
|
+
isNonEmptyArray,
|
|
35
|
+
arrayHasAtLeastXItems
|
|
36
|
+
};
|