@ezez/utils 4.1.0 → 4.2.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.
Files changed (242) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +1 -0
  3. package/dist/cap.d.ts.map +1 -1
  4. package/dist/capitalize.d.ts.map +1 -1
  5. package/dist/coalesce.d.ts.map +1 -1
  6. package/dist/compareArrays.d.ts.map +1 -1
  7. package/dist/compareProps.d.ts.map +1 -1
  8. package/dist/deserialize.d.ts.map +1 -1
  9. package/dist/ensureArray.d.ts.map +1 -1
  10. package/dist/ensureDate.d.ts.map +1 -1
  11. package/dist/ensureError.d.ts.map +1 -1
  12. package/dist/ensurePrefix.d.ts.map +1 -1
  13. package/dist/ensureSuffix.d.ts.map +1 -1
  14. package/dist/ensureTimestamp.d.ts.map +1 -1
  15. package/dist/escapeRegExp.d.ts.map +1 -1
  16. package/dist/formatDate.d.ts.map +1 -1
  17. package/dist/get.d.ts.map +1 -1
  18. package/dist/getMultiple.d.ts.map +1 -1
  19. package/dist/index.d.ts +1 -0
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +1 -0
  22. package/dist/index.js.map +1 -1
  23. package/dist/insertSeparator.d.ts.map +1 -1
  24. package/dist/isEmpty.d.ts.map +1 -1
  25. package/dist/isNumericString.d.ts.map +1 -1
  26. package/dist/isPlainObject.d.ts.map +1 -1
  27. package/dist/last.d.ts.map +1 -1
  28. package/dist/later.d.ts.map +1 -1
  29. package/dist/mapAsync.d.ts.map +1 -1
  30. package/dist/mapValues.d.ts.map +1 -1
  31. package/dist/match.d.ts.map +1 -1
  32. package/dist/memoize.d.ts.map +1 -1
  33. package/dist/mostFrequent.d.ts.map +1 -1
  34. package/dist/occurrences.d.ts.map +1 -1
  35. package/dist/omit.d.ts.map +1 -1
  36. package/dist/pick.d.ts.map +1 -1
  37. package/dist/pull.d.ts.map +1 -1
  38. package/dist/remove.d.ts.map +1 -1
  39. package/dist/removeCommonProperties.d.ts.map +1 -1
  40. package/dist/replace.d.ts.map +1 -1
  41. package/dist/replaceDeep.d.ts.map +1 -1
  42. package/dist/replaceDeepByFn.d.ts.map +1 -1
  43. package/dist/rethrow.d.ts.map +1 -1
  44. package/dist/retry.d.ts +9 -0
  45. package/dist/retry.d.ts.map +1 -0
  46. package/dist/retry.js +26 -0
  47. package/dist/retry.js.map +1 -0
  48. package/dist/round.d.ts.map +1 -1
  49. package/dist/safe.js +1 -2
  50. package/dist/safe.js.map +1 -1
  51. package/dist/sample.d.ts.map +1 -1
  52. package/dist/samples.d.ts.map +1 -1
  53. package/dist/scale.d.ts.map +1 -1
  54. package/dist/seq.d.ts.map +1 -1
  55. package/dist/serialize.d.ts.map +1 -1
  56. package/dist/set.d.ts.map +1 -1
  57. package/dist/setImmutable.d.ts.map +1 -1
  58. package/dist/shuffle.d.ts.map +1 -1
  59. package/dist/sortBy.d.ts.map +1 -1
  60. package/dist/sortProps.d.ts.map +1 -1
  61. package/dist/stripPrefix.d.ts.map +1 -1
  62. package/dist/stripSuffix.d.ts.map +1 -1
  63. package/dist/throttle.d.ts.map +1 -1
  64. package/dist/toggle.d.ts.map +1 -1
  65. package/dist/trim.d.ts.map +1 -1
  66. package/dist/trimEnd.d.ts.map +1 -1
  67. package/dist/trimStart.d.ts.map +1 -1
  68. package/dist/truthy.d.ts.map +1 -1
  69. package/dist/unique.d.ts.map +1 -1
  70. package/dist/wait.d.ts.map +1 -1
  71. package/dist/waitFor.d.ts.map +1 -1
  72. package/dist/waitSync.d.ts.map +1 -1
  73. package/docs/assets/icons.js +17 -14
  74. package/docs/assets/icons.svg +1 -1
  75. package/docs/assets/main.js +5 -4
  76. package/docs/assets/navigation.js +1 -1
  77. package/docs/assets/search.js +1 -1
  78. package/docs/assets/style.css +87 -23
  79. package/docs/documents/CHANGELOG.html +44 -41
  80. package/docs/functions/index.cap.html +3 -3
  81. package/docs/functions/index.capitalize.html +7 -7
  82. package/docs/functions/index.coalesce.html +7 -7
  83. package/docs/functions/index.compareArrays.html +2 -2
  84. package/docs/functions/index.compareProps.html +4 -4
  85. package/docs/functions/index.deserialize.html +5 -5
  86. package/docs/functions/index.ensureArray.html +2 -2
  87. package/docs/functions/index.ensureDate.html +6 -6
  88. package/docs/functions/index.ensureError.html +6 -6
  89. package/docs/functions/index.ensurePrefix.html +5 -5
  90. package/docs/functions/index.ensureSuffix.html +5 -5
  91. package/docs/functions/index.ensureTimestamp.html +6 -6
  92. package/docs/functions/index.escapeRegExp.html +5 -5
  93. package/docs/functions/index.formatDate.html +2 -2
  94. package/docs/functions/index.get.html +8 -8
  95. package/docs/functions/index.getMultiple.html +6 -6
  96. package/docs/functions/index.insertSeparator.html +2 -2
  97. package/docs/functions/index.isEmpty.html +10 -10
  98. package/docs/functions/index.isNumericString.html +3 -3
  99. package/docs/functions/index.isPlainObject.html +8 -8
  100. package/docs/functions/index.last.html +6 -6
  101. package/docs/functions/index.later-1.html +3 -3
  102. package/docs/functions/index.mapAsync.html +3 -3
  103. package/docs/functions/index.mapValues.html +6 -6
  104. package/docs/functions/index.match.html +2 -2
  105. package/docs/functions/index.memoize.html +4 -4
  106. package/docs/functions/index.merge.html +6 -6
  107. package/docs/functions/index.mostFrequent.html +2 -2
  108. package/docs/functions/index.noop.html +2 -2
  109. package/docs/functions/index.occurrences.html +7 -7
  110. package/docs/functions/index.omit.html +5 -5
  111. package/docs/functions/index.pick.html +5 -5
  112. package/docs/functions/index.pull.html +2 -2
  113. package/docs/functions/index.remove.html +2 -2
  114. package/docs/functions/index.removeCommonProperties.html +5 -5
  115. package/docs/functions/index.replace.html +5 -5
  116. package/docs/functions/index.replaceDeep.html +3 -3
  117. package/docs/functions/index.rethrow.html +2 -2
  118. package/docs/functions/index.retry.html +4 -0
  119. package/docs/functions/index.round.html +5 -5
  120. package/docs/functions/index.safe.html +5 -5
  121. package/docs/functions/index.sample.html +4 -4
  122. package/docs/functions/index.samples.html +2 -2
  123. package/docs/functions/index.scale.html +2 -2
  124. package/docs/functions/index.seq.html +6 -6
  125. package/docs/functions/index.seqEarlyBreak.html +5 -5
  126. package/docs/functions/index.serialize.html +4 -4
  127. package/docs/functions/index.set.html +7 -7
  128. package/docs/functions/index.setImmutable.html +7 -7
  129. package/docs/functions/index.shuffle.html +2 -2
  130. package/docs/functions/index.sortBy.html +4 -4
  131. package/docs/functions/index.sortProps.html +4 -4
  132. package/docs/functions/index.stripPrefix.html +2 -2
  133. package/docs/functions/index.stripSuffix.html +2 -2
  134. package/docs/functions/index.throttle.html +3 -3
  135. package/docs/functions/index.toggle.html +2 -2
  136. package/docs/functions/index.trim.html +5 -5
  137. package/docs/functions/index.trimEnd.html +5 -5
  138. package/docs/functions/index.trimStart.html +5 -5
  139. package/docs/functions/index.truthy.html +3 -3
  140. package/docs/functions/index.unique.html +4 -4
  141. package/docs/functions/index.wait.html +2 -2
  142. package/docs/functions/index.waitFor.html +2 -2
  143. package/docs/functions/index.waitSync.html +2 -2
  144. package/docs/index.html +4 -3
  145. package/docs/interfaces/index.ComparePropsOptions.html +4 -4
  146. package/docs/interfaces/index.GetMultipleSource.html +3 -3
  147. package/docs/interfaces/index.GetSource.html +3 -3
  148. package/docs/interfaces/index.IsNumericStringOptions.html +2 -2
  149. package/docs/interfaces/index.OccurencesOptions.html +3 -3
  150. package/docs/interfaces/index.SetImmutableSource.html +3 -3
  151. package/docs/interfaces/index.SetSource.html +3 -3
  152. package/docs/interfaces/index.ThrottleOptions.html +4 -4
  153. package/docs/interfaces/index.ThrottledFunctionExtras.html +4 -4
  154. package/docs/modules/index.html +4 -3
  155. package/docs/modules.html +3 -0
  156. package/docs/types/index.CustomDeserializers.html +1 -1
  157. package/docs/types/index.CustomSerializers.html +1 -1
  158. package/docs/types/index.Later.html +4 -4
  159. package/docs/types/index.MapValuesFn.html +4 -4
  160. package/docs/types/index.MatchCallback.html +1 -1
  161. package/docs/types/index.MergeTwo.html +2 -2
  162. package/docs/types/index.SeqEarlyBreaker.html +3 -3
  163. package/docs/types/index.SeqFn.html +3 -3
  164. package/docs/types/index.SeqFunctions.html +3 -3
  165. package/docs/types/index.SetImmutablePath.html +2 -2
  166. package/docs/types/index.ThrottledFunction.html +1 -1
  167. package/docs/variables/index.mapValuesUNSET.html +2 -2
  168. package/docs/variables/index.mergeUNSET.html +3 -3
  169. package/esm/cap.d.ts.map +1 -1
  170. package/esm/capitalize.d.ts.map +1 -1
  171. package/esm/coalesce.d.ts.map +1 -1
  172. package/esm/compareArrays.d.ts.map +1 -1
  173. package/esm/compareProps.d.ts.map +1 -1
  174. package/esm/deserialize.d.ts.map +1 -1
  175. package/esm/ensureArray.d.ts.map +1 -1
  176. package/esm/ensureDate.d.ts.map +1 -1
  177. package/esm/ensureError.d.ts.map +1 -1
  178. package/esm/ensurePrefix.d.ts.map +1 -1
  179. package/esm/ensureSuffix.d.ts.map +1 -1
  180. package/esm/ensureTimestamp.d.ts.map +1 -1
  181. package/esm/escapeRegExp.d.ts.map +1 -1
  182. package/esm/formatDate.d.ts.map +1 -1
  183. package/esm/get.d.ts.map +1 -1
  184. package/esm/getMultiple.d.ts.map +1 -1
  185. package/esm/index.d.ts +1 -0
  186. package/esm/index.d.ts.map +1 -1
  187. package/esm/index.js +1 -0
  188. package/esm/index.js.map +1 -1
  189. package/esm/insertSeparator.d.ts.map +1 -1
  190. package/esm/isEmpty.d.ts.map +1 -1
  191. package/esm/isNumericString.d.ts.map +1 -1
  192. package/esm/isPlainObject.d.ts.map +1 -1
  193. package/esm/last.d.ts.map +1 -1
  194. package/esm/later.d.ts.map +1 -1
  195. package/esm/mapAsync.d.ts.map +1 -1
  196. package/esm/mapValues.d.ts.map +1 -1
  197. package/esm/match.d.ts.map +1 -1
  198. package/esm/memoize.d.ts.map +1 -1
  199. package/esm/mostFrequent.d.ts.map +1 -1
  200. package/esm/occurrences.d.ts.map +1 -1
  201. package/esm/omit.d.ts.map +1 -1
  202. package/esm/pick.d.ts.map +1 -1
  203. package/esm/pull.d.ts.map +1 -1
  204. package/esm/remove.d.ts.map +1 -1
  205. package/esm/removeCommonProperties.d.ts.map +1 -1
  206. package/esm/replace.d.ts.map +1 -1
  207. package/esm/replaceDeep.d.ts.map +1 -1
  208. package/esm/replaceDeepByFn.d.ts.map +1 -1
  209. package/esm/rethrow.d.ts.map +1 -1
  210. package/esm/retry.d.ts +9 -0
  211. package/esm/retry.d.ts.map +1 -0
  212. package/esm/retry.js +23 -0
  213. package/esm/retry.js.map +1 -0
  214. package/esm/round.d.ts.map +1 -1
  215. package/esm/sample.d.ts.map +1 -1
  216. package/esm/samples.d.ts.map +1 -1
  217. package/esm/scale.d.ts.map +1 -1
  218. package/esm/seq.d.ts.map +1 -1
  219. package/esm/serialize.d.ts.map +1 -1
  220. package/esm/set.d.ts.map +1 -1
  221. package/esm/setImmutable.d.ts.map +1 -1
  222. package/esm/shuffle.d.ts.map +1 -1
  223. package/esm/sortBy.d.ts.map +1 -1
  224. package/esm/sortProps.d.ts.map +1 -1
  225. package/esm/stripPrefix.d.ts.map +1 -1
  226. package/esm/stripSuffix.d.ts.map +1 -1
  227. package/esm/throttle.d.ts.map +1 -1
  228. package/esm/toggle.d.ts.map +1 -1
  229. package/esm/trim.d.ts.map +1 -1
  230. package/esm/trimEnd.d.ts.map +1 -1
  231. package/esm/trimStart.d.ts.map +1 -1
  232. package/esm/truthy.d.ts.map +1 -1
  233. package/esm/unique.d.ts.map +1 -1
  234. package/esm/wait.d.ts.map +1 -1
  235. package/esm/waitFor.d.ts.map +1 -1
  236. package/esm/waitSync.d.ts.map +1 -1
  237. package/package.json +1 -1
  238. package/pnpm-lock.yaml +4729 -3874
  239. package/src/get.spec.ts +1 -9
  240. package/src/index.ts +1 -0
  241. package/src/mapValues.spec.ts +1 -2
  242. 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-unsafe-assignment
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
@@ -37,6 +37,7 @@ export * from "./removeCommonProperties.js";
37
37
  export * from "./replace.js";
38
38
  export * from "./replaceDeep.js";
39
39
  export * from "./rethrow.js";
40
+ export * from "./retry.js";
40
41
  export * from "./round.js";
41
42
  export * from "./safe.js";
42
43
  export * from "./sample.js";
@@ -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/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