@ls-stack/utils 3.63.0 → 3.66.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 → arrayUtils.d.mts} +24 -23
- package/dist/arrayUtils.mjs +249 -0
- package/dist/assertions-qMxfVhSu.mjs +207 -0
- package/dist/{assertions.d.ts → assertions.d.mts} +4 -3
- package/dist/assertions.mjs +3 -0
- package/dist/asyncQueue.d.mts +497 -0
- package/dist/asyncQueue.mjs +757 -0
- package/dist/{awaitDebounce.d.cts → awaitDebounce.d.mts} +11 -6
- package/dist/awaitDebounce.mjs +54 -0
- package/dist/{cache.d.ts → cache.d.mts} +76 -68
- package/dist/cache.mjs +355 -0
- package/dist/castValues-DfICShCc.mjs +19 -0
- package/dist/{castValues.d.cts → castValues.d.mts} +3 -2
- package/dist/castValues.mjs +3 -0
- package/dist/{concurrentCalls.d.ts → concurrentCalls.d.mts} +74 -65
- package/dist/concurrentCalls.mjs +295 -0
- package/dist/consoleFmt.d.mts +55 -0
- package/dist/consoleFmt.mjs +63 -0
- package/dist/conversions-DTmwEMIu.mjs +12 -0
- package/dist/conversions.d.mts +4 -0
- package/dist/conversions.mjs +3 -0
- package/dist/createThrottleController.d.mts +18 -0
- package/dist/createThrottleController.mjs +40 -0
- package/dist/debounce.d.mts +47 -0
- package/dist/debounce.mjs +117 -0
- package/dist/dedent.d.mts +74 -0
- package/dist/dedent.mjs +80 -0
- package/dist/deepEqual-C7EZEixx.mjs +78 -0
- package/dist/{deepEqual.d.cts → deepEqual.d.mts} +3 -2
- package/dist/deepEqual.mjs +3 -0
- package/dist/{deepReplaceValues.d.cts → deepReplaceValues.d.mts} +4 -3
- package/dist/deepReplaceValues.mjs +61 -0
- package/dist/diffParser.d.mts +63 -0
- package/dist/diffParser.mjs +410 -0
- package/dist/enhancedMap.d.mts +21 -0
- package/dist/enhancedMap.mjs +69 -0
- package/dist/exhaustiveMatch.d.mts +10 -0
- package/dist/exhaustiveMatch.mjs +48 -0
- package/dist/{filterObjectOrArrayKeys.d.cts → filterObjectOrArrayKeys.d.mts} +15 -8
- package/dist/filterObjectOrArrayKeys.mjs +497 -0
- package/dist/{getAutoIncrementId.d.cts → getAutoIncrementId.d.mts} +9 -5
- package/dist/getAutoIncrementId.mjs +53 -0
- package/dist/{getCompositeKey.d.cts → getCompositeKey.d.mts} +3 -2
- package/dist/getCompositeKey.mjs +50 -0
- package/dist/{getValueStableKey.d.cts → getValueStableKey.d.mts} +5 -3
- package/dist/getValueStableKey.mjs +17 -0
- package/dist/{hash.d.cts → hash.d.mts} +3 -2
- package/dist/hash.mjs +28 -0
- package/dist/interpolate.d.mts +17 -0
- package/dist/interpolate.mjs +28 -0
- package/dist/{iteratorUtils.d.cts → iteratorUtils.d.mts} +5 -4
- package/dist/iteratorUtils.mjs +39 -0
- package/dist/keepPrevIfUnchanged.d.mts +12 -0
- package/dist/keepPrevIfUnchanged.mjs +9 -0
- package/dist/keyedMap.d.mts +76 -0
- package/dist/keyedMap.mjs +139 -0
- package/dist/keyedSet.d.mts +77 -0
- package/dist/keyedSet.mjs +129 -0
- package/dist/{levenshtein.d.cts → levenshtein.d.mts} +3 -2
- package/dist/levenshtein.mjs +121 -0
- package/dist/main.d.mts +4 -0
- package/dist/main.mjs +7 -0
- package/dist/matchPath.d.mts +50 -0
- package/dist/matchPath.mjs +81 -0
- package/dist/mathUtils-BDP1lM_z.mjs +81 -0
- package/dist/{mathUtils.d.cts → mathUtils.d.mts} +3 -2
- package/dist/mathUtils.mjs +3 -0
- package/dist/{mutationUtils.d.cts → mutationUtils.d.mts} +6 -5
- package/dist/mutationUtils.mjs +44 -0
- package/dist/{objUtils.d.ts → objUtils.d.mts} +8 -6
- package/dist/objUtils.mjs +115 -0
- package/dist/parallelAsyncCalls.d.mts +83 -0
- package/dist/parallelAsyncCalls.mjs +121 -0
- package/dist/partialEqual.d.mts +139 -0
- package/dist/partialEqual.mjs +1055 -0
- package/dist/promiseUtils.d.mts +9 -0
- package/dist/promiseUtils.mjs +17 -0
- package/dist/regexUtils.d.mts +18 -0
- package/dist/regexUtils.mjs +34 -0
- package/dist/{retryOnError.d.cts → retryOnError.d.mts} +38 -37
- package/dist/retryOnError.mjs +91 -0
- package/dist/{runShellCmd.d.ts → runShellCmd.d.mts} +24 -15
- package/dist/runShellCmd.mjs +151 -0
- package/dist/{safeJson.d.cts → safeJson.d.mts} +3 -2
- package/dist/safeJson.mjs +30 -0
- package/dist/{saferTyping.d.cts → saferTyping.d.mts} +4 -3
- package/dist/saferTyping.mjs +45 -0
- package/dist/serializeXML.d.mts +23 -0
- package/dist/serializeXML.mjs +74 -0
- package/dist/{shallowEqual.d.cts → shallowEqual.d.mts} +3 -2
- package/dist/shallowEqual.mjs +54 -0
- package/dist/sleep.d.mts +4 -0
- package/dist/sleep.mjs +7 -0
- package/dist/stringUtils-DjhWOiYn.mjs +113 -0
- package/dist/{stringUtils.d.cts → stringUtils.d.mts} +3 -2
- package/dist/stringUtils.mjs +3 -0
- package/dist/{testUtils.d.ts → testUtils.d.mts} +83 -52
- package/dist/testUtils.mjs +310 -0
- package/dist/{throttle.d.ts → throttle.d.mts} +18 -17
- package/dist/throttle.mjs +102 -0
- package/dist/time-sr2lhQRw.mjs +67 -0
- package/dist/{time.d.ts → time.d.mts} +8 -7
- package/dist/time.mjs +3 -0
- package/dist/{timers.d.cts → timers.d.mts} +22 -13
- package/dist/timers.mjs +220 -0
- package/dist/{tsResult.d.cts → tsResult.d.mts} +52 -48
- package/dist/tsResult.mjs +142 -0
- package/dist/typeGuards-B1mzA-Rz.mjs +128 -0
- package/dist/{typeGuards.d.cts → typeGuards.d.mts} +3 -2
- package/dist/typeGuards.mjs +3 -0
- package/dist/{typeUtils.d.ts → typeUtils.d.mts} +13 -34
- package/dist/typeUtils.mjs +1 -0
- package/dist/{typedStrings.d.cts → typedStrings.d.mts} +5 -4
- package/dist/typedStrings.mjs +131 -0
- package/dist/typingFnUtils-Bb8drgKF.mjs +101 -0
- package/dist/{typingFnUtils.d.cts → typingFnUtils.d.mts} +13 -22
- package/dist/typingFnUtils.mjs +3 -0
- package/dist/{typingTestUtils.d.cts → typingTestUtils.d.mts} +11 -15
- package/dist/typingTestUtils.mjs +80 -0
- package/dist/typingUtils.d.mts +20 -0
- package/dist/typingUtils.mjs +1 -0
- package/dist/yamlStringify.d.mts +17 -0
- package/dist/yamlStringify.mjs +189 -0
- package/package.json +65 -234
- package/dist/arrayUtils.cjs +0 -229
- package/dist/arrayUtils.d.ts +0 -171
- package/dist/arrayUtils.js +0 -42
- package/dist/assertions.cjs +0 -107
- package/dist/assertions.d.cts +0 -192
- package/dist/assertions.js +0 -25
- package/dist/asyncQueue.cjs +0 -672
- package/dist/asyncQueue.d.cts +0 -488
- package/dist/asyncQueue.d.ts +0 -488
- package/dist/asyncQueue.js +0 -631
- package/dist/awaitDebounce.cjs +0 -106
- package/dist/awaitDebounce.d.ts +0 -41
- package/dist/awaitDebounce.js +0 -28
- package/dist/cache.cjs +0 -367
- package/dist/cache.d.cts +0 -228
- package/dist/cache.js +0 -19
- package/dist/castValues.cjs +0 -50
- package/dist/castValues.d.ts +0 -4
- package/dist/castValues.js +0 -8
- package/dist/chunk-5DZT3Z5Z.js +0 -8
- package/dist/chunk-6FBIEPWU.js +0 -96
- package/dist/chunk-6FIBVC2P.js +0 -56
- package/dist/chunk-7CQPOM5I.js +0 -100
- package/dist/chunk-B6DNOZCP.js +0 -369
- package/dist/chunk-BM4PYVOX.js +0 -109
- package/dist/chunk-C2SVCIWE.js +0 -57
- package/dist/chunk-CCUPDGSZ.js +0 -132
- package/dist/chunk-DBOWTYR4.js +0 -49
- package/dist/chunk-DFXNVEH6.js +0 -14
- package/dist/chunk-DX2524CZ.js +0 -314
- package/dist/chunk-GMJTLFM6.js +0 -60
- package/dist/chunk-IATIXMCE.js +0 -20
- package/dist/chunk-II4R3VVX.js +0 -25
- package/dist/chunk-JF2MDHOJ.js +0 -40
- package/dist/chunk-JQFUKJU5.js +0 -71
- package/dist/chunk-MI4UE2PQ.js +0 -561
- package/dist/chunk-PUKVXYYL.js +0 -52
- package/dist/chunk-QQS7I7ZL.js +0 -16
- package/dist/chunk-VAAMRG4K.js +0 -20
- package/dist/chunk-WFQJUJTC.js +0 -182
- package/dist/chunk-ZXIKIA5B.js +0 -178
- package/dist/concurrentCalls.cjs +0 -406
- package/dist/concurrentCalls.d.cts +0 -116
- package/dist/concurrentCalls.js +0 -346
- package/dist/consoleFmt.cjs +0 -85
- package/dist/consoleFmt.d.cts +0 -54
- package/dist/consoleFmt.d.ts +0 -54
- package/dist/consoleFmt.js +0 -60
- package/dist/conversions.cjs +0 -44
- package/dist/conversions.d.cts +0 -3
- package/dist/conversions.d.ts +0 -3
- package/dist/conversions.js +0 -6
- package/dist/createThrottleController.cjs +0 -193
- package/dist/createThrottleController.d.cts +0 -13
- package/dist/createThrottleController.d.ts +0 -13
- package/dist/createThrottleController.js +0 -61
- package/dist/debounce.cjs +0 -157
- package/dist/debounce.d.cts +0 -46
- package/dist/debounce.d.ts +0 -46
- package/dist/debounce.js +0 -8
- package/dist/dedent.cjs +0 -104
- package/dist/dedent.d.cts +0 -73
- package/dist/dedent.d.ts +0 -73
- package/dist/dedent.js +0 -79
- package/dist/deepEqual.cjs +0 -96
- package/dist/deepEqual.d.ts +0 -21
- package/dist/deepEqual.js +0 -8
- package/dist/deepReplaceValues.cjs +0 -87
- package/dist/deepReplaceValues.d.ts +0 -27
- package/dist/deepReplaceValues.js +0 -7
- package/dist/enhancedMap.cjs +0 -131
- package/dist/enhancedMap.d.cts +0 -20
- package/dist/enhancedMap.d.ts +0 -20
- package/dist/enhancedMap.js +0 -10
- package/dist/exhaustiveMatch.cjs +0 -66
- package/dist/exhaustiveMatch.d.cts +0 -9
- package/dist/exhaustiveMatch.d.ts +0 -9
- package/dist/exhaustiveMatch.js +0 -40
- package/dist/filterObjectOrArrayKeys.cjs +0 -619
- package/dist/filterObjectOrArrayKeys.d.ts +0 -88
- package/dist/filterObjectOrArrayKeys.js +0 -9
- package/dist/getAutoIncrementId.cjs +0 -44
- package/dist/getAutoIncrementId.d.ts +0 -46
- package/dist/getAutoIncrementId.js +0 -18
- package/dist/getCompositeKey.cjs +0 -86
- package/dist/getCompositeKey.d.ts +0 -11
- package/dist/getCompositeKey.js +0 -8
- package/dist/getValueStableKey.cjs +0 -89
- package/dist/getValueStableKey.d.ts +0 -15
- package/dist/getValueStableKey.js +0 -11
- package/dist/hash.cjs +0 -57
- package/dist/hash.d.ts +0 -7
- package/dist/hash.js +0 -32
- package/dist/interpolate.cjs +0 -88
- package/dist/interpolate.d.cts +0 -11
- package/dist/interpolate.d.ts +0 -11
- package/dist/interpolate.js +0 -46
- package/dist/iteratorUtils.cjs +0 -73
- package/dist/iteratorUtils.d.ts +0 -10
- package/dist/iteratorUtils.js +0 -44
- package/dist/keepPrevIfUnchanged.cjs +0 -102
- package/dist/keepPrevIfUnchanged.d.cts +0 -7
- package/dist/keepPrevIfUnchanged.d.ts +0 -7
- package/dist/keepPrevIfUnchanged.js +0 -7
- package/dist/levenshtein.cjs +0 -180
- package/dist/levenshtein.d.ts +0 -5
- package/dist/levenshtein.js +0 -153
- package/dist/main.cjs +0 -32
- package/dist/main.d.cts +0 -3
- package/dist/main.d.ts +0 -3
- package/dist/main.js +0 -7
- package/dist/matchPath.cjs +0 -155
- package/dist/matchPath.d.cts +0 -53
- package/dist/matchPath.d.ts +0 -53
- package/dist/matchPath.js +0 -108
- package/dist/mathUtils.cjs +0 -81
- package/dist/mathUtils.d.ts +0 -54
- package/dist/mathUtils.js +0 -22
- package/dist/mutationUtils.cjs +0 -153
- package/dist/mutationUtils.d.ts +0 -15
- package/dist/mutationUtils.js +0 -55
- package/dist/objUtils.cjs +0 -242
- package/dist/objUtils.d.cts +0 -28
- package/dist/objUtils.js +0 -38
- package/dist/parallelAsyncCalls.cjs +0 -162
- package/dist/parallelAsyncCalls.d.cts +0 -82
- package/dist/parallelAsyncCalls.d.ts +0 -82
- package/dist/parallelAsyncCalls.js +0 -126
- package/dist/partialEqual.cjs +0 -1196
- package/dist/partialEqual.d.cts +0 -141
- package/dist/partialEqual.d.ts +0 -141
- package/dist/partialEqual.js +0 -1168
- package/dist/promiseUtils.cjs +0 -38
- package/dist/promiseUtils.d.cts +0 -8
- package/dist/promiseUtils.d.ts +0 -8
- package/dist/promiseUtils.js +0 -6
- package/dist/regexUtils.cjs +0 -60
- package/dist/regexUtils.d.cts +0 -17
- package/dist/regexUtils.d.ts +0 -17
- package/dist/regexUtils.js +0 -33
- package/dist/retryOnError.cjs +0 -130
- package/dist/retryOnError.d.ts +0 -83
- package/dist/retryOnError.js +0 -101
- package/dist/runShellCmd.cjs +0 -127
- package/dist/runShellCmd.d.cts +0 -90
- package/dist/runShellCmd.js +0 -98
- package/dist/safeJson.cjs +0 -45
- package/dist/safeJson.d.ts +0 -16
- package/dist/safeJson.js +0 -8
- package/dist/saferTyping.cjs +0 -52
- package/dist/saferTyping.d.ts +0 -47
- package/dist/saferTyping.js +0 -23
- package/dist/serializeXML.cjs +0 -154
- package/dist/serializeXML.d.cts +0 -22
- package/dist/serializeXML.d.ts +0 -22
- package/dist/serializeXML.js +0 -116
- package/dist/shallowEqual.cjs +0 -88
- package/dist/shallowEqual.d.ts +0 -4
- package/dist/shallowEqual.js +0 -63
- package/dist/sleep.cjs +0 -32
- package/dist/sleep.d.cts +0 -3
- package/dist/sleep.d.ts +0 -3
- package/dist/sleep.js +0 -6
- package/dist/stringUtils.cjs +0 -155
- package/dist/stringUtils.d.ts +0 -55
- package/dist/stringUtils.js +0 -50
- package/dist/testUtils.cjs +0 -1490
- package/dist/testUtils.d.cts +0 -133
- package/dist/testUtils.js +0 -359
- package/dist/throttle.cjs +0 -282
- package/dist/throttle.d.cts +0 -98
- package/dist/throttle.js +0 -38
- package/dist/time.cjs +0 -152
- package/dist/time.d.cts +0 -25
- package/dist/time.js +0 -38
- package/dist/timers.cjs +0 -194
- package/dist/timers.d.ts +0 -121
- package/dist/timers.js +0 -156
- package/dist/tsResult.cjs +0 -226
- package/dist/tsResult.d.ts +0 -114
- package/dist/tsResult.js +0 -180
- package/dist/typeGuards.cjs +0 -70
- package/dist/typeGuards.d.ts +0 -111
- package/dist/typeGuards.js +0 -18
- package/dist/typeUtils.cjs +0 -18
- package/dist/typeUtils.d.cts +0 -61
- package/dist/typeUtils.js +0 -0
- package/dist/typedStrings.cjs +0 -90
- package/dist/typedStrings.d.ts +0 -163
- package/dist/typedStrings.js +0 -57
- package/dist/typingFnUtils.cjs +0 -96
- package/dist/typingFnUtils.d.ts +0 -100
- package/dist/typingFnUtils.js +0 -30
- package/dist/typingTestUtils.cjs +0 -52
- package/dist/typingTestUtils.d.ts +0 -79
- package/dist/typingTestUtils.js +0 -27
- package/dist/typingUtils.cjs +0 -18
- package/dist/typingUtils.d.cts +0 -35
- package/dist/typingUtils.d.ts +0 -35
- package/dist/typingUtils.js +0 -0
- package/dist/yamlStringify.cjs +0 -423
- package/dist/yamlStringify.d.cts +0 -10
- package/dist/yamlStringify.d.ts +0 -10
- package/dist/yamlStringify.js +0 -9
package/dist/timers.mjs
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { defer } from "./promiseUtils.mjs";
|
|
2
|
+
import { Result } from "t-result";
|
|
3
|
+
|
|
4
|
+
//#region src/timers.ts
|
|
5
|
+
/**
|
|
6
|
+
* Creates a timeout with automatic cleanup capability.
|
|
7
|
+
*
|
|
8
|
+
* Returns a cleanup function that can be called to cancel the timeout. The
|
|
9
|
+
* cleanup function is idempotent - calling it multiple times is safe.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const cleanup = createTimeout(1000, () => {
|
|
14
|
+
* console.log('Timeout completed');
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* // Cancel the timeout before it completes
|
|
18
|
+
* cleanup();
|
|
19
|
+
* ```;
|
|
20
|
+
*
|
|
21
|
+
* @param ms - The timeout duration in milliseconds
|
|
22
|
+
* @param callback - The function to execute when the timeout completes
|
|
23
|
+
* @returns A cleanup function that cancels the timeout when called
|
|
24
|
+
*/
|
|
25
|
+
function createTimeout(ms, callback) {
|
|
26
|
+
const timeoutId = setTimeout(callback, ms);
|
|
27
|
+
let isCleaned = false;
|
|
28
|
+
return () => {
|
|
29
|
+
if (isCleaned) return;
|
|
30
|
+
clearTimeout(timeoutId);
|
|
31
|
+
isCleaned = true;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Creates an interval with automatic cleanup capability.
|
|
36
|
+
*
|
|
37
|
+
* Returns a cleanup function that can be called to cancel the interval. The
|
|
38
|
+
* cleanup function is idempotent - calling it multiple times is safe.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* const cleanup = createInterval(1000, () => {
|
|
43
|
+
* console.log('Interval tick');
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // Stop the interval
|
|
47
|
+
* cleanup();
|
|
48
|
+
* ```;
|
|
49
|
+
*
|
|
50
|
+
* @param ms - The interval duration in milliseconds
|
|
51
|
+
* @param callback - The function to execute on each interval tick
|
|
52
|
+
* @returns A cleanup function that cancels the interval when called
|
|
53
|
+
*/
|
|
54
|
+
function createInterval(ms, callback) {
|
|
55
|
+
const intervalId = setInterval(callback, ms);
|
|
56
|
+
let isCleaned = false;
|
|
57
|
+
return () => {
|
|
58
|
+
if (isCleaned) return;
|
|
59
|
+
clearInterval(intervalId);
|
|
60
|
+
isCleaned = true;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Creates a timeout that prevents concurrent executions.
|
|
65
|
+
*
|
|
66
|
+
* Each call to the `call` function will cancel any previous pending timeout and
|
|
67
|
+
* start a new one. This is useful for debouncing or ensuring only the last call
|
|
68
|
+
* executes after a delay.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* const { call, clean } = createDebouncedTimeout(1000, () => {
|
|
73
|
+
* console.log('Only the last call executes');
|
|
74
|
+
* });
|
|
75
|
+
*
|
|
76
|
+
* call(); // This will be cancelled
|
|
77
|
+
* call(); // This will be cancelled
|
|
78
|
+
* call(); // Only this one will execute after 1000ms
|
|
79
|
+
*
|
|
80
|
+
* // Or cancel all pending timeouts
|
|
81
|
+
* clean();
|
|
82
|
+
* ```;
|
|
83
|
+
*
|
|
84
|
+
* @param ms - The timeout duration in milliseconds
|
|
85
|
+
* @param callback - The function to execute when the timeout completes
|
|
86
|
+
* @returns An object with `call` to trigger the timeout and `clean` to cancel
|
|
87
|
+
* it
|
|
88
|
+
*/
|
|
89
|
+
function createDebouncedTimeout(ms, callback) {
|
|
90
|
+
let cleanupTimer = null;
|
|
91
|
+
return {
|
|
92
|
+
clean: () => {
|
|
93
|
+
cleanupTimer?.();
|
|
94
|
+
},
|
|
95
|
+
call: () => {
|
|
96
|
+
cleanupTimer?.();
|
|
97
|
+
cleanupTimer = createTimeout(ms, () => {
|
|
98
|
+
callback();
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Creates a timeout that waits for a condition to become true.
|
|
105
|
+
*
|
|
106
|
+
* Polls the condition function at regular intervals until it returns a truthy
|
|
107
|
+
* value, then calls the callback with that value. If the condition doesn't
|
|
108
|
+
* become true within the maximum wait time, the timeout expires without calling
|
|
109
|
+
* the callback.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const cleanup = createWaitUntil({
|
|
114
|
+
* condition: () => document.getElementById('myElement'),
|
|
115
|
+
* maxWaitMs: 5000,
|
|
116
|
+
* callback: (element) => {
|
|
117
|
+
* console.log('Element found:', element);
|
|
118
|
+
* },
|
|
119
|
+
* checkIntervalMs: 50
|
|
120
|
+
* });
|
|
121
|
+
*
|
|
122
|
+
* // Cancel the condition check
|
|
123
|
+
* cleanup();
|
|
124
|
+
* ```;
|
|
125
|
+
*
|
|
126
|
+
* @template T - The type of value returned by the condition function when true
|
|
127
|
+
* @param options - Configuration options
|
|
128
|
+
* @param options.condition - Function that returns false or a truthy value when
|
|
129
|
+
* the condition is met
|
|
130
|
+
* @param options.maxWaitMs - Maximum time to wait for the condition in
|
|
131
|
+
* milliseconds
|
|
132
|
+
* @param options.callback - Function to call when the condition becomes true
|
|
133
|
+
* @param options.checkIntervalMs - How often to check the condition in
|
|
134
|
+
* milliseconds (default: 20)
|
|
135
|
+
* @returns A cleanup function that cancels the condition timeout
|
|
136
|
+
*/
|
|
137
|
+
function createWaitUntil({ condition, maxWaitMs, callback, checkIntervalMs = 20 }) {
|
|
138
|
+
let cleanCheckTimeout = null;
|
|
139
|
+
let cleanMaxWaitTimeout = null;
|
|
140
|
+
cleanMaxWaitTimeout = createTimeout(maxWaitMs, () => {
|
|
141
|
+
cleanCheckTimeout?.();
|
|
142
|
+
});
|
|
143
|
+
function check() {
|
|
144
|
+
const result = condition();
|
|
145
|
+
if (result) {
|
|
146
|
+
cleanMaxWaitTimeout?.();
|
|
147
|
+
callback(result);
|
|
148
|
+
} else cleanCheckTimeout = createTimeout(checkIntervalMs, check);
|
|
149
|
+
}
|
|
150
|
+
check();
|
|
151
|
+
return () => {
|
|
152
|
+
cleanMaxWaitTimeout();
|
|
153
|
+
cleanCheckTimeout?.();
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
async function waitFor(condition, { polling, timeout }) {
|
|
157
|
+
const { promise, resolve } = defer();
|
|
158
|
+
let timeoutId = null;
|
|
159
|
+
let intervalId = null;
|
|
160
|
+
let rafId = null;
|
|
161
|
+
let isResolved = false;
|
|
162
|
+
function cleanup() {
|
|
163
|
+
if (timeoutId) {
|
|
164
|
+
clearTimeout(timeoutId);
|
|
165
|
+
timeoutId = null;
|
|
166
|
+
}
|
|
167
|
+
if (intervalId) {
|
|
168
|
+
clearInterval(intervalId);
|
|
169
|
+
intervalId = null;
|
|
170
|
+
}
|
|
171
|
+
if (rafId && typeof cancelAnimationFrame !== "undefined") {
|
|
172
|
+
cancelAnimationFrame(rafId);
|
|
173
|
+
rafId = null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function resolveWith(result) {
|
|
177
|
+
if (isResolved) return;
|
|
178
|
+
isResolved = true;
|
|
179
|
+
cleanup();
|
|
180
|
+
resolve(result);
|
|
181
|
+
}
|
|
182
|
+
async function checkCondition() {
|
|
183
|
+
try {
|
|
184
|
+
const result = condition();
|
|
185
|
+
if (result instanceof Promise ? await result : result) {
|
|
186
|
+
resolveWith(Result.ok(void 0));
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
return false;
|
|
190
|
+
} catch (error) {
|
|
191
|
+
resolveWith(Result.err(/* @__PURE__ */ new Error(`Condition check failed: ${error}`)));
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
checkCondition().then((resolved) => {
|
|
196
|
+
if (resolved) return;
|
|
197
|
+
timeoutId = setTimeout(() => {
|
|
198
|
+
resolveWith(Result.err(/* @__PURE__ */ new Error(`Timeout of ${timeout}ms exceeded while waiting for condition`)));
|
|
199
|
+
}, timeout);
|
|
200
|
+
if (polling === "raf") {
|
|
201
|
+
if (typeof requestAnimationFrame === "undefined") {
|
|
202
|
+
resolveWith(Result.err(/* @__PURE__ */ new Error("requestAnimationFrame is not available in this environment")));
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
function rafCheck() {
|
|
206
|
+
if (isResolved) return;
|
|
207
|
+
checkCondition().then((conditionResolved) => {
|
|
208
|
+
if (!conditionResolved && !isResolved) rafId = requestAnimationFrame(rafCheck);
|
|
209
|
+
}).catch(() => {});
|
|
210
|
+
}
|
|
211
|
+
rafId = requestAnimationFrame(rafCheck);
|
|
212
|
+
} else intervalId = setInterval(() => {
|
|
213
|
+
checkCondition().catch(() => {});
|
|
214
|
+
}, polling);
|
|
215
|
+
}).catch(() => {});
|
|
216
|
+
return promise;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
//#endregion
|
|
220
|
+
export { createDebouncedTimeout, createInterval, createTimeout, createWaitUntil, waitFor };
|
|
@@ -1,38 +1,41 @@
|
|
|
1
|
+
//#region src/tsResult.d.ts
|
|
1
2
|
type Ok<T> = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
ok: true;
|
|
4
|
+
error: false;
|
|
5
|
+
value: T;
|
|
5
6
|
} & AnyResultMethods;
|
|
6
7
|
type AnyResultMethods = Record<ResultMethodsKeys, never>;
|
|
7
8
|
/** @deprecated Use `t-result` library instead. */
|
|
8
9
|
type ResultValidErrors = Error | Record<string, unknown> | unknown[] | readonly unknown[] | true;
|
|
9
10
|
type Err<E extends ResultValidErrors> = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
ok: false;
|
|
12
|
+
error: E;
|
|
13
|
+
/** @deprecated Use `t-result` library instead. */
|
|
14
|
+
errorResult: () => Err<E>;
|
|
14
15
|
} & AnyResultMethods;
|
|
15
16
|
type ResultMethods<T, E extends ResultValidErrors> = {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
17
|
+
/** @deprecated Use `t-result` library instead. */
|
|
18
|
+
unwrapOrNull: () => T | null;
|
|
19
|
+
/** @deprecated Use `t-result` library instead. */
|
|
20
|
+
unwrapOr: <R extends T>(defaultValue: R) => T | R;
|
|
21
|
+
/** @deprecated Use `t-result` library instead. */
|
|
22
|
+
unwrap: () => T;
|
|
23
|
+
/** @deprecated Use `t-result` library instead. */
|
|
24
|
+
mapOk: <NewValue>(mapFn: (value: T) => NewValue) => Result<NewValue, E>;
|
|
25
|
+
/** @deprecated Use `t-result` library instead. */
|
|
26
|
+
mapErr: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Result<T, NewError>;
|
|
27
|
+
/** @deprecated Use `t-result` library instead. */
|
|
28
|
+
mapOkAndErr: <NewValue, NewError extends ResultValidErrors>(mapFns: {
|
|
29
|
+
ok: (value: T) => NewValue;
|
|
30
|
+
err: (error: E) => NewError;
|
|
31
|
+
}) => Result<NewValue, NewError>;
|
|
32
|
+
/** @deprecated Use `t-result` library instead. */
|
|
33
|
+
ifOk: (fn: (value: T) => void) => Result<T, E>;
|
|
34
|
+
/** @deprecated Use `t-result` library instead. */
|
|
35
|
+
ifErr: (fn: (error: E) => void) => Result<T, E>;
|
|
35
36
|
};
|
|
37
|
+
/** @deprecated Use `t-result` library instead. */
|
|
38
|
+
type Result<T, E extends ResultValidErrors = Error> = (Omit<Ok<T>, ResultMethodsKeys> | Omit<Err<E>, ResultMethodsKeys>) & ResultMethods<T, E>;
|
|
36
39
|
type ResultMethodsKeys = keyof ResultMethods<any, any>;
|
|
37
40
|
/** @deprecated Use `t-result` library instead. */
|
|
38
41
|
declare function ok(): Ok<void>;
|
|
@@ -54,23 +57,24 @@ declare function unknownToResultError(error: unknown): Err<Error>;
|
|
|
54
57
|
*/
|
|
55
58
|
declare function asyncUnwrap<T>(result: Promise<Result<T, ResultValidErrors>>): Promise<T>;
|
|
56
59
|
declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise<Result<T, E>>): {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
err: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Promise<Result<T, NewError>>;
|
|
61
|
+
ok: <NewValue>(mapFn: (value: T) => NewValue) => Promise<Result<NewValue, E>>;
|
|
62
|
+
okAndErr: <NewValue, NewError extends ResultValidErrors>({
|
|
63
|
+
ok: mapFn,
|
|
64
|
+
err: mapErrFn
|
|
65
|
+
}: {
|
|
66
|
+
ok: (value: T) => NewValue;
|
|
67
|
+
err: (error: E) => NewError;
|
|
68
|
+
}) => Promise<Result<NewValue, NewError>>;
|
|
63
69
|
};
|
|
64
70
|
/** @deprecated Use `t-result` library instead. */
|
|
65
|
-
type Result<T, E extends ResultValidErrors = Error> = (Omit<Ok<T>, ResultMethodsKeys> | Omit<Err<E>, ResultMethodsKeys>) & ResultMethods<T, E>;
|
|
66
|
-
/** @deprecated Use `t-result` library instead. */
|
|
67
71
|
declare const Result: {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
ok: typeof ok;
|
|
73
|
+
err: typeof err;
|
|
74
|
+
unknownToError: typeof unknownToResultError;
|
|
75
|
+
asyncUnwrap: typeof asyncUnwrap;
|
|
76
|
+
asyncMap: typeof asyncMap;
|
|
77
|
+
getOkErr: typeof getOkErr;
|
|
74
78
|
};
|
|
75
79
|
/**
|
|
76
80
|
* @deprecated Use `t-result` library instead.
|
|
@@ -97,12 +101,12 @@ declare function resultify<T, E extends ResultValidErrors = Error>(fn: Promise<T
|
|
|
97
101
|
declare function unknownToError(error: unknown): Error;
|
|
98
102
|
/** @deprecated Use `t-result` library instead. */
|
|
99
103
|
type TypedResult<T, E extends ResultValidErrors = Error> = {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
/** @deprecated Use `t-result` library instead. */
|
|
105
|
+
ok: (value: T) => Ok<T>;
|
|
106
|
+
/** @deprecated Use `t-result` library instead. */
|
|
107
|
+
err: (error: E) => Err<E>;
|
|
108
|
+
/** @deprecated Use `t-result` library instead. */
|
|
109
|
+
_type: Result<T, E>;
|
|
106
110
|
};
|
|
107
111
|
/** @deprecated Use `t-result` library instead. */
|
|
108
112
|
type GetTypedResult<R extends Result<any, any>> = TypedResult<R extends Result<infer T, any> ? T : never, R extends Result<any, infer E> ? E : never>;
|
|
@@ -110,5 +114,5 @@ declare function getOkErr<F extends (...args: any[]) => Promise<Result<any, any>
|
|
|
110
114
|
declare function getOkErr<F extends (...args: any[]) => Result<any, any>>(): TypedResult<ReturnType<F> extends Result<infer T, any> ? T : never, ReturnType<F> extends Result<any, infer E> ? E : never>;
|
|
111
115
|
declare function getOkErr<R extends Result<any, any>>(): TypedResult<R extends Result<infer T, any> ? T : never, R extends Result<any, infer E> ? E : never>;
|
|
112
116
|
declare function getOkErr<T, E extends ResultValidErrors = Error>(): TypedResult<T, E>;
|
|
113
|
-
|
|
114
|
-
export {
|
|
117
|
+
//#endregion
|
|
118
|
+
export { GetTypedResult, Result, ResultValidErrors, TypedResult, err, ok, resultify, unknownToError };
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { a as isFunction, c as isPromise, o as isObject } from "./assertions-qMxfVhSu.mjs";
|
|
2
|
+
import { safeJsonStringify } from "./safeJson.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/tsResult.ts
|
|
5
|
+
function okUnwrapOr() {
|
|
6
|
+
return this.value;
|
|
7
|
+
}
|
|
8
|
+
function okMap(mapFn) {
|
|
9
|
+
return this.ok ? ok(mapFn(this.value)) : this;
|
|
10
|
+
}
|
|
11
|
+
function errMap(mapFn) {
|
|
12
|
+
return this.ok ? this : err(mapFn(this.error));
|
|
13
|
+
}
|
|
14
|
+
function mapOkAndErr({ ok: mapFn, err: mapErrFn }) {
|
|
15
|
+
return this.ok ? ok(mapFn(this.value)) : err(mapErrFn(this.error));
|
|
16
|
+
}
|
|
17
|
+
function returnResult() {
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
function okOnOk(fn) {
|
|
21
|
+
if (this.ok) fn(this.value);
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
function errOnErr(fn) {
|
|
25
|
+
if (!this.ok) fn(this.error);
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* @deprecated Use `t-result` library instead.
|
|
30
|
+
* @param value
|
|
31
|
+
*/
|
|
32
|
+
function ok(value = void 0) {
|
|
33
|
+
return {
|
|
34
|
+
ok: true,
|
|
35
|
+
error: false,
|
|
36
|
+
value,
|
|
37
|
+
unwrapOrNull: okUnwrapOr,
|
|
38
|
+
unwrapOr: okUnwrapOr,
|
|
39
|
+
unwrap: okUnwrapOr,
|
|
40
|
+
mapOk: okMap,
|
|
41
|
+
mapErr: returnResult,
|
|
42
|
+
mapOkAndErr,
|
|
43
|
+
ifOk: okOnOk,
|
|
44
|
+
ifErr: returnResult
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* @deprecated Use `t-result` library instead.
|
|
49
|
+
* @param error
|
|
50
|
+
*/
|
|
51
|
+
function err(error) {
|
|
52
|
+
return {
|
|
53
|
+
ok: false,
|
|
54
|
+
error,
|
|
55
|
+
errorResult() {
|
|
56
|
+
return err(error);
|
|
57
|
+
},
|
|
58
|
+
unwrapOrNull: () => null,
|
|
59
|
+
unwrapOr: (defaultValue) => defaultValue,
|
|
60
|
+
unwrap: (() => {
|
|
61
|
+
if (error instanceof Error) throw error;
|
|
62
|
+
throw unknownToError(error);
|
|
63
|
+
}),
|
|
64
|
+
mapOk: returnResult,
|
|
65
|
+
mapErr: errMap,
|
|
66
|
+
mapOkAndErr,
|
|
67
|
+
ifOk: returnResult,
|
|
68
|
+
ifErr: errOnErr
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function unknownToResultError(error) {
|
|
72
|
+
return err(unknownToError(error));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Unwraps a promise result
|
|
76
|
+
*
|
|
77
|
+
* @param result
|
|
78
|
+
*/
|
|
79
|
+
async function asyncUnwrap(result) {
|
|
80
|
+
const unwrapped = await result;
|
|
81
|
+
if (unwrapped.ok) return unwrapped.value;
|
|
82
|
+
if (unwrapped.error instanceof Error) throw unwrapped.error;
|
|
83
|
+
throw unknownToError(unwrapped.error);
|
|
84
|
+
}
|
|
85
|
+
function asyncMap(resultPromise) {
|
|
86
|
+
return {
|
|
87
|
+
err: async (mapFn) => {
|
|
88
|
+
const result = await resultPromise;
|
|
89
|
+
return result.ok ? ok(result.value) : err(mapFn(result.error));
|
|
90
|
+
},
|
|
91
|
+
ok: async (mapFn) => {
|
|
92
|
+
const result = await resultPromise;
|
|
93
|
+
return result.ok ? ok(mapFn(result.value)) : err(result.error);
|
|
94
|
+
},
|
|
95
|
+
okAndErr: async ({ ok: mapFn, err: mapErrFn }) => {
|
|
96
|
+
const result = await resultPromise;
|
|
97
|
+
return result.ok ? ok(mapFn(result.value)) : err(mapErrFn(result.error));
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** @deprecated Use `t-result` library instead. */
|
|
102
|
+
const Result = {
|
|
103
|
+
ok,
|
|
104
|
+
err,
|
|
105
|
+
unknownToError: unknownToResultError,
|
|
106
|
+
asyncUnwrap,
|
|
107
|
+
asyncMap,
|
|
108
|
+
getOkErr
|
|
109
|
+
};
|
|
110
|
+
function resultify(fn, errorNormalizer) {
|
|
111
|
+
if (!isFunction(fn)) return fn.then((value) => ok(value)).catch((error) => err(errorNormalizer ? errorNormalizer(error) : unknownToError(error)));
|
|
112
|
+
try {
|
|
113
|
+
const result = fn();
|
|
114
|
+
if (isPromise(result)) return result.then((value) => ok(value)).catch((error) => err(errorNormalizer ? errorNormalizer(error) : unknownToError(error)));
|
|
115
|
+
return ok(result);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
return err(errorNormalizer ? errorNormalizer(error) : unknownToError(error));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* @deprecated Use `t-result` library instead.
|
|
122
|
+
* @param error
|
|
123
|
+
*/
|
|
124
|
+
function unknownToError(error) {
|
|
125
|
+
if (error instanceof Error) return error;
|
|
126
|
+
if (typeof error === "string") return new Error(error);
|
|
127
|
+
if (isObject(error)) return new Error("message" in error && error.message && typeof error.message === "string" ? error.message : safeJsonStringify(error) ?? "unknown", { cause: error });
|
|
128
|
+
return new Error(safeJsonStringify(error) ?? "unknown", { cause: error });
|
|
129
|
+
}
|
|
130
|
+
const typedResult = {
|
|
131
|
+
ok,
|
|
132
|
+
err,
|
|
133
|
+
get _type() {
|
|
134
|
+
throw new Error("usage as value is not allowed");
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
function getOkErr() {
|
|
138
|
+
return typedResult;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
//#endregion
|
|
142
|
+
export { Result, err, ok, resultify, unknownToError };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
//#region src/typeGuards.ts
|
|
2
|
+
/**
|
|
3
|
+
* Type guard to check if a value is a plain object (not null, not an array).
|
|
4
|
+
*
|
|
5
|
+
* Returns true if the value is an object that is not null and not an array.
|
|
6
|
+
* This is useful for distinguishing between objects and other types.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* if (isObject(value)) {
|
|
11
|
+
* // TypeScript knows value is Record<string, unknown>
|
|
12
|
+
* console.log(value.someProperty);
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* isObject({}); // true
|
|
16
|
+
* isObject({ a: 1 }); // true
|
|
17
|
+
* isObject(null); // false
|
|
18
|
+
* isObject([]); // false
|
|
19
|
+
* isObject('string'); // false
|
|
20
|
+
* ```;
|
|
21
|
+
*
|
|
22
|
+
* @param value - The value to check
|
|
23
|
+
* @returns True if the value is a plain object, false otherwise
|
|
24
|
+
*/
|
|
25
|
+
function isObject(value) {
|
|
26
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Type guard to check if a value is a function.
|
|
30
|
+
*
|
|
31
|
+
* Returns true if the value is a function of any kind (regular function, arrow
|
|
32
|
+
* function, method, constructor, etc.).
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* if (isFunction(value)) {
|
|
37
|
+
* // TypeScript knows value is (...args: any[]) => any
|
|
38
|
+
* const result = value();
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* isFunction(() => {}); // true
|
|
42
|
+
* isFunction(function() {}); // true
|
|
43
|
+
* isFunction(Math.max); // true
|
|
44
|
+
* isFunction('string'); // false
|
|
45
|
+
* isFunction({}); // false
|
|
46
|
+
* ```;
|
|
47
|
+
*
|
|
48
|
+
* @param value - The value to check
|
|
49
|
+
* @returns True if the value is a function, false otherwise
|
|
50
|
+
*/
|
|
51
|
+
function isFunction(value) {
|
|
52
|
+
return typeof value === "function";
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Type guard to check if a value is a Promise or thenable object.
|
|
56
|
+
*
|
|
57
|
+
* Returns true if the value is an object with a `then` method that is a
|
|
58
|
+
* function. This covers both native Promises and thenable objects that
|
|
59
|
+
* implement the Promise interface.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* if (isPromise(value)) {
|
|
64
|
+
* // TypeScript knows value is Promise<unknown>
|
|
65
|
+
* const result = await value;
|
|
66
|
+
* }
|
|
67
|
+
*
|
|
68
|
+
* isPromise(Promise.resolve()); // true
|
|
69
|
+
* isPromise(new Promise(() => {})); // true
|
|
70
|
+
* isPromise({ then: () => {} }); // true
|
|
71
|
+
* isPromise({ then: 'not a function' }); // false
|
|
72
|
+
* isPromise('string'); // false
|
|
73
|
+
* ```;
|
|
74
|
+
*
|
|
75
|
+
* @param value - The value to check
|
|
76
|
+
* @returns True if the value is a Promise or thenable, false otherwise
|
|
77
|
+
*/
|
|
78
|
+
function isPromise(value) {
|
|
79
|
+
return isObject(value) && "then" in value && isFunction(value.then);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Type guard to check if a value is a plain object (created by Object literal
|
|
83
|
+
* or Object constructor).
|
|
84
|
+
*
|
|
85
|
+
* Returns true if the value is a plain object - an object created by the Object
|
|
86
|
+
* constructor or object literal syntax. This excludes instances of classes,
|
|
87
|
+
* built-in objects like Date, RegExp, etc.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* if (isPlainObject(value)) {
|
|
92
|
+
* // TypeScript knows value is Record<string, unknown>
|
|
93
|
+
* console.log(Object.keys(value));
|
|
94
|
+
* }
|
|
95
|
+
*
|
|
96
|
+
* isPlainObject({}); // true
|
|
97
|
+
* isPlainObject({ a: 1 }); // true
|
|
98
|
+
* isPlainObject(Object.create(null)); // true
|
|
99
|
+
* isPlainObject(new Date()); // false
|
|
100
|
+
* isPlainObject(/regex/); // false
|
|
101
|
+
* isPlainObject(new MyClass()); // false
|
|
102
|
+
* isPlainObject([]); // false
|
|
103
|
+
* ```;
|
|
104
|
+
*
|
|
105
|
+
* @param value - The value to check
|
|
106
|
+
* @returns True if the value is a plain object, false otherwise
|
|
107
|
+
*/
|
|
108
|
+
function isPlainObject(value) {
|
|
109
|
+
if (!value || typeof value !== "object") return false;
|
|
110
|
+
const proto = Object.getPrototypeOf(value);
|
|
111
|
+
if (proto === null) return true;
|
|
112
|
+
const Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
|
|
113
|
+
if (Ctor === Object) return true;
|
|
114
|
+
const objectCtorString = Object.prototype.constructor.toString();
|
|
115
|
+
return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
|
|
116
|
+
}
|
|
117
|
+
function isNonEmptyArray(value) {
|
|
118
|
+
return value.length > 0;
|
|
119
|
+
}
|
|
120
|
+
function arrayHasAtLeastXItems(array, minLength) {
|
|
121
|
+
return array.length >= minLength;
|
|
122
|
+
}
|
|
123
|
+
function isTruthy(value) {
|
|
124
|
+
return !!value;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
//#endregion
|
|
128
|
+
export { isPlainObject as a, isObject as i, isFunction as n, isPromise as o, isNonEmptyArray as r, isTruthy as s, arrayHasAtLeastXItems as t };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/typeGuards.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Type guard to check if a value is a plain object (not null, not an array).
|
|
3
4
|
*
|
|
@@ -107,5 +108,5 @@ declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 3): array is [T
|
|
|
107
108
|
declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 4): array is [T, T, T, T, ...T[]];
|
|
108
109
|
declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 5): array is [T, T, T, T, T, ...T[]];
|
|
109
110
|
declare function isTruthy<T>(value: T): value is Exclude<T, null | undefined | false | 0 | '' | 0n>;
|
|
110
|
-
|
|
111
|
-
export {
|
|
111
|
+
//#endregion
|
|
112
|
+
export { NonEmptyArray, arrayHasAtLeastXItems, isFunction, isNonEmptyArray, isObject, isPlainObject, isPromise, isTruthy };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { a as isPlainObject, i as isObject, n as isFunction, o as isPromise, r as isNonEmptyArray, s as isTruthy, t as arrayHasAtLeastXItems } from "./typeGuards-B1mzA-Rz.mjs";
|
|
2
|
+
|
|
3
|
+
export { arrayHasAtLeastXItems, isFunction, isNonEmptyArray, isObject, isPlainObject, isPromise, isTruthy };
|