@ezez/utils 2.1.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/omit.d.ts +1 -1
- package/dist/omit.d.ts.map +1 -1
- package/dist/omit.js.map +1 -1
- package/dist/replace.d.ts.map +1 -1
- package/dist/replace.js.map +1 -1
- package/dist/replaceDeep.d.ts +5 -1
- package/dist/replaceDeep.d.ts.map +1 -1
- package/dist/replaceDeep.js +3 -20
- package/dist/replaceDeep.js.map +1 -1
- package/dist/replaceDeepByFn.d.ts +7 -0
- package/dist/replaceDeepByFn.d.ts.map +1 -0
- package/dist/replaceDeepByFn.js +47 -0
- package/dist/replaceDeepByFn.js.map +1 -0
- package/dist/safe.d.ts.map +1 -1
- package/dist/safe.js.map +1 -1
- package/dist/sample.d.ts +3 -0
- package/dist/sample.d.ts.map +1 -0
- package/dist/sample.js +8 -0
- package/dist/sample.js.map +1 -0
- package/dist/samples.d.ts +3 -0
- package/dist/samples.d.ts.map +1 -0
- package/dist/samples.js +28 -0
- package/dist/samples.js.map +1 -0
- package/dist/serialize.d.ts.map +1 -1
- package/dist/serialize.js +56 -18
- package/dist/serialize.js.map +1 -1
- package/dist/shuffle.d.ts +3 -0
- package/dist/shuffle.d.ts.map +1 -0
- package/dist/shuffle.js +9 -0
- package/dist/shuffle.js.map +1 -0
- package/dist/toggle.d.ts +3 -0
- package/dist/toggle.d.ts.map +1 -0
- package/dist/toggle.js +15 -0
- package/dist/toggle.js.map +1 -0
- package/dist/trim.d.ts +3 -0
- package/dist/trim.d.ts.map +1 -0
- package/dist/trim.js +10 -0
- package/dist/trim.js.map +1 -0
- package/dist/trimEnd.d.ts +3 -0
- package/dist/trimEnd.d.ts.map +1 -0
- package/dist/trimEnd.js +12 -0
- package/dist/trimEnd.js.map +1 -0
- package/dist/trimStart.d.ts +3 -0
- package/dist/trimStart.d.ts.map +1 -0
- package/dist/trimStart.js +12 -0
- package/dist/trimStart.js.map +1 -0
- package/dist/utils/utils.d.ts +6 -0
- package/dist/utils/utils.d.ts.map +1 -0
- package/dist/utils/utils.js +10 -0
- package/dist/utils/utils.js.map +1 -0
- package/dist/waitFor.d.ts +7 -1
- package/dist/waitFor.d.ts.map +1 -1
- package/dist/waitFor.js +35 -10
- package/dist/waitFor.js.map +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/functions/cap.html +12 -5
- package/docs/functions/capitalize.html +12 -5
- package/docs/functions/coalesce.html +12 -5
- package/docs/functions/compareArrays.html +12 -5
- package/docs/functions/compareProps.html +12 -5
- package/docs/functions/deserialize.html +12 -5
- package/docs/functions/ensureArray.html +12 -5
- package/docs/functions/ensureDate.html +12 -5
- package/docs/functions/ensureError.html +12 -5
- package/docs/functions/ensurePrefix.html +12 -5
- package/docs/functions/ensureSuffix.html +12 -5
- package/docs/functions/ensureTimestamp.html +12 -5
- package/docs/functions/escapeRegExp.html +12 -5
- package/docs/functions/formatDate.html +12 -5
- package/docs/functions/get.html +12 -5
- package/docs/functions/getMultiple.html +12 -5
- package/docs/functions/insertSeparator.html +12 -5
- package/docs/functions/isEmpty.html +12 -5
- package/docs/functions/isNumericString.html +12 -5
- package/docs/functions/isPlainObject.html +12 -5
- package/docs/functions/last.html +12 -5
- package/docs/functions/later-1.html +12 -5
- package/docs/functions/mapAsync.html +12 -5
- package/docs/functions/mapValues.html +12 -5
- package/docs/functions/match.html +12 -5
- package/docs/functions/merge.html +20 -13
- package/docs/functions/mostFrequent.html +12 -5
- package/docs/functions/noop.html +12 -5
- package/docs/functions/occurrences.html +12 -5
- package/docs/functions/omit.html +18 -7
- package/docs/functions/pick.html +13 -6
- package/docs/functions/pull.html +12 -5
- package/docs/functions/remove.html +12 -5
- package/docs/functions/removeCommonProperties.html +12 -5
- package/docs/functions/replace.html +12 -5
- package/docs/functions/replaceDeep.html +23 -7
- package/docs/functions/rethrow.html +12 -5
- package/docs/functions/round.html +12 -5
- package/docs/functions/safe.html +13 -6
- package/docs/functions/sample.html +149 -0
- package/docs/functions/samples.html +159 -0
- package/docs/functions/scale.html +12 -5
- package/docs/functions/seq.html +12 -5
- package/docs/functions/seqEarlyBreak.html +12 -5
- package/docs/functions/serialize.html +12 -5
- package/docs/functions/set.html +12 -5
- package/docs/functions/setImmutable.html +12 -5
- package/docs/functions/shuffle.html +149 -0
- package/docs/functions/sortBy.html +12 -5
- package/docs/functions/sortProps.html +12 -5
- package/docs/functions/stripPrefix.html +12 -5
- package/docs/functions/stripSuffix.html +12 -5
- package/docs/functions/throttle.html +12 -5
- package/docs/functions/toggle.html +154 -0
- package/docs/functions/trim.html +152 -0
- package/docs/functions/trimEnd.html +152 -0
- package/docs/functions/trimStart.html +152 -0
- package/docs/functions/truthy.html +12 -5
- package/docs/functions/unique.html +12 -5
- package/docs/functions/wait.html +12 -5
- package/docs/functions/waitFor.html +23 -18
- package/docs/functions/waitSync.html +12 -5
- package/docs/index.html +11 -4
- package/docs/interfaces/ComparePropsOptions.html +6 -6
- package/docs/interfaces/GetMultipleSource.html +12 -5
- package/docs/interfaces/GetSource.html +12 -5
- package/docs/interfaces/IsNumericStringOptions.html +9 -9
- package/docs/interfaces/OccurencesOptions.html +6 -6
- package/docs/interfaces/SetImmutableSource.html +12 -5
- package/docs/interfaces/SetSource.html +12 -5
- package/docs/interfaces/ThrottleOptions.html +7 -7
- package/docs/interfaces/ThrottledFunctionExtras.html +7 -7
- package/docs/modules.html +18 -4
- package/docs/pages/CHANGELOG.html +150 -61
- package/docs/pages/Introduction.html +11 -4
- package/docs/types/CustomDeserializers.html +12 -5
- package/docs/types/CustomSerializers.html +12 -5
- package/docs/types/Later.html +12 -5
- package/docs/types/MapValuesFn.html +12 -5
- package/docs/types/MatchCallback.html +12 -5
- package/docs/types/SeqEarlyBreaker.html +12 -5
- package/docs/types/SeqFn.html +12 -5
- package/docs/types/SeqFunctions.html +12 -5
- package/docs/types/SetImmutablePath.html +12 -5
- package/docs/types/ThrottledFunction.html +12 -5
- package/docs/variables/mapValuesUNSET.html +12 -5
- package/docs/variables/mergeUNSET.html +12 -5
- package/esm/index.d.ts +7 -0
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +7 -0
- package/esm/index.js.map +1 -1
- package/esm/omit.d.ts +1 -1
- package/esm/omit.d.ts.map +1 -1
- package/esm/omit.js.map +1 -1
- package/esm/replace.d.ts.map +1 -1
- package/esm/replace.js.map +1 -1
- package/esm/replaceDeep.d.ts +5 -1
- package/esm/replaceDeep.d.ts.map +1 -1
- package/esm/replaceDeep.js +3 -20
- package/esm/replaceDeep.js.map +1 -1
- package/esm/replaceDeepByFn.d.ts +7 -0
- package/esm/replaceDeepByFn.d.ts.map +1 -0
- package/esm/replaceDeepByFn.js +44 -0
- package/esm/replaceDeepByFn.js.map +1 -0
- package/esm/safe.d.ts.map +1 -1
- package/esm/safe.js.map +1 -1
- package/esm/sample.d.ts +3 -0
- package/esm/sample.d.ts.map +1 -0
- package/esm/sample.js +5 -0
- package/esm/sample.js.map +1 -0
- package/esm/samples.d.ts +3 -0
- package/esm/samples.d.ts.map +1 -0
- package/esm/samples.js +25 -0
- package/esm/samples.js.map +1 -0
- package/esm/serialize.d.ts.map +1 -1
- package/esm/serialize.js +56 -18
- package/esm/serialize.js.map +1 -1
- package/esm/shuffle.d.ts +3 -0
- package/esm/shuffle.d.ts.map +1 -0
- package/esm/shuffle.js +6 -0
- package/esm/shuffle.js.map +1 -0
- package/esm/toggle.d.ts +3 -0
- package/esm/toggle.d.ts.map +1 -0
- package/esm/toggle.js +12 -0
- package/esm/toggle.js.map +1 -0
- package/esm/trim.d.ts +3 -0
- package/esm/trim.d.ts.map +1 -0
- package/esm/trim.js +7 -0
- package/esm/trim.js.map +1 -0
- package/esm/trimEnd.d.ts +3 -0
- package/esm/trimEnd.d.ts.map +1 -0
- package/esm/trimEnd.js +9 -0
- package/esm/trimEnd.js.map +1 -0
- package/esm/trimStart.d.ts +3 -0
- package/esm/trimStart.d.ts.map +1 -0
- package/esm/trimStart.js +9 -0
- package/esm/trimStart.js.map +1 -0
- package/esm/utils/utils.d.ts +6 -0
- package/esm/utils/utils.d.ts.map +1 -0
- package/esm/utils/utils.js +7 -0
- package/esm/utils/utils.js.map +1 -0
- package/esm/waitFor.d.ts +7 -1
- package/esm/waitFor.d.ts.map +1 -1
- package/esm/waitFor.js +35 -10
- package/esm/waitFor.js.map +1 -1
- package/package.json +4 -4
- package/pnpm-lock.yaml +1162 -986
- package/src/deserialize.spec.ts +12 -0
- package/src/index.ts +7 -0
- package/src/omit.ts +8 -2
- package/src/pick.ts +1 -1
- package/src/replace.ts +0 -1
- package/src/replaceDeep.spec.ts +91 -0
- package/src/replaceDeep.ts +22 -27
- package/src/replaceDeepByFn.spec.ts +162 -0
- package/src/replaceDeepByFn.ts +93 -0
- package/src/safe.ts +0 -1
- package/src/sample.spec.ts +31 -0
- package/src/sample.ts +11 -0
- package/src/samples.spec.ts +50 -0
- package/src/samples.ts +41 -0
- package/src/serialize.spec.ts +42 -0
- package/src/serialize.ts +65 -18
- package/src/shuffle.spec.ts +39 -0
- package/src/shuffle.ts +13 -0
- package/src/toggle.spec.ts +43 -0
- package/src/toggle.ts +22 -0
- package/src/trim.spec.ts +22 -0
- package/src/trim.ts +23 -0
- package/src/trimEnd.spec.ts +20 -0
- package/src/trimEnd.ts +22 -0
- package/src/trimStart.spec.ts +20 -0
- package/src/trimStart.ts +22 -0
- package/src/utils/utils.ts +11 -0
- package/src/waitFor.spec.ts +141 -0
- package/src/waitFor.ts +69 -18
package/src/waitFor.ts
CHANGED
|
@@ -1,46 +1,97 @@
|
|
|
1
|
+
import { noop } from "./noop.js";
|
|
2
|
+
|
|
1
3
|
const DEFAULT_INTERVAL = 50;
|
|
2
4
|
|
|
3
5
|
type TTimeout = ReturnType<typeof setTimeout>;
|
|
4
|
-
type
|
|
6
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
7
|
+
|
|
8
|
+
type Options = {
|
|
9
|
+
/**
|
|
10
|
+
* Interval between checks in milliseconds
|
|
11
|
+
*/
|
|
12
|
+
interval?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Timeout in milliseconds
|
|
15
|
+
*/
|
|
16
|
+
timeout?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Maximum number of tries, 1 means no retry!
|
|
19
|
+
*/
|
|
20
|
+
maxTries?: number;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const defaultOptions: Required<Options> = {
|
|
24
|
+
interval: DEFAULT_INTERVAL,
|
|
25
|
+
timeout: Infinity,
|
|
26
|
+
maxTries: Infinity,
|
|
27
|
+
};
|
|
5
28
|
|
|
6
29
|
/**
|
|
7
30
|
* Runs the callback function every specified interval and returns a Promise that resolves when the callback returns
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
31
|
+
* any other value than `null`, `undefined` or `false`.
|
|
32
|
+
* If your callback throws (or Promise rejects) it will stop the interval and reject the returned Promise.
|
|
33
|
+
* To avoid that use:
|
|
34
|
+
* ```typescript
|
|
35
|
+
* waitFor(() => promiseReturningFunction().catch(() => null));
|
|
36
|
+
* // or
|
|
37
|
+
* waitFor(() => safe(() => functionThatThrows()));
|
|
38
|
+
* ```
|
|
39
|
+
* @param fn - callback function
|
|
40
|
+
* @param options - options object
|
|
14
41
|
*/
|
|
15
|
-
const waitFor = <T>(fn: () => T
|
|
42
|
+
const waitFor = <T>(fn: () => MaybePromise<T>, options: Options = defaultOptions) => {
|
|
16
43
|
return new Promise<T>((resolve, reject) => {
|
|
17
|
-
let intervalTimer:
|
|
44
|
+
let intervalTimer: TTimeout, failTimer: TTimeout;
|
|
18
45
|
|
|
19
|
-
|
|
46
|
+
const opts = { ...defaultOptions, ...options };
|
|
47
|
+
if (typeof opts.maxTries === "number" && opts.maxTries < 1) {
|
|
48
|
+
reject(new TypeError("[waitFor] maxTries must be greater than 0"));
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (Number.isFinite(opts.timeout)) {
|
|
20
53
|
failTimer = setTimeout(() => {
|
|
21
54
|
reject(new Error("[waitFor] Timeout"));
|
|
22
|
-
|
|
23
|
-
}, timeout);
|
|
55
|
+
clearTimeout(intervalTimer);
|
|
56
|
+
}, opts.timeout);
|
|
24
57
|
}
|
|
25
58
|
|
|
26
|
-
|
|
59
|
+
let tries = 0;
|
|
60
|
+
|
|
61
|
+
// eslint-disable-next-line max-statements
|
|
62
|
+
const tryFn = (async () => {
|
|
27
63
|
try {
|
|
28
|
-
|
|
29
|
-
|
|
64
|
+
tries++;
|
|
65
|
+
const result = await fn();
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
67
|
+
if (result != null && result !== false) {
|
|
30
68
|
clearTimeout(failTimer);
|
|
31
|
-
|
|
69
|
+
clearTimeout(intervalTimer);
|
|
32
70
|
resolve(result);
|
|
33
71
|
}
|
|
72
|
+
else {
|
|
73
|
+
if (Number.isFinite(opts.maxTries) && tries >= opts.maxTries) {
|
|
74
|
+
clearTimeout(failTimer);
|
|
75
|
+
clearTimeout(intervalTimer);
|
|
76
|
+
reject(new Error("[waitFor] Max tries reached"));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
setTimeout(() => {
|
|
81
|
+
tryFn().catch(noop);
|
|
82
|
+
}, options.interval);
|
|
83
|
+
}
|
|
34
84
|
}
|
|
35
85
|
catch (error: unknown) {
|
|
36
86
|
clearTimeout(failTimer);
|
|
37
|
-
|
|
87
|
+
clearTimeout(intervalTimer);
|
|
38
88
|
|
|
39
89
|
const e: Error & { details?: unknown } = new Error("[waitFor] check function threw an error");
|
|
40
90
|
e.details = { error };
|
|
41
91
|
reject(e);
|
|
42
92
|
}
|
|
43
|
-
}
|
|
93
|
+
});
|
|
94
|
+
tryFn().catch(noop);
|
|
44
95
|
});
|
|
45
96
|
};
|
|
46
97
|
|