@ls-stack/utils 3.17.1 → 3.19.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 (114) hide show
  1. package/README.md +1 -15
  2. package/docs/README.md +72 -0
  3. package/docs/_media/modules.md +52 -0
  4. package/docs/arrayUtils/-internal-.md +17 -0
  5. package/docs/arrayUtils/README.md +423 -0
  6. package/docs/assertions/-internal-.md +63 -0
  7. package/docs/assertions/README.md +565 -0
  8. package/docs/asyncQueue/-internal-.md +815 -0
  9. package/docs/asyncQueue/README.md +75 -0
  10. package/docs/awaitDebounce.md +66 -0
  11. package/docs/cache/-internal-.md +168 -0
  12. package/docs/cache/README.md +360 -0
  13. package/docs/castValues.md +47 -0
  14. package/docs/concurrentCalls/-internal-.md +416 -0
  15. package/docs/concurrentCalls/README.md +77 -0
  16. package/docs/consoleFmt.md +91 -0
  17. package/docs/conversions.md +27 -0
  18. package/docs/createThrottleController/-internal-.md +73 -0
  19. package/docs/createThrottleController/README.md +31 -0
  20. package/docs/debounce.md +188 -0
  21. package/docs/dedent.md +117 -0
  22. package/docs/deepEqual.md +94 -0
  23. package/docs/enhancedMap.md +358 -0
  24. package/docs/exhaustiveMatch/-internal-.md +39 -0
  25. package/docs/exhaustiveMatch/README.md +146 -0
  26. package/docs/getAutoIncrementId.md +93 -0
  27. package/docs/getCompositeKey.md +39 -0
  28. package/docs/getValueStableKey.md +57 -0
  29. package/docs/hash.md +31 -0
  30. package/docs/interpolate/-internal-.md +61 -0
  31. package/docs/interpolate/README.md +62 -0
  32. package/docs/levenshtein.md +93 -0
  33. package/docs/main.md +21 -0
  34. package/docs/mathUtils.md +137 -0
  35. package/docs/modules.md +52 -0
  36. package/docs/objUtils.md +237 -0
  37. package/docs/parallelAsyncCalls/-internal-.md +347 -0
  38. package/docs/parallelAsyncCalls/README.md +45 -0
  39. package/docs/promiseUtils/-internal-.md +69 -0
  40. package/docs/promiseUtils/README.md +31 -0
  41. package/docs/retryOnError.md +67 -0
  42. package/docs/runShellCmd/-internal-.md +111 -0
  43. package/docs/runShellCmd/README.md +201 -0
  44. package/docs/safeJson.md +51 -0
  45. package/docs/saferTyping.md +228 -0
  46. package/docs/serializeXML.md +100 -0
  47. package/docs/shallowEqual.md +33 -0
  48. package/docs/sleep.md +27 -0
  49. package/docs/stringUtils/-internal-.md +17 -0
  50. package/docs/stringUtils/README.md +166 -0
  51. package/docs/testUtils.md +315 -0
  52. package/docs/throttle/-internal-.md +47 -0
  53. package/docs/throttle/README.md +178 -0
  54. package/docs/time.md +274 -0
  55. package/docs/timers.md +256 -0
  56. package/docs/tsResult/-internal-.md +327 -0
  57. package/docs/tsResult/README.md +696 -0
  58. package/docs/typeGuards.md +399 -0
  59. package/docs/typingFnUtils/-internal-.md +27 -0
  60. package/docs/typingFnUtils/README.md +293 -0
  61. package/docs/typingTestUtils.md +172 -0
  62. package/docs/typingUtils.md +111 -0
  63. package/docs/yamlStringify.md +45 -0
  64. package/lib/arrayUtils.js +3 -3
  65. package/lib/assertions.js +2 -2
  66. package/lib/awaitDebounce.cjs +106 -0
  67. package/lib/awaitDebounce.d.cts +38 -0
  68. package/lib/awaitDebounce.d.ts +38 -0
  69. package/lib/awaitDebounce.js +28 -0
  70. package/lib/cache.js +2 -2
  71. package/lib/{chunk-NH2LCAQS.js → chunk-6FIBVC2P.js} +1 -1
  72. package/lib/{chunk-GKOTKAIV.js → chunk-7CQPOM5I.js} +1 -1
  73. package/lib/{chunk-WS4WEVHU.js → chunk-C2SVCIWE.js} +1 -1
  74. package/lib/{chunk-SSKW673U.js → chunk-JF2MDHOJ.js} +5 -1
  75. package/lib/chunk-NW5H5EW7.js +100 -0
  76. package/lib/{chunk-DMW5Q4T2.js → chunk-SRVMMYSW.js} +1 -1
  77. package/lib/concurrentCalls.js +3 -3
  78. package/lib/createThrottleController.js +3 -3
  79. package/lib/debounce.js +4 -95
  80. package/lib/enhancedMap.js +3 -3
  81. package/lib/getAutoIncrementId.cjs +44 -0
  82. package/lib/getAutoIncrementId.d.cts +44 -0
  83. package/lib/getAutoIncrementId.d.ts +44 -0
  84. package/lib/getAutoIncrementId.js +18 -0
  85. package/lib/getCompositeKey.js +3 -3
  86. package/lib/getValueStableKey.js +3 -3
  87. package/lib/interpolate.js +2 -2
  88. package/lib/parallelAsyncCalls.js +2 -2
  89. package/lib/runShellCmd.d.cts +50 -0
  90. package/lib/runShellCmd.d.ts +50 -0
  91. package/lib/serializeXML.cjs +7 -2
  92. package/lib/serializeXML.d.cts +1 -1
  93. package/lib/serializeXML.d.ts +1 -1
  94. package/lib/serializeXML.js +7 -5
  95. package/lib/testUtils.js +3 -3
  96. package/lib/throttle.cjs +250 -0
  97. package/lib/throttle.d.cts +89 -0
  98. package/lib/throttle.d.ts +89 -0
  99. package/lib/throttle.js +38 -0
  100. package/lib/timers.cjs +93 -0
  101. package/lib/timers.d.cts +110 -0
  102. package/lib/timers.d.ts +110 -0
  103. package/lib/timers.js +65 -0
  104. package/lib/tsResult.js +2 -2
  105. package/lib/typeGuards.cjs +7 -2
  106. package/lib/typeGuards.d.cts +2 -1
  107. package/lib/typeGuards.d.ts +2 -1
  108. package/lib/typeGuards.js +5 -3
  109. package/lib/typingFnUtils.cjs +5 -1
  110. package/lib/typingFnUtils.d.cts +1 -1
  111. package/lib/typingFnUtils.d.ts +1 -1
  112. package/lib/typingFnUtils.js +5 -1
  113. package/lib/yamlStringify.js +5 -5
  114. package/package.json +26 -12
@@ -0,0 +1,110 @@
1
+ type CleanupTimer = () => void;
2
+ /**
3
+ * Creates a timeout with automatic cleanup capability.
4
+ *
5
+ * Returns a cleanup function that can be called to cancel the timeout.
6
+ * The cleanup function is idempotent - calling it multiple times is safe.
7
+ *
8
+ * @param ms - The timeout duration in milliseconds
9
+ * @param callback - The function to execute when the timeout completes
10
+ * @returns A cleanup function that cancels the timeout when called
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const cleanup = createTimeout(1000, () => {
15
+ * console.log('Timeout completed');
16
+ * });
17
+ *
18
+ * // Cancel the timeout before it completes
19
+ * cleanup();
20
+ * ```
21
+ */
22
+ declare function createTimeout(ms: number, callback: () => void): CleanupTimer;
23
+ /**
24
+ * Creates an interval with automatic cleanup capability.
25
+ *
26
+ * Returns a cleanup function that can be called to cancel the interval.
27
+ * The cleanup function is idempotent - calling it multiple times is safe.
28
+ *
29
+ * @param ms - The interval duration in milliseconds
30
+ * @param callback - The function to execute on each interval tick
31
+ * @returns A cleanup function that cancels the interval when called
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * const cleanup = createInterval(1000, () => {
36
+ * console.log('Interval tick');
37
+ * });
38
+ *
39
+ * // Stop the interval
40
+ * cleanup();
41
+ * ```
42
+ */
43
+ declare function createInterval(ms: number, callback: () => void): CleanupTimer;
44
+ /**
45
+ * Creates a timeout that prevents concurrent executions.
46
+ *
47
+ * Each call to the `call` function will cancel any previous pending timeout
48
+ * and start a new one. This is useful for debouncing or ensuring only the
49
+ * last call executes after a delay.
50
+ *
51
+ * @param ms - The timeout duration in milliseconds
52
+ * @param callback - The function to execute when the timeout completes
53
+ * @returns An object with `call` to trigger the timeout and `clean` to cancel it
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * const { call, clean } = createDebouncedTimeout(1000, () => {
58
+ * console.log('Only the last call executes');
59
+ * });
60
+ *
61
+ * call(); // This will be cancelled
62
+ * call(); // This will be cancelled
63
+ * call(); // Only this one will execute after 1000ms
64
+ *
65
+ * // Or cancel all pending timeouts
66
+ * clean();
67
+ * ```
68
+ */
69
+ declare function createDebouncedTimeout(ms: number, callback: () => void): {
70
+ call: () => void;
71
+ clean: CleanupTimer;
72
+ };
73
+ /**
74
+ * Creates a timeout that waits for a condition to become true.
75
+ *
76
+ * Polls the condition function at regular intervals until it returns a truthy value,
77
+ * then calls the callback with that value. If the condition doesn't become true
78
+ * within the maximum wait time, the timeout expires without calling the callback.
79
+ *
80
+ * @template T - The type of value returned by the condition function when true
81
+ * @param options - Configuration options
82
+ * @param options.condition - Function that returns false or a truthy value when the condition is met
83
+ * @param options.maxWaitMs - Maximum time to wait for the condition in milliseconds
84
+ * @param options.callback - Function to call when the condition becomes true
85
+ * @param options.checkIntervalMs - How often to check the condition in milliseconds (default: 20)
86
+ * @returns A cleanup function that cancels the condition timeout
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * const cleanup = createWaitUntil({
91
+ * condition: () => document.getElementById('myElement'),
92
+ * maxWaitMs: 5000,
93
+ * callback: (element) => {
94
+ * console.log('Element found:', element);
95
+ * },
96
+ * checkIntervalMs: 50
97
+ * });
98
+ *
99
+ * // Cancel the condition check
100
+ * cleanup();
101
+ * ```
102
+ */
103
+ declare function createWaitUntil<T extends NonNullable<unknown>>({ condition, maxWaitMs, callback, checkIntervalMs, }: {
104
+ condition: () => false | T;
105
+ maxWaitMs: number;
106
+ callback: (value: T) => void;
107
+ checkIntervalMs?: number;
108
+ }): CleanupTimer;
109
+
110
+ export { type CleanupTimer, createDebouncedTimeout, createInterval, createTimeout, createWaitUntil };
package/lib/timers.js ADDED
@@ -0,0 +1,65 @@
1
+ // src/timers.ts
2
+ function createTimeout(ms, callback) {
3
+ const timeoutId = setTimeout(callback, ms);
4
+ let isCleaned = false;
5
+ return () => {
6
+ if (isCleaned) return;
7
+ clearTimeout(timeoutId);
8
+ isCleaned = true;
9
+ };
10
+ }
11
+ function createInterval(ms, callback) {
12
+ const intervalId = setInterval(callback, ms);
13
+ let isCleaned = false;
14
+ return () => {
15
+ if (isCleaned) return;
16
+ clearInterval(intervalId);
17
+ isCleaned = true;
18
+ };
19
+ }
20
+ function createDebouncedTimeout(ms, callback) {
21
+ let cleanupTimer = null;
22
+ return {
23
+ clean: () => {
24
+ cleanupTimer?.();
25
+ },
26
+ call: () => {
27
+ cleanupTimer?.();
28
+ cleanupTimer = createTimeout(ms, () => {
29
+ callback();
30
+ });
31
+ }
32
+ };
33
+ }
34
+ function createWaitUntil({
35
+ condition,
36
+ maxWaitMs,
37
+ callback,
38
+ checkIntervalMs = 20
39
+ }) {
40
+ let cleanCheckTimeout = null;
41
+ let cleanMaxWaitTimeout = null;
42
+ cleanMaxWaitTimeout = createTimeout(maxWaitMs, () => {
43
+ cleanCheckTimeout?.();
44
+ });
45
+ function check() {
46
+ const result = condition();
47
+ if (result) {
48
+ cleanMaxWaitTimeout?.();
49
+ callback(result);
50
+ } else {
51
+ cleanCheckTimeout = createTimeout(checkIntervalMs, check);
52
+ }
53
+ }
54
+ check();
55
+ return () => {
56
+ cleanMaxWaitTimeout();
57
+ cleanCheckTimeout?.();
58
+ };
59
+ }
60
+ export {
61
+ createDebouncedTimeout,
62
+ createInterval,
63
+ createTimeout,
64
+ createWaitUntil
65
+ };
package/lib/tsResult.js CHANGED
@@ -5,8 +5,8 @@ import {
5
5
  isFunction,
6
6
  isObject,
7
7
  isPromise
8
- } from "./chunk-WS4WEVHU.js";
9
- import "./chunk-SSKW673U.js";
8
+ } from "./chunk-C2SVCIWE.js";
9
+ import "./chunk-JF2MDHOJ.js";
10
10
 
11
11
  // src/tsResult.ts
12
12
  function okUnwrapOr() {
@@ -25,7 +25,8 @@ __export(typeGuards_exports, {
25
25
  isNonEmptyArray: () => isNonEmptyArray,
26
26
  isObject: () => isObject,
27
27
  isPlainObject: () => isPlainObject,
28
- isPromise: () => isPromise
28
+ isPromise: () => isPromise,
29
+ isTruthy: () => isTruthy
29
30
  });
30
31
  module.exports = __toCommonJS(typeGuards_exports);
31
32
  function isObject(value) {
@@ -54,6 +55,9 @@ function isNonEmptyArray(value) {
54
55
  function arrayHasAtLeastXItems(array, minLength) {
55
56
  return array.length >= minLength;
56
57
  }
58
+ function isTruthy(value) {
59
+ return !!value;
60
+ }
57
61
  // Annotate the CommonJS export names for ESM import in node:
58
62
  0 && (module.exports = {
59
63
  arrayHasAtLeastXItems,
@@ -61,5 +65,6 @@ function arrayHasAtLeastXItems(array, minLength) {
61
65
  isNonEmptyArray,
62
66
  isObject,
63
67
  isPlainObject,
64
- isPromise
68
+ isPromise,
69
+ isTruthy
65
70
  });
@@ -105,5 +105,6 @@ declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 2): array is [T
105
105
  declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 3): array is [T, T, T, ...T[]];
106
106
  declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 4): array is [T, T, T, T, ...T[]];
107
107
  declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 5): array is [T, T, T, T, T, ...T[]];
108
+ declare function isTruthy<T>(value: T): value is Exclude<T, null | undefined | false | 0 | '' | 0n>;
108
109
 
109
- export { type NonEmptyArray, arrayHasAtLeastXItems, isFunction, isNonEmptyArray, isObject, isPlainObject, isPromise };
110
+ export { type NonEmptyArray, arrayHasAtLeastXItems, isFunction, isNonEmptyArray, isObject, isPlainObject, isPromise, isTruthy };
@@ -105,5 +105,6 @@ declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 2): array is [T
105
105
  declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 3): array is [T, T, T, ...T[]];
106
106
  declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 4): array is [T, T, T, T, ...T[]];
107
107
  declare function arrayHasAtLeastXItems<T>(array: T[], minLength: 5): array is [T, T, T, T, T, ...T[]];
108
+ declare function isTruthy<T>(value: T): value is Exclude<T, null | undefined | false | 0 | '' | 0n>;
108
109
 
109
- export { type NonEmptyArray, arrayHasAtLeastXItems, isFunction, isNonEmptyArray, isObject, isPlainObject, isPromise };
110
+ export { type NonEmptyArray, arrayHasAtLeastXItems, isFunction, isNonEmptyArray, isObject, isPlainObject, isPromise, isTruthy };
package/lib/typeGuards.js CHANGED
@@ -4,13 +4,15 @@ import {
4
4
  isNonEmptyArray,
5
5
  isObject,
6
6
  isPlainObject,
7
- isPromise
8
- } from "./chunk-SSKW673U.js";
7
+ isPromise,
8
+ isTruthy
9
+ } from "./chunk-JF2MDHOJ.js";
9
10
  export {
10
11
  arrayHasAtLeastXItems,
11
12
  isFunction,
12
13
  isNonEmptyArray,
13
14
  isObject,
14
15
  isPlainObject,
15
- isPromise
16
+ isPromise,
17
+ isTruthy
16
18
  };
@@ -44,7 +44,11 @@ function asType(value) {
44
44
  return value;
45
45
  }
46
46
  function narrowStringToUnion(key, union) {
47
- if (key && union.includes(key)) {
47
+ if (!key) return void 0;
48
+ if (union instanceof Set) {
49
+ return union.has(key) ? key : void 0;
50
+ }
51
+ if (union.includes(key)) {
48
52
  return key;
49
53
  }
50
54
  return void 0;
@@ -10,7 +10,7 @@ declare function typedObjectKeys<T extends Record<string, unknown>>(obj: T): (ke
10
10
  /** a safe way to cast types, use to substitute the `as Type` */
11
11
  declare function asType<T = unknown>(value: T): T;
12
12
  /** narrow a string to a union of strings */
13
- declare function narrowStringToUnion<const T extends string>(key: string | undefined | null, union: T[]): T | undefined;
13
+ declare function narrowStringToUnion<const T extends string>(key: string | undefined | null, union: T[] | readonly T[] | Set<T>): T | undefined;
14
14
  /**
15
15
  * Type helper to check if a type is a subtype of another type.
16
16
  *
@@ -10,7 +10,7 @@ declare function typedObjectKeys<T extends Record<string, unknown>>(obj: T): (ke
10
10
  /** a safe way to cast types, use to substitute the `as Type` */
11
11
  declare function asType<T = unknown>(value: T): T;
12
12
  /** narrow a string to a union of strings */
13
- declare function narrowStringToUnion<const T extends string>(key: string | undefined | null, union: T[]): T | undefined;
13
+ declare function narrowStringToUnion<const T extends string>(key: string | undefined | null, union: T[] | readonly T[] | Set<T>): T | undefined;
14
14
  /**
15
15
  * Type helper to check if a type is a subtype of another type.
16
16
  *
@@ -12,7 +12,11 @@ function asType(value) {
12
12
  return value;
13
13
  }
14
14
  function narrowStringToUnion(key, union) {
15
- if (key && union.includes(key)) {
15
+ if (!key) return void 0;
16
+ if (union instanceof Set) {
17
+ return union.has(key) ? key : void 0;
18
+ }
19
+ if (union.includes(key)) {
16
20
  return key;
17
21
  }
18
22
  return void 0;
@@ -1,14 +1,14 @@
1
+ import {
2
+ bytesToHumanReadable
3
+ } from "./chunk-IATIXMCE.js";
1
4
  import {
2
5
  truncateString
3
6
  } from "./chunk-4REIIZQY.js";
4
7
  import {
5
8
  isObject,
6
9
  isPlainObject
7
- } from "./chunk-WS4WEVHU.js";
8
- import "./chunk-SSKW673U.js";
9
- import {
10
- bytesToHumanReadable
11
- } from "./chunk-IATIXMCE.js";
10
+ } from "./chunk-C2SVCIWE.js";
11
+ import "./chunk-JF2MDHOJ.js";
12
12
 
13
13
  // src/yamlStringify.ts
14
14
  function yamlStringify(obj, {
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
- "description": "Typescript utils",
4
- "version": "3.17.1",
3
+ "description": "Universal TypeScript utilities for browser and Node.js",
4
+ "version": "3.19.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
- "lib"
7
+ "lib",
8
+ "docs"
8
9
  ],
9
10
  "repository": "github:lucasols/utils",
11
+ "homepage": "https://github.com/lucasols/utils/tree/main/packages/utils",
10
12
  "author": "Lucas Santos",
11
13
  "type": "module",
12
14
  "sideEffects": false,
@@ -34,6 +36,10 @@
34
36
  "import": "./lib/asyncQueue.js",
35
37
  "require": "./lib/asyncQueue.cjs"
36
38
  },
39
+ "./awaitDebounce": {
40
+ "import": "./lib/awaitDebounce.js",
41
+ "require": "./lib/awaitDebounce.cjs"
42
+ },
37
43
  "./cache": {
38
44
  "import": "./lib/cache.js",
39
45
  "require": "./lib/cache.cjs"
@@ -78,6 +84,10 @@
78
84
  "import": "./lib/exhaustiveMatch.js",
79
85
  "require": "./lib/exhaustiveMatch.cjs"
80
86
  },
87
+ "./getAutoIncrementId": {
88
+ "import": "./lib/getAutoIncrementId.js",
89
+ "require": "./lib/getAutoIncrementId.cjs"
90
+ },
81
91
  "./getCompositeKey": {
82
92
  "import": "./lib/getCompositeKey.js",
83
93
  "require": "./lib/getCompositeKey.cjs"
@@ -150,10 +160,18 @@
150
160
  "import": "./lib/testUtils.js",
151
161
  "require": "./lib/testUtils.cjs"
152
162
  },
163
+ "./throttle": {
164
+ "import": "./lib/throttle.js",
165
+ "require": "./lib/throttle.cjs"
166
+ },
153
167
  "./time": {
154
168
  "import": "./lib/time.js",
155
169
  "require": "./lib/time.cjs"
156
170
  },
171
+ "./timers": {
172
+ "import": "./lib/timers.js",
173
+ "require": "./lib/timers.cjs"
174
+ },
157
175
  "./tsResult": {
158
176
  "import": "./lib/tsResult.js",
159
177
  "require": "./lib/tsResult.cjs"
@@ -183,6 +201,7 @@
183
201
  "node": ">=20.0.0"
184
202
  },
185
203
  "devDependencies": {
204
+ "@eslint/js": "^9.18.0",
186
205
  "@ls-stack/extended-lint": "^0.20.1",
187
206
  "@types/eslint": "^9.6.1",
188
207
  "@types/eslint__js": "^8.42.3",
@@ -217,16 +236,11 @@
217
236
  "lint": "pnpm tsc && pnpm eslint",
218
237
  "tsc": "tsc -p tsconfig.prod.json",
219
238
  "tsc:watch": "tsc -p tsconfig.prod.json --watch",
220
- "eslint": "CI=true eslint src/ scripts/ --color --max-warnings=0",
221
- "build": "pnpm test && pnpm lint && pnpm build:no-test && pnpm run docs && pnpm run docs:commit && pnpm build:update-exports",
222
- "build:no-test": "tsup",
223
- "build:update-exports": "tsm --no-warnings scripts/updatePackageExports.ts",
224
- "build-test": "tsup --config tsup.test.config.ts",
225
- "pre-publish": "./scripts/check-if-is-sync.sh && pnpm build",
226
- "test:console-fmt": "tsm --no-warnings scripts/testConsoleFmt.ts",
239
+ "eslint": "CI=true eslint src/ --color --max-warnings=0 --fix",
240
+ "format": "prettier --write .",
241
+ "build": "tsup",
227
242
  "bench:deepEqual": "tsm --no-warnings benchmarks/deepEqual.ts",
228
243
  "docs": "typedoc",
229
- "docs:watch": "typedoc --watch",
230
- "docs:commit": "git add docs && git diff --cached --quiet docs || git commit -m 'docs: update docs'"
244
+ "docs:watch": "typedoc --watch"
231
245
  }
232
246
  }