@ezez/utils 1.6.0 → 1.8.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 (140) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +2 -0
  3. package/dist/formatDate.d.ts +3 -0
  4. package/dist/formatDate.d.ts.map +1 -0
  5. package/dist/formatDate.js +55 -0
  6. package/dist/formatDate.js.map +1 -0
  7. package/dist/getMultiple.js.map +1 -1
  8. package/dist/index.d.ts +2 -0
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +2 -0
  11. package/dist/index.js.map +1 -1
  12. package/dist/isNumericString.js.map +1 -1
  13. package/dist/later.d.ts +9 -0
  14. package/dist/later.d.ts.map +1 -0
  15. package/dist/later.js +13 -0
  16. package/dist/later.js.map +1 -0
  17. package/dist/mapAsync.js.map +1 -1
  18. package/dist/mostFrequent.d.ts.map +1 -1
  19. package/dist/mostFrequent.js.map +1 -1
  20. package/dist/pull.js.map +1 -1
  21. package/dist/remove.js.map +1 -1
  22. package/dist/replace.js.map +1 -1
  23. package/dist/set.js.map +1 -1
  24. package/dist/setImmutable.js.map +1 -1
  25. package/dist/throttle.js.map +1 -1
  26. package/docs/assets/highlight.css +7 -0
  27. package/docs/assets/search.js +1 -1
  28. package/docs/functions/cap.html +9 -6
  29. package/docs/functions/capitalize.html +9 -6
  30. package/docs/functions/coalesce.html +9 -6
  31. package/docs/functions/compareArrays.html +9 -6
  32. package/docs/functions/compareProps.html +9 -6
  33. package/docs/functions/ensureArray.html +9 -6
  34. package/docs/functions/ensureDate.html +9 -6
  35. package/docs/functions/ensureError.html +9 -6
  36. package/docs/functions/ensurePrefix.html +9 -6
  37. package/docs/functions/ensureSuffix.html +9 -6
  38. package/docs/functions/ensureTimestamp.html +9 -6
  39. package/docs/functions/escapeRegExp.html +9 -6
  40. package/docs/functions/formatDate.html +136 -0
  41. package/docs/functions/get.html +9 -6
  42. package/docs/functions/getMultiple.html +9 -6
  43. package/docs/functions/insertSeparator.html +9 -6
  44. package/docs/functions/isEmpty.html +9 -6
  45. package/docs/functions/isNumericString.html +9 -6
  46. package/docs/functions/isPlainObject.html +9 -6
  47. package/docs/functions/last.html +9 -6
  48. package/docs/functions/later-1.html +133 -0
  49. package/docs/functions/mapAsync.html +9 -6
  50. package/docs/functions/mapValues.html +9 -6
  51. package/docs/functions/match.html +9 -6
  52. package/docs/functions/merge.html +17 -14
  53. package/docs/functions/mostFrequent.html +9 -6
  54. package/docs/functions/noop.html +9 -6
  55. package/docs/functions/occurrences.html +9 -6
  56. package/docs/functions/omit.html +9 -6
  57. package/docs/functions/pick.html +9 -6
  58. package/docs/functions/pull.html +9 -6
  59. package/docs/functions/remove.html +9 -6
  60. package/docs/functions/replace.html +9 -6
  61. package/docs/functions/rethrow.html +9 -6
  62. package/docs/functions/safe.html +10 -7
  63. package/docs/functions/scale.html +9 -6
  64. package/docs/functions/seq.html +9 -6
  65. package/docs/functions/seqEarlyBreak.html +9 -6
  66. package/docs/functions/set.html +9 -6
  67. package/docs/functions/setImmutable.html +9 -6
  68. package/docs/functions/sortBy.html +9 -6
  69. package/docs/functions/sortProps.html +9 -6
  70. package/docs/functions/stripPrefix.html +9 -6
  71. package/docs/functions/stripSuffix.html +9 -6
  72. package/docs/functions/throttle.html +9 -6
  73. package/docs/functions/truthy.html +9 -6
  74. package/docs/functions/unique.html +9 -6
  75. package/docs/functions/wait.html +9 -6
  76. package/docs/functions/waitFor.html +9 -6
  77. package/docs/functions/waitSync.html +9 -6
  78. package/docs/index.html +10 -5
  79. package/docs/interfaces/ComparePropsOptions.html +6 -6
  80. package/docs/interfaces/GetMultipleSource.html +9 -6
  81. package/docs/interfaces/GetSource.html +9 -6
  82. package/docs/interfaces/IsNumericStringOptions.html +9 -9
  83. package/docs/interfaces/OccurencesOptions.html +6 -6
  84. package/docs/interfaces/SetImmutableSource.html +9 -6
  85. package/docs/interfaces/SetSource.html +9 -6
  86. package/docs/interfaces/ThrottleOptions.html +7 -7
  87. package/docs/interfaces/ThrottledFunctionExtras.html +7 -7
  88. package/docs/modules.html +11 -5
  89. package/docs/pages/CHANGELOG.html +70 -38
  90. package/docs/pages/Introduction.html +8 -5
  91. package/docs/types/Later.html +159 -0
  92. package/docs/types/MapValuesFn.html +9 -6
  93. package/docs/types/MatchCallback.html +9 -6
  94. package/docs/types/SeqEarlyBreaker.html +9 -6
  95. package/docs/types/SeqFn.html +9 -6
  96. package/docs/types/SeqFunctions.html +9 -6
  97. package/docs/types/SetImmutablePath.html +9 -6
  98. package/docs/types/ThrottledFunction.html +9 -6
  99. package/docs/variables/mapValuesUNSET.html +9 -6
  100. package/docs/variables/mergeUNSET.html +9 -6
  101. package/esm/formatDate.d.ts +3 -0
  102. package/esm/formatDate.d.ts.map +1 -0
  103. package/esm/formatDate.js +52 -0
  104. package/esm/formatDate.js.map +1 -0
  105. package/esm/getMultiple.js.map +1 -1
  106. package/esm/index.d.ts +2 -0
  107. package/esm/index.d.ts.map +1 -1
  108. package/esm/index.js +2 -0
  109. package/esm/index.js.map +1 -1
  110. package/esm/isNumericString.js.map +1 -1
  111. package/esm/later.d.ts +9 -0
  112. package/esm/later.d.ts.map +1 -0
  113. package/esm/later.js +10 -0
  114. package/esm/later.js.map +1 -0
  115. package/esm/mapAsync.js.map +1 -1
  116. package/esm/mostFrequent.d.ts.map +1 -1
  117. package/esm/mostFrequent.js.map +1 -1
  118. package/esm/pull.js.map +1 -1
  119. package/esm/remove.js.map +1 -1
  120. package/esm/replace.js.map +1 -1
  121. package/esm/set.js.map +1 -1
  122. package/esm/setImmutable.js.map +1 -1
  123. package/esm/throttle.js.map +1 -1
  124. package/package.json +9 -8
  125. package/src/formatDate.spec.ts +199 -0
  126. package/src/formatDate.ts +73 -0
  127. package/src/getMultiple.ts +1 -1
  128. package/src/index.ts +2 -0
  129. package/src/isNumericString.ts +1 -1
  130. package/src/later.spec.ts +33 -0
  131. package/src/later.ts +42 -0
  132. package/src/mapAsync.ts +1 -1
  133. package/src/mostFrequent.ts +1 -0
  134. package/src/pull.ts +1 -1
  135. package/src/remove.ts +2 -2
  136. package/src/replace.ts +1 -1
  137. package/src/safe.spec.ts +0 -1
  138. package/src/set.ts +1 -1
  139. package/src/setImmutable.ts +1 -1
  140. package/src/throttle.ts +1 -1
@@ -0,0 +1,33 @@
1
+ import { later } from "./later.js";
2
+
3
+ describe("later", () => {
4
+ it("creates values synchronously", async () => {
5
+ const l = later();
6
+ l.must.have.property("promise");
7
+ l.must.have.property("resolve");
8
+ l.must.have.property("reject");
9
+ });
10
+
11
+ it("creates expected types", async () => {
12
+ const l = later();
13
+
14
+ l.promise.must.be.instanceof(Promise);
15
+ l.resolve.must.be.a.function();
16
+ l.reject.must.be.a.function();
17
+ });
18
+
19
+ it("resolves promise", async () => {
20
+ const l = later();
21
+ l.resolve(42);
22
+
23
+ const r = await l.promise;
24
+ r.must.equal(42);
25
+ });
26
+
27
+ it("rejects promise", async () => {
28
+ const l = later();
29
+ l.reject(new Error("foo"));
30
+
31
+ await l.promise.must.reject.with.error("foo");
32
+ });
33
+ });
package/src/later.ts ADDED
@@ -0,0 +1,42 @@
1
+ type Later<T> = {
2
+ /**
3
+ * The promise that will be resolved or rejected later.
4
+ */
5
+ promise: Promise<T>;
6
+ /**
7
+ * Promise resolver function
8
+ */
9
+ resolve: (result: T) => void;
10
+ /**
11
+ * Promise rejecter function
12
+ */
13
+ reject: (reason: Error) => void;
14
+ };
15
+
16
+ /**
17
+ * Creates a promise that can be resolved or rejected from outside the promise.
18
+ *
19
+ * @example In this example someFunction will return a promise that's typed to return number and myDependency will
20
+ * resolve it when it's ready.
21
+ * ```
22
+ * function someFunction() {
23
+ * const l = later<number>();
24
+ * myDependency.load(l);
25
+ * return l.promise;
26
+ * }
27
+ * ```
28
+ */
29
+ const later = <T>(): Later<T> => {
30
+ // @ts-expect-error This is expected - we fill the values later (no pun intended)
31
+ const res: Later<T> = {};
32
+
33
+ res.promise = new Promise((resolve, reject) => {
34
+ res.resolve = resolve;
35
+ res.reject = reject;
36
+ });
37
+
38
+ return res;
39
+ };
40
+
41
+ export type { Later };
42
+ export { later };
package/src/mapAsync.ts CHANGED
@@ -24,7 +24,7 @@ const mapAsync = async <T, Y>(
24
24
  const l = context.length;
25
25
 
26
26
  for (let i = 0; i < l; i++) {
27
- const item = context[i];
27
+ const item = context[i]!;
28
28
  // eslint-disable-next-line callback-return
29
29
  result.push(callback(item, i, context));
30
30
  if (Date.now() - lastWaitMoment >= pauseEvery) {
@@ -19,6 +19,7 @@ const mostFrequent = <T>(array: T[]): T => {
19
19
  }
20
20
  });
21
21
 
22
+ // @ts-expect-error - idk if there is a good workaround for this with `noUncheckedIndexedAccess`
22
23
  return topValue;
23
24
  };
24
25
 
package/src/pull.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  const pull = <T>(array: T[], ...valuesToPull: T[]): T[] => {
7
7
  const len = array.length;
8
8
  for (let i = len - 1; i >= 0; i--) {
9
- if (valuesToPull.includes(array[i])) {
9
+ if (valuesToPull.includes(array[i]!)) {
10
10
  array.splice(i, 1);
11
11
  }
12
12
  }
package/src/remove.ts CHANGED
@@ -12,9 +12,9 @@ const remove = <T>(array: T[], fn: (elem: T, num: number, list: T[]) => boolean)
12
12
  const removedValues = [];
13
13
 
14
14
  for (let counter = 0; counter < len; counter++) {
15
- if (fn(array[counter], counter, array)) {
15
+ if (fn(array[counter]!, counter, array)) {
16
16
  idsToRemove.push(counter - idsToRemove.length);
17
- removedValues.push(array[counter]);
17
+ removedValues.push(array[counter]!);
18
18
  }
19
19
  }
20
20
 
package/src/replace.ts CHANGED
@@ -11,7 +11,7 @@ import { escapeRegExp } from "./escapeRegExp.js";
11
11
  const replace = (source: string, replaceMap: Record<string, string>) => {
12
12
  /* eslint-enable max-len */
13
13
  const regex = new RegExp(Object.keys(replaceMap).map(escapeRegExp).join("|"), "g");
14
- return source.replace(regex, (matched) => replaceMap[matched]);
14
+ return source.replace(regex, (matched) => replaceMap[matched]!);
15
15
  };
16
16
 
17
17
  export {
package/src/safe.spec.ts CHANGED
@@ -40,7 +40,6 @@ describe("safe", () => {
40
40
  });
41
41
 
42
42
  it("doesn't 'work' with promises", () => {
43
- // eslint-disable-next-line @typescript-eslint/require-await
44
43
  const result = safe(async () => {
45
44
  throw new Error("Boo!!!");
46
45
  });
package/src/set.ts CHANGED
@@ -39,7 +39,7 @@ const set = (source: Source, path: string | string[], value: unknown): Source |
39
39
  let current: Source | unknown = result;
40
40
  for (let i = 0; i < len; i++) {
41
41
  const isLast = i === len - 1;
42
- const key = pathParts[i];
42
+ const key = pathParts[i]!;
43
43
  if (isLast) {
44
44
  (current as Source)[key] = value;
45
45
  return result;
@@ -80,7 +80,7 @@ const setImmutable = (source: Source, path: Path, value: unknown): Source | unkn
80
80
  let current: Source | unknown = result;
81
81
  for (let i = 0; i < len; i++) {
82
82
  const isLast = i === len - 1;
83
- const key = pathParts[i];
83
+ const key = pathParts[i]!;
84
84
  if (isLast) {
85
85
  (current as Source)[key] = value;
86
86
  return result;
package/src/throttle.ts CHANGED
@@ -61,7 +61,7 @@ const throttle = <RT, F extends (...args: any[]) => RT>( // eslint-disable-line
61
61
  throw new TypeError("`time` must be an array with at least one number.");
62
62
  }
63
63
 
64
- const finalTime = typeof time === "number" ? time : time[time.length - 1];
64
+ const finalTime = typeof time === "number" ? time : time[time.length - 1]!;
65
65
 
66
66
  let lastRun = 0,
67
67
  timeoutId: ReturnType<typeof setTimeout> | null = null,