@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.
Files changed (236) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/dist/index.d.ts +7 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +7 -0
  5. package/dist/index.js.map +1 -1
  6. package/dist/omit.d.ts +1 -1
  7. package/dist/omit.d.ts.map +1 -1
  8. package/dist/omit.js.map +1 -1
  9. package/dist/replace.d.ts.map +1 -1
  10. package/dist/replace.js.map +1 -1
  11. package/dist/replaceDeep.d.ts +5 -1
  12. package/dist/replaceDeep.d.ts.map +1 -1
  13. package/dist/replaceDeep.js +3 -20
  14. package/dist/replaceDeep.js.map +1 -1
  15. package/dist/replaceDeepByFn.d.ts +7 -0
  16. package/dist/replaceDeepByFn.d.ts.map +1 -0
  17. package/dist/replaceDeepByFn.js +47 -0
  18. package/dist/replaceDeepByFn.js.map +1 -0
  19. package/dist/safe.d.ts.map +1 -1
  20. package/dist/safe.js.map +1 -1
  21. package/dist/sample.d.ts +3 -0
  22. package/dist/sample.d.ts.map +1 -0
  23. package/dist/sample.js +8 -0
  24. package/dist/sample.js.map +1 -0
  25. package/dist/samples.d.ts +3 -0
  26. package/dist/samples.d.ts.map +1 -0
  27. package/dist/samples.js +28 -0
  28. package/dist/samples.js.map +1 -0
  29. package/dist/serialize.d.ts.map +1 -1
  30. package/dist/serialize.js +56 -18
  31. package/dist/serialize.js.map +1 -1
  32. package/dist/shuffle.d.ts +3 -0
  33. package/dist/shuffle.d.ts.map +1 -0
  34. package/dist/shuffle.js +9 -0
  35. package/dist/shuffle.js.map +1 -0
  36. package/dist/toggle.d.ts +3 -0
  37. package/dist/toggle.d.ts.map +1 -0
  38. package/dist/toggle.js +15 -0
  39. package/dist/toggle.js.map +1 -0
  40. package/dist/trim.d.ts +3 -0
  41. package/dist/trim.d.ts.map +1 -0
  42. package/dist/trim.js +10 -0
  43. package/dist/trim.js.map +1 -0
  44. package/dist/trimEnd.d.ts +3 -0
  45. package/dist/trimEnd.d.ts.map +1 -0
  46. package/dist/trimEnd.js +12 -0
  47. package/dist/trimEnd.js.map +1 -0
  48. package/dist/trimStart.d.ts +3 -0
  49. package/dist/trimStart.d.ts.map +1 -0
  50. package/dist/trimStart.js +12 -0
  51. package/dist/trimStart.js.map +1 -0
  52. package/dist/utils/utils.d.ts +6 -0
  53. package/dist/utils/utils.d.ts.map +1 -0
  54. package/dist/utils/utils.js +10 -0
  55. package/dist/utils/utils.js.map +1 -0
  56. package/dist/waitFor.d.ts +7 -1
  57. package/dist/waitFor.d.ts.map +1 -1
  58. package/dist/waitFor.js +35 -10
  59. package/dist/waitFor.js.map +1 -1
  60. package/docs/assets/search.js +1 -1
  61. package/docs/functions/cap.html +12 -5
  62. package/docs/functions/capitalize.html +12 -5
  63. package/docs/functions/coalesce.html +12 -5
  64. package/docs/functions/compareArrays.html +12 -5
  65. package/docs/functions/compareProps.html +12 -5
  66. package/docs/functions/deserialize.html +12 -5
  67. package/docs/functions/ensureArray.html +12 -5
  68. package/docs/functions/ensureDate.html +12 -5
  69. package/docs/functions/ensureError.html +12 -5
  70. package/docs/functions/ensurePrefix.html +12 -5
  71. package/docs/functions/ensureSuffix.html +12 -5
  72. package/docs/functions/ensureTimestamp.html +12 -5
  73. package/docs/functions/escapeRegExp.html +12 -5
  74. package/docs/functions/formatDate.html +12 -5
  75. package/docs/functions/get.html +12 -5
  76. package/docs/functions/getMultiple.html +12 -5
  77. package/docs/functions/insertSeparator.html +12 -5
  78. package/docs/functions/isEmpty.html +12 -5
  79. package/docs/functions/isNumericString.html +12 -5
  80. package/docs/functions/isPlainObject.html +12 -5
  81. package/docs/functions/last.html +12 -5
  82. package/docs/functions/later-1.html +12 -5
  83. package/docs/functions/mapAsync.html +12 -5
  84. package/docs/functions/mapValues.html +12 -5
  85. package/docs/functions/match.html +12 -5
  86. package/docs/functions/merge.html +20 -13
  87. package/docs/functions/mostFrequent.html +12 -5
  88. package/docs/functions/noop.html +12 -5
  89. package/docs/functions/occurrences.html +12 -5
  90. package/docs/functions/omit.html +18 -7
  91. package/docs/functions/pick.html +13 -6
  92. package/docs/functions/pull.html +12 -5
  93. package/docs/functions/remove.html +12 -5
  94. package/docs/functions/removeCommonProperties.html +12 -5
  95. package/docs/functions/replace.html +12 -5
  96. package/docs/functions/replaceDeep.html +23 -7
  97. package/docs/functions/rethrow.html +12 -5
  98. package/docs/functions/round.html +12 -5
  99. package/docs/functions/safe.html +13 -6
  100. package/docs/functions/sample.html +149 -0
  101. package/docs/functions/samples.html +159 -0
  102. package/docs/functions/scale.html +12 -5
  103. package/docs/functions/seq.html +12 -5
  104. package/docs/functions/seqEarlyBreak.html +12 -5
  105. package/docs/functions/serialize.html +12 -5
  106. package/docs/functions/set.html +12 -5
  107. package/docs/functions/setImmutable.html +12 -5
  108. package/docs/functions/shuffle.html +149 -0
  109. package/docs/functions/sortBy.html +12 -5
  110. package/docs/functions/sortProps.html +12 -5
  111. package/docs/functions/stripPrefix.html +12 -5
  112. package/docs/functions/stripSuffix.html +12 -5
  113. package/docs/functions/throttle.html +12 -5
  114. package/docs/functions/toggle.html +154 -0
  115. package/docs/functions/trim.html +152 -0
  116. package/docs/functions/trimEnd.html +152 -0
  117. package/docs/functions/trimStart.html +152 -0
  118. package/docs/functions/truthy.html +12 -5
  119. package/docs/functions/unique.html +12 -5
  120. package/docs/functions/wait.html +12 -5
  121. package/docs/functions/waitFor.html +23 -18
  122. package/docs/functions/waitSync.html +12 -5
  123. package/docs/index.html +11 -4
  124. package/docs/interfaces/ComparePropsOptions.html +6 -6
  125. package/docs/interfaces/GetMultipleSource.html +12 -5
  126. package/docs/interfaces/GetSource.html +12 -5
  127. package/docs/interfaces/IsNumericStringOptions.html +9 -9
  128. package/docs/interfaces/OccurencesOptions.html +6 -6
  129. package/docs/interfaces/SetImmutableSource.html +12 -5
  130. package/docs/interfaces/SetSource.html +12 -5
  131. package/docs/interfaces/ThrottleOptions.html +7 -7
  132. package/docs/interfaces/ThrottledFunctionExtras.html +7 -7
  133. package/docs/modules.html +18 -4
  134. package/docs/pages/CHANGELOG.html +150 -61
  135. package/docs/pages/Introduction.html +11 -4
  136. package/docs/types/CustomDeserializers.html +12 -5
  137. package/docs/types/CustomSerializers.html +12 -5
  138. package/docs/types/Later.html +12 -5
  139. package/docs/types/MapValuesFn.html +12 -5
  140. package/docs/types/MatchCallback.html +12 -5
  141. package/docs/types/SeqEarlyBreaker.html +12 -5
  142. package/docs/types/SeqFn.html +12 -5
  143. package/docs/types/SeqFunctions.html +12 -5
  144. package/docs/types/SetImmutablePath.html +12 -5
  145. package/docs/types/ThrottledFunction.html +12 -5
  146. package/docs/variables/mapValuesUNSET.html +12 -5
  147. package/docs/variables/mergeUNSET.html +12 -5
  148. package/esm/index.d.ts +7 -0
  149. package/esm/index.d.ts.map +1 -1
  150. package/esm/index.js +7 -0
  151. package/esm/index.js.map +1 -1
  152. package/esm/omit.d.ts +1 -1
  153. package/esm/omit.d.ts.map +1 -1
  154. package/esm/omit.js.map +1 -1
  155. package/esm/replace.d.ts.map +1 -1
  156. package/esm/replace.js.map +1 -1
  157. package/esm/replaceDeep.d.ts +5 -1
  158. package/esm/replaceDeep.d.ts.map +1 -1
  159. package/esm/replaceDeep.js +3 -20
  160. package/esm/replaceDeep.js.map +1 -1
  161. package/esm/replaceDeepByFn.d.ts +7 -0
  162. package/esm/replaceDeepByFn.d.ts.map +1 -0
  163. package/esm/replaceDeepByFn.js +44 -0
  164. package/esm/replaceDeepByFn.js.map +1 -0
  165. package/esm/safe.d.ts.map +1 -1
  166. package/esm/safe.js.map +1 -1
  167. package/esm/sample.d.ts +3 -0
  168. package/esm/sample.d.ts.map +1 -0
  169. package/esm/sample.js +5 -0
  170. package/esm/sample.js.map +1 -0
  171. package/esm/samples.d.ts +3 -0
  172. package/esm/samples.d.ts.map +1 -0
  173. package/esm/samples.js +25 -0
  174. package/esm/samples.js.map +1 -0
  175. package/esm/serialize.d.ts.map +1 -1
  176. package/esm/serialize.js +56 -18
  177. package/esm/serialize.js.map +1 -1
  178. package/esm/shuffle.d.ts +3 -0
  179. package/esm/shuffle.d.ts.map +1 -0
  180. package/esm/shuffle.js +6 -0
  181. package/esm/shuffle.js.map +1 -0
  182. package/esm/toggle.d.ts +3 -0
  183. package/esm/toggle.d.ts.map +1 -0
  184. package/esm/toggle.js +12 -0
  185. package/esm/toggle.js.map +1 -0
  186. package/esm/trim.d.ts +3 -0
  187. package/esm/trim.d.ts.map +1 -0
  188. package/esm/trim.js +7 -0
  189. package/esm/trim.js.map +1 -0
  190. package/esm/trimEnd.d.ts +3 -0
  191. package/esm/trimEnd.d.ts.map +1 -0
  192. package/esm/trimEnd.js +9 -0
  193. package/esm/trimEnd.js.map +1 -0
  194. package/esm/trimStart.d.ts +3 -0
  195. package/esm/trimStart.d.ts.map +1 -0
  196. package/esm/trimStart.js +9 -0
  197. package/esm/trimStart.js.map +1 -0
  198. package/esm/utils/utils.d.ts +6 -0
  199. package/esm/utils/utils.d.ts.map +1 -0
  200. package/esm/utils/utils.js +7 -0
  201. package/esm/utils/utils.js.map +1 -0
  202. package/esm/waitFor.d.ts +7 -1
  203. package/esm/waitFor.d.ts.map +1 -1
  204. package/esm/waitFor.js +35 -10
  205. package/esm/waitFor.js.map +1 -1
  206. package/package.json +4 -4
  207. package/pnpm-lock.yaml +1162 -986
  208. package/src/deserialize.spec.ts +12 -0
  209. package/src/index.ts +7 -0
  210. package/src/omit.ts +8 -2
  211. package/src/pick.ts +1 -1
  212. package/src/replace.ts +0 -1
  213. package/src/replaceDeep.spec.ts +91 -0
  214. package/src/replaceDeep.ts +22 -27
  215. package/src/replaceDeepByFn.spec.ts +162 -0
  216. package/src/replaceDeepByFn.ts +93 -0
  217. package/src/safe.ts +0 -1
  218. package/src/sample.spec.ts +31 -0
  219. package/src/sample.ts +11 -0
  220. package/src/samples.spec.ts +50 -0
  221. package/src/samples.ts +41 -0
  222. package/src/serialize.spec.ts +42 -0
  223. package/src/serialize.ts +65 -18
  224. package/src/shuffle.spec.ts +39 -0
  225. package/src/shuffle.ts +13 -0
  226. package/src/toggle.spec.ts +43 -0
  227. package/src/toggle.ts +22 -0
  228. package/src/trim.spec.ts +22 -0
  229. package/src/trim.ts +23 -0
  230. package/src/trimEnd.spec.ts +20 -0
  231. package/src/trimEnd.ts +22 -0
  232. package/src/trimStart.spec.ts +20 -0
  233. package/src/trimStart.ts +22 -0
  234. package/src/utils/utils.ts +11 -0
  235. package/src/waitFor.spec.ts +141 -0
  236. 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 TInterval = ReturnType<typeof setInterval>;
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
- * truthy value.
9
- * Pro-tip: Value returned from the callback is returned via resolved Promise. If you want to pass back falsy value then
10
- * wrap your potential return value with an object or array.
11
- * @param {function} fn - callback function
12
- * @param {number} interval - interval between checks
13
- * @param {number} timeout - optional timeout
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, interval = DEFAULT_INTERVAL, timeout = Infinity) => {
42
+ const waitFor = <T>(fn: () => MaybePromise<T>, options: Options = defaultOptions) => {
16
43
  return new Promise<T>((resolve, reject) => {
17
- let intervalTimer: TInterval, failTimer: TTimeout;
44
+ let intervalTimer: TTimeout, failTimer: TTimeout;
18
45
 
19
- if (isFinite(timeout)) {
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
- clearInterval(intervalTimer);
23
- }, timeout);
55
+ clearTimeout(intervalTimer);
56
+ }, opts.timeout);
24
57
  }
25
58
 
26
- intervalTimer = setInterval(() => {
59
+ let tries = 0;
60
+
61
+ // eslint-disable-next-line max-statements
62
+ const tryFn = (async () => {
27
63
  try {
28
- const result = fn();
29
- if (result) {
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
- clearInterval(intervalTimer);
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
- clearInterval(intervalTimer);
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
- }, interval);
93
+ });
94
+ tryFn().catch(noop);
44
95
  });
45
96
  };
46
97