@ezez/utils 4.1.0 → 4.3.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 +8 -0
- package/README.md +1 -0
- package/dist/cap.d.ts.map +1 -1
- package/dist/capitalize.d.ts.map +1 -1
- package/dist/coalesce.d.ts.map +1 -1
- package/dist/compareArrays.d.ts.map +1 -1
- package/dist/compareProps.d.ts.map +1 -1
- package/dist/deserialize.d.ts.map +1 -1
- package/dist/ensureArray.d.ts.map +1 -1
- package/dist/ensureDate.d.ts.map +1 -1
- package/dist/ensureError.d.ts.map +1 -1
- package/dist/ensurePrefix.d.ts.map +1 -1
- package/dist/ensureSuffix.d.ts.map +1 -1
- package/dist/ensureTimestamp.d.ts.map +1 -1
- package/dist/escapeRegExp.d.ts.map +1 -1
- package/dist/formatDate.d.ts.map +1 -1
- package/dist/get.d.ts.map +1 -1
- package/dist/getMultiple.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/insertSeparator.d.ts.map +1 -1
- package/dist/isEmpty.d.ts.map +1 -1
- package/dist/isNumericString.d.ts.map +1 -1
- package/dist/isPlainObject.d.ts.map +1 -1
- package/dist/last.d.ts.map +1 -1
- package/dist/later.d.ts.map +1 -1
- package/dist/mapAsync.d.ts.map +1 -1
- package/dist/mapValues.d.ts.map +1 -1
- package/dist/match.d.ts.map +1 -1
- package/dist/memoize.d.ts.map +1 -1
- package/dist/mostFrequent.d.ts.map +1 -1
- package/dist/occurrences.d.ts.map +1 -1
- package/dist/omit.d.ts.map +1 -1
- package/dist/pick.d.ts.map +1 -1
- package/dist/pull.d.ts.map +1 -1
- package/dist/race.d.ts +3 -0
- package/dist/race.d.ts.map +1 -0
- package/dist/race.js +14 -0
- package/dist/race.js.map +1 -0
- package/dist/remove.d.ts.map +1 -1
- package/dist/removeCommonProperties.d.ts.map +1 -1
- package/dist/replace.d.ts.map +1 -1
- package/dist/replaceDeep.d.ts.map +1 -1
- package/dist/replaceDeepByFn.d.ts.map +1 -1
- package/dist/rethrow.d.ts.map +1 -1
- package/dist/retry.d.ts +9 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +26 -0
- package/dist/retry.js.map +1 -0
- package/dist/round.d.ts.map +1 -1
- package/dist/safe.js +1 -2
- package/dist/safe.js.map +1 -1
- package/dist/sample.d.ts.map +1 -1
- package/dist/samples.d.ts.map +1 -1
- package/dist/scale.d.ts.map +1 -1
- package/dist/seq.d.ts.map +1 -1
- package/dist/serialize.d.ts.map +1 -1
- package/dist/set.d.ts.map +1 -1
- package/dist/setImmutable.d.ts.map +1 -1
- package/dist/shuffle.d.ts.map +1 -1
- package/dist/sortBy.d.ts.map +1 -1
- package/dist/sortProps.d.ts.map +1 -1
- package/dist/stripPrefix.d.ts.map +1 -1
- package/dist/stripSuffix.d.ts.map +1 -1
- package/dist/throttle.d.ts.map +1 -1
- package/dist/toggle.d.ts.map +1 -1
- package/dist/trim.d.ts.map +1 -1
- package/dist/trimEnd.d.ts.map +1 -1
- package/dist/trimStart.d.ts.map +1 -1
- package/dist/truthy.d.ts.map +1 -1
- package/dist/unique.d.ts.map +1 -1
- package/dist/wait.d.ts.map +1 -1
- package/dist/waitFor.d.ts.map +1 -1
- package/dist/waitSync.d.ts.map +1 -1
- package/docs/assets/icons.js +17 -14
- package/docs/assets/icons.svg +1 -1
- package/docs/assets/main.js +5 -4
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/assets/style.css +87 -23
- package/docs/documents/CHANGELOG.html +47 -41
- package/docs/functions/index.cap.html +3 -3
- package/docs/functions/index.capitalize.html +7 -7
- package/docs/functions/index.coalesce.html +7 -7
- package/docs/functions/index.compareArrays.html +2 -2
- package/docs/functions/index.compareProps.html +4 -4
- package/docs/functions/index.deserialize.html +5 -5
- package/docs/functions/index.ensureArray.html +2 -2
- package/docs/functions/index.ensureDate.html +6 -6
- package/docs/functions/index.ensureError.html +6 -6
- package/docs/functions/index.ensurePrefix.html +5 -5
- package/docs/functions/index.ensureSuffix.html +5 -5
- package/docs/functions/index.ensureTimestamp.html +6 -6
- package/docs/functions/index.escapeRegExp.html +5 -5
- package/docs/functions/index.formatDate.html +2 -2
- package/docs/functions/index.get.html +8 -8
- package/docs/functions/index.getMultiple.html +6 -6
- package/docs/functions/index.insertSeparator.html +2 -2
- package/docs/functions/index.isEmpty.html +10 -10
- package/docs/functions/index.isNumericString.html +3 -3
- package/docs/functions/index.isPlainObject.html +8 -8
- package/docs/functions/index.last.html +6 -6
- package/docs/functions/index.later-1.html +3 -3
- package/docs/functions/index.mapAsync.html +3 -3
- package/docs/functions/index.mapValues.html +6 -6
- package/docs/functions/index.match.html +2 -2
- package/docs/functions/index.memoize.html +4 -4
- package/docs/functions/index.merge.html +6 -6
- package/docs/functions/index.mostFrequent.html +2 -2
- package/docs/functions/index.noop.html +2 -2
- package/docs/functions/index.occurrences.html +7 -7
- package/docs/functions/index.omit.html +5 -5
- package/docs/functions/index.pick.html +5 -5
- package/docs/functions/index.pull.html +2 -2
- package/docs/functions/index.race.html +8 -0
- package/docs/functions/index.remove.html +2 -2
- package/docs/functions/index.removeCommonProperties.html +5 -5
- package/docs/functions/index.replace.html +5 -5
- package/docs/functions/index.replaceDeep.html +3 -3
- package/docs/functions/index.rethrow.html +2 -2
- package/docs/functions/index.retry.html +4 -0
- package/docs/functions/index.round.html +5 -5
- package/docs/functions/index.safe.html +5 -5
- package/docs/functions/index.sample.html +4 -4
- package/docs/functions/index.samples.html +2 -2
- package/docs/functions/index.scale.html +2 -2
- package/docs/functions/index.seq.html +6 -6
- package/docs/functions/index.seqEarlyBreak.html +5 -5
- package/docs/functions/index.serialize.html +4 -4
- package/docs/functions/index.set.html +7 -7
- package/docs/functions/index.setImmutable.html +7 -7
- package/docs/functions/index.shuffle.html +2 -2
- package/docs/functions/index.sortBy.html +4 -4
- package/docs/functions/index.sortProps.html +4 -4
- package/docs/functions/index.stripPrefix.html +2 -2
- package/docs/functions/index.stripSuffix.html +2 -2
- package/docs/functions/index.throttle.html +3 -3
- package/docs/functions/index.toggle.html +2 -2
- package/docs/functions/index.trim.html +5 -5
- package/docs/functions/index.trimEnd.html +5 -5
- package/docs/functions/index.trimStart.html +5 -5
- package/docs/functions/index.truthy.html +3 -3
- package/docs/functions/index.unique.html +4 -4
- package/docs/functions/index.wait.html +2 -2
- package/docs/functions/index.waitFor.html +2 -2
- package/docs/functions/index.waitSync.html +2 -2
- package/docs/index.html +4 -3
- package/docs/interfaces/index.ComparePropsOptions.html +4 -4
- package/docs/interfaces/index.GetMultipleSource.html +3 -3
- package/docs/interfaces/index.GetSource.html +3 -3
- package/docs/interfaces/index.IsNumericStringOptions.html +2 -2
- package/docs/interfaces/index.OccurencesOptions.html +3 -3
- package/docs/interfaces/index.SetImmutableSource.html +3 -3
- package/docs/interfaces/index.SetSource.html +3 -3
- package/docs/interfaces/index.ThrottleOptions.html +4 -4
- package/docs/interfaces/index.ThrottledFunctionExtras.html +4 -4
- package/docs/modules/index.html +5 -3
- package/docs/modules.html +3 -0
- package/docs/types/index.CustomDeserializers.html +1 -1
- package/docs/types/index.CustomSerializers.html +1 -1
- package/docs/types/index.Later.html +4 -4
- package/docs/types/index.MapValuesFn.html +4 -4
- package/docs/types/index.MatchCallback.html +1 -1
- package/docs/types/index.MergeTwo.html +2 -2
- package/docs/types/index.SeqEarlyBreaker.html +3 -3
- package/docs/types/index.SeqFn.html +3 -3
- package/docs/types/index.SeqFunctions.html +3 -3
- package/docs/types/index.SetImmutablePath.html +2 -2
- package/docs/types/index.ThrottledFunction.html +1 -1
- package/docs/variables/index.mapValuesUNSET.html +2 -2
- package/docs/variables/index.mergeUNSET.html +3 -3
- package/esm/cap.d.ts.map +1 -1
- package/esm/capitalize.d.ts.map +1 -1
- package/esm/coalesce.d.ts.map +1 -1
- package/esm/compareArrays.d.ts.map +1 -1
- package/esm/compareProps.d.ts.map +1 -1
- package/esm/deserialize.d.ts.map +1 -1
- package/esm/ensureArray.d.ts.map +1 -1
- package/esm/ensureDate.d.ts.map +1 -1
- package/esm/ensureError.d.ts.map +1 -1
- package/esm/ensurePrefix.d.ts.map +1 -1
- package/esm/ensureSuffix.d.ts.map +1 -1
- package/esm/ensureTimestamp.d.ts.map +1 -1
- package/esm/escapeRegExp.d.ts.map +1 -1
- package/esm/formatDate.d.ts.map +1 -1
- package/esm/get.d.ts.map +1 -1
- package/esm/getMultiple.d.ts.map +1 -1
- package/esm/index.d.ts +2 -0
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +2 -0
- package/esm/index.js.map +1 -1
- package/esm/insertSeparator.d.ts.map +1 -1
- package/esm/isEmpty.d.ts.map +1 -1
- package/esm/isNumericString.d.ts.map +1 -1
- package/esm/isPlainObject.d.ts.map +1 -1
- package/esm/last.d.ts.map +1 -1
- package/esm/later.d.ts.map +1 -1
- package/esm/mapAsync.d.ts.map +1 -1
- package/esm/mapValues.d.ts.map +1 -1
- package/esm/match.d.ts.map +1 -1
- package/esm/memoize.d.ts.map +1 -1
- package/esm/mostFrequent.d.ts.map +1 -1
- package/esm/occurrences.d.ts.map +1 -1
- package/esm/omit.d.ts.map +1 -1
- package/esm/pick.d.ts.map +1 -1
- package/esm/pull.d.ts.map +1 -1
- package/esm/race.d.ts +3 -0
- package/esm/race.d.ts.map +1 -0
- package/esm/race.js +11 -0
- package/esm/race.js.map +1 -0
- package/esm/remove.d.ts.map +1 -1
- package/esm/removeCommonProperties.d.ts.map +1 -1
- package/esm/replace.d.ts.map +1 -1
- package/esm/replaceDeep.d.ts.map +1 -1
- package/esm/replaceDeepByFn.d.ts.map +1 -1
- package/esm/rethrow.d.ts.map +1 -1
- package/esm/retry.d.ts +9 -0
- package/esm/retry.d.ts.map +1 -0
- package/esm/retry.js +23 -0
- package/esm/retry.js.map +1 -0
- package/esm/round.d.ts.map +1 -1
- package/esm/sample.d.ts.map +1 -1
- package/esm/samples.d.ts.map +1 -1
- package/esm/scale.d.ts.map +1 -1
- package/esm/seq.d.ts.map +1 -1
- package/esm/serialize.d.ts.map +1 -1
- package/esm/set.d.ts.map +1 -1
- package/esm/setImmutable.d.ts.map +1 -1
- package/esm/shuffle.d.ts.map +1 -1
- package/esm/sortBy.d.ts.map +1 -1
- package/esm/sortProps.d.ts.map +1 -1
- package/esm/stripPrefix.d.ts.map +1 -1
- package/esm/stripSuffix.d.ts.map +1 -1
- package/esm/throttle.d.ts.map +1 -1
- package/esm/toggle.d.ts.map +1 -1
- package/esm/trim.d.ts.map +1 -1
- package/esm/trimEnd.d.ts.map +1 -1
- package/esm/trimStart.d.ts.map +1 -1
- package/esm/truthy.d.ts.map +1 -1
- package/esm/unique.d.ts.map +1 -1
- package/esm/wait.d.ts.map +1 -1
- package/esm/waitFor.d.ts.map +1 -1
- package/esm/waitSync.d.ts.map +1 -1
- package/package.json +1 -1
- package/pnpm-lock.yaml +4729 -3874
- package/src/get.spec.ts +1 -9
- package/src/index.ts +2 -0
- package/src/mapValues.spec.ts +1 -2
- package/src/race.ts +29 -0
- package/src/retry.ts +53 -0
package/src/get.spec.ts
CHANGED
|
@@ -48,7 +48,6 @@ describe("get", () => {
|
|
|
48
48
|
const value = get(testObject, "product.name");
|
|
49
49
|
value.must.equal("abc");
|
|
50
50
|
|
|
51
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
52
51
|
const _value = _get(testObject, "product.name");
|
|
53
52
|
value.must.equal(_value);
|
|
54
53
|
});
|
|
@@ -59,7 +58,6 @@ describe("get", () => {
|
|
|
59
58
|
const name = get(testObject, "product[name]");
|
|
60
59
|
(name === undefined).must.be.true();
|
|
61
60
|
|
|
62
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
63
61
|
const _name = _get(testObject, "product[name]"); // deep value
|
|
64
62
|
_name.must.equal("abc");
|
|
65
63
|
|
|
@@ -74,7 +72,7 @@ describe("get", () => {
|
|
|
74
72
|
const value = get(testObject, "product.something");
|
|
75
73
|
(value === undefined).must.be.true();
|
|
76
74
|
|
|
77
|
-
// eslint-disable-next-line @typescript-eslint/no-
|
|
75
|
+
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
|
|
78
76
|
const _value = _get(testObject, "product.something");
|
|
79
77
|
(_value === undefined).must.be.true();
|
|
80
78
|
});
|
|
@@ -83,7 +81,6 @@ describe("get", () => {
|
|
|
83
81
|
const value = get(testObject, "product.something", 3);
|
|
84
82
|
value.must.equal(3);
|
|
85
83
|
|
|
86
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
87
84
|
const _value = _get(testObject, "product.something", 3);
|
|
88
85
|
_value.must.equal(3);
|
|
89
86
|
});
|
|
@@ -92,7 +89,6 @@ describe("get", () => {
|
|
|
92
89
|
const value = get(testObject, "product.value", 3);
|
|
93
90
|
(value === undefined).must.be.true();
|
|
94
91
|
|
|
95
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
96
92
|
const _value = _get(testObject, "product.value", 3);
|
|
97
93
|
_value.must.equal(3);
|
|
98
94
|
});
|
|
@@ -101,7 +97,6 @@ describe("get", () => {
|
|
|
101
97
|
const value = get(testObject, "product.empty", 3);
|
|
102
98
|
(value === null).must.be.true();
|
|
103
99
|
|
|
104
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
105
100
|
const _value = _get(testObject, "product.empty", 3);
|
|
106
101
|
(_value === null).must.be.true();
|
|
107
102
|
});
|
|
@@ -110,7 +105,6 @@ describe("get", () => {
|
|
|
110
105
|
const value = get(testObject, "product.notANumber", 3);
|
|
111
106
|
value.must.be.nan();
|
|
112
107
|
|
|
113
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
114
108
|
const _value = _get(testObject, "product.notANumber", 3);
|
|
115
109
|
_value.must.be.nan();
|
|
116
110
|
});
|
|
@@ -119,7 +113,6 @@ describe("get", () => {
|
|
|
119
113
|
const value = get(testObject, "product.object");
|
|
120
114
|
value.must.equal(otherObject);
|
|
121
115
|
|
|
122
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
123
116
|
const _value = _get(testObject, "product.object");
|
|
124
117
|
_value.must.equal(otherObject);
|
|
125
118
|
});
|
|
@@ -144,7 +137,6 @@ describe("get", () => {
|
|
|
144
137
|
const value = get(NaN, "product.object", 3);
|
|
145
138
|
value.must.equal(3);
|
|
146
139
|
|
|
147
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
148
140
|
const _value = _get(NaN, "product.object", 3);
|
|
149
141
|
_value.must.equal(3);
|
|
150
142
|
});
|
package/src/index.ts
CHANGED
|
@@ -32,11 +32,13 @@ export * from "./occurrences.js";
|
|
|
32
32
|
export * from "./omit.js";
|
|
33
33
|
export * from "./pick.js";
|
|
34
34
|
export * from "./pull.js";
|
|
35
|
+
export * from "./race.js";
|
|
35
36
|
export * from "./remove.js";
|
|
36
37
|
export * from "./removeCommonProperties.js";
|
|
37
38
|
export * from "./replace.js";
|
|
38
39
|
export * from "./replaceDeep.js";
|
|
39
40
|
export * from "./rethrow.js";
|
|
41
|
+
export * from "./retry.js";
|
|
40
42
|
export * from "./round.js";
|
|
41
43
|
export * from "./safe.js";
|
|
42
44
|
export * from "./sample.js";
|
package/src/mapValues.spec.ts
CHANGED
|
@@ -165,9 +165,8 @@ describe("mapValues", () => {
|
|
|
165
165
|
|
|
166
166
|
const res = mapValues(array, (val) => (typeof val === "string" ? REMOVE : val));
|
|
167
167
|
|
|
168
|
-
// eslint-disable-next-line no-sparse-arrays
|
|
169
168
|
res.must.eql([
|
|
170
|
-
1, , 1, , 69,
|
|
169
|
+
1, , 1, , 69, // eslint-disable-line no-sparse-arrays
|
|
171
170
|
]);
|
|
172
171
|
("0" in res).must.be.true();
|
|
173
172
|
("1" in res).must.be.false();
|
package/src/race.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This function helps you timeout your promises while keeping the TS types right.
|
|
3
|
+
*
|
|
4
|
+
* @example await race(yourPromise, 1000); // resolves (or rejects) with the value of yourPromise or rejects with "Race: Timeout" message in a second
|
|
5
|
+
* @example
|
|
6
|
+
* ```
|
|
7
|
+
* type Data = { a: number };
|
|
8
|
+
* const myPromise = new Promise<Data>((resolve) => setTimeout(() => resolve({ a: 1 }), 2000));
|
|
9
|
+
* const { a } = await race(myPromise, 1000); // You're allowed to destructure the result with TS
|
|
10
|
+
*
|
|
11
|
+
* @param promise - Your promise
|
|
12
|
+
* @param timeout - Time in milliseconds to wait for the promise to resolve
|
|
13
|
+
* @param message - Error message to use when the timeout is reached
|
|
14
|
+
*/
|
|
15
|
+
const race = <T>(promise: Promise<T>, timeout: number, message = "Race: Timeout"): Promise<T> => {
|
|
16
|
+
return Promise.race([
|
|
17
|
+
promise, new Promise<T>((_, reject) => {
|
|
18
|
+
setTimeout(() => {
|
|
19
|
+
reject(new Error(message));
|
|
20
|
+
}, timeout);
|
|
21
|
+
}),
|
|
22
|
+
]);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export {
|
|
26
|
+
race,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// TODO unit tests
|
package/src/retry.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { wait } from "./wait";
|
|
2
|
+
|
|
3
|
+
type EarlyBreaker = (error: unknown, count: number) => boolean;
|
|
4
|
+
type Options = {
|
|
5
|
+
/**
|
|
6
|
+
* Maximum number of retries. If not specified, the function will retry indefinitely.
|
|
7
|
+
* 0 means the function will be executed once (no retries)
|
|
8
|
+
* 1 means the function will be executed twice (1 retry), etc.
|
|
9
|
+
*/
|
|
10
|
+
maxRetries?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Function to determine if the function should be retried, based on the error that was thrown and the number of retries so far.
|
|
13
|
+
*/
|
|
14
|
+
earlyBreak?: EarlyBreaker;
|
|
15
|
+
/**
|
|
16
|
+
* Number of milliseconds to wait between retries.
|
|
17
|
+
*/
|
|
18
|
+
waitBetween?: number | ((retriesCount: number) => number);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Execute a function until it succeeds. Limit the retries, wait between retries, control when to stop trying early.
|
|
23
|
+
* @param fn - The function to execute.
|
|
24
|
+
* @param options - Options controlling the behavior of the function.
|
|
25
|
+
*/
|
|
26
|
+
const retry = async <T>(fn: () => Promise<T>, options?: Options): Promise<T> => {
|
|
27
|
+
const maxRetries = options?.maxRetries ?? Infinity;
|
|
28
|
+
const earlyBreak = options?.earlyBreak ?? (() => false);
|
|
29
|
+
const waitBetween = options?.waitBetween ?? 0;
|
|
30
|
+
|
|
31
|
+
let retriesCount = 0;
|
|
32
|
+
|
|
33
|
+
while (true) { // eslint-disable-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
|
|
34
|
+
try {
|
|
35
|
+
return await fn();
|
|
36
|
+
}
|
|
37
|
+
catch (e: unknown) {
|
|
38
|
+
if (retriesCount >= maxRetries || earlyBreak(e, retriesCount)) {
|
|
39
|
+
throw e; // eslint-disable-line @typescript-eslint/only-throw-error
|
|
40
|
+
}
|
|
41
|
+
retriesCount++;
|
|
42
|
+
if (waitBetween) {
|
|
43
|
+
await wait(typeof waitBetween === "function" ? waitBetween(retriesCount) : waitBetween);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export {
|
|
50
|
+
retry,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// TODO unit tests
|