@jsnw/common-utils 1.4.0 → 1.5.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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # jsn-utils
2
2
  ![coverage badge](https://img.shields.io/badge/coverage-100%25-brightgreen)
3
3
 
4
- ## 1.1.0
4
+ ## 1.5.0
5
5
  ## List of utility functions
6
6
 
7
7
  - `noop(): void` - A no-operation function that does nothing
@@ -13,6 +13,7 @@
13
13
  - `createPromisesCache` - deprecated; use createInFlightDeduper instead
14
14
  - `createInFlightDeduper` - Creates a deduplicator for in-flight promises or sync functions. Prevents duplicate executions for the same key while a promise is pending. Clears the entry after settlement
15
15
  - `resultify` - Executes a function and returns its result as a tuple `[value, error]`. Works with both sync and async functions. Never throws.
16
+ - `clamp(num: number, min: number, max: number)` - Clamps a number between min and max
16
17
 
17
18
  ## List of utility classes
18
19
 
package/dist/index.cjs CHANGED
@@ -46,6 +46,23 @@ function chunkStr(str, chunkSize) {
46
46
  return Array.from({ length: Math.ceil(str.length / chunkSize) }, (_, i) => str.slice(i * chunkSize, i * chunkSize + chunkSize));
47
47
  }
48
48
  //#endregion
49
+ //#region src/defer.ts
50
+ /**
51
+ * @template T
52
+ * @returns {DeferredPromise<T>}
53
+ */
54
+ const defer = () => {
55
+ let resolve, reject;
56
+ return {
57
+ promise: new Promise((res, rej) => {
58
+ resolve = res;
59
+ reject = rej;
60
+ }),
61
+ resolve,
62
+ reject
63
+ };
64
+ };
65
+ //#endregion
49
66
  //#region src/wait.ts
50
67
  /**
51
68
  * Waits asynchronously for the specified amount of milliseconds before resolving.
@@ -274,11 +291,22 @@ const promisify = (fn, ...args) => {
274
291
  });
275
292
  };
276
293
  //#endregion
294
+ //#region src/clamp.ts
295
+ /**
296
+ * @param {number} num
297
+ * @param {number} min
298
+ * @param {number} max
299
+ * @returns {number}
300
+ */
301
+ const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
302
+ //#endregion
277
303
  exports.Monitor = Monitor;
278
304
  exports.PerfmonError = PerfmonError;
279
305
  exports.chunk = chunk;
280
306
  exports.chunkStr = chunkStr;
307
+ exports.clamp = clamp;
281
308
  exports.createInFlightDeduper = createInFlightDeduper;
309
+ exports.defer = defer;
282
310
  exports.ignorePromise = ignorePromise;
283
311
  exports.isAbortError = isAbortError;
284
312
  exports.isObjectEmpty = isObjectEmpty;
package/dist/index.d.cts CHANGED
@@ -37,6 +37,18 @@ declare const chunk: <T extends any>(arr: T[], chunk_size: number) => T[][];
37
37
  */
38
38
  declare function chunkStr(str: string, chunkSize: number): string[];
39
39
  //#endregion
40
+ //#region src/defer.d.ts
41
+ type DeferredPromise<T> = {
42
+ promise: Promise<T>;
43
+ resolve: (value: T) => void;
44
+ reject: (reason?: any) => void;
45
+ };
46
+ /**
47
+ * @template T
48
+ * @returns {DeferredPromise<T>}
49
+ */
50
+ declare const defer: <T>() => DeferredPromise<T>;
51
+ //#endregion
40
52
  //#region src/wait.d.ts
41
53
  /**
42
54
  * Waits asynchronously for the specified amount of milliseconds before resolving.
@@ -169,4 +181,13 @@ declare const withPerfmon: <F extends AnyFn>(fn: F, ...args: Parameters<F>) => W
169
181
  */
170
182
  declare const promisify: <F extends (...args: any[]) => any>(fn: F, ...args: Parameters<F>) => Promise<Awaited<ReturnType<F>>>;
171
183
  //#endregion
172
- export { ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, promisify, resultify, trySettlePromises, wait, withPerfmon };
184
+ //#region src/clamp.d.ts
185
+ /**
186
+ * @param {number} num
187
+ * @param {number} min
188
+ * @param {number} max
189
+ * @returns {number}
190
+ */
191
+ declare const clamp: (num: number, min: number, max: number) => number;
192
+ //#endregion
193
+ export { DeferredPromise, ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, clamp, createInFlightDeduper, defer, ignorePromise, isAbortError, isObjectEmpty, noop, promisify, resultify, trySettlePromises, wait, withPerfmon };
package/dist/index.d.mts CHANGED
@@ -37,6 +37,18 @@ declare const chunk: <T extends any>(arr: T[], chunk_size: number) => T[][];
37
37
  */
38
38
  declare function chunkStr(str: string, chunkSize: number): string[];
39
39
  //#endregion
40
+ //#region src/defer.d.ts
41
+ type DeferredPromise<T> = {
42
+ promise: Promise<T>;
43
+ resolve: (value: T) => void;
44
+ reject: (reason?: any) => void;
45
+ };
46
+ /**
47
+ * @template T
48
+ * @returns {DeferredPromise<T>}
49
+ */
50
+ declare const defer: <T>() => DeferredPromise<T>;
51
+ //#endregion
40
52
  //#region src/wait.d.ts
41
53
  /**
42
54
  * Waits asynchronously for the specified amount of milliseconds before resolving.
@@ -169,4 +181,13 @@ declare const withPerfmon: <F extends AnyFn>(fn: F, ...args: Parameters<F>) => W
169
181
  */
170
182
  declare const promisify: <F extends (...args: any[]) => any>(fn: F, ...args: Parameters<F>) => Promise<Awaited<ReturnType<F>>>;
171
183
  //#endregion
172
- export { ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, promisify, resultify, trySettlePromises, wait, withPerfmon };
184
+ //#region src/clamp.d.ts
185
+ /**
186
+ * @param {number} num
187
+ * @param {number} min
188
+ * @param {number} max
189
+ * @returns {number}
190
+ */
191
+ declare const clamp: (num: number, min: number, max: number) => number;
192
+ //#endregion
193
+ export { DeferredPromise, ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, clamp, createInFlightDeduper, defer, ignorePromise, isAbortError, isObjectEmpty, noop, promisify, resultify, trySettlePromises, wait, withPerfmon };
package/dist/index.mjs CHANGED
@@ -45,6 +45,23 @@ function chunkStr(str, chunkSize) {
45
45
  return Array.from({ length: Math.ceil(str.length / chunkSize) }, (_, i) => str.slice(i * chunkSize, i * chunkSize + chunkSize));
46
46
  }
47
47
  //#endregion
48
+ //#region src/defer.ts
49
+ /**
50
+ * @template T
51
+ * @returns {DeferredPromise<T>}
52
+ */
53
+ const defer = () => {
54
+ let resolve, reject;
55
+ return {
56
+ promise: new Promise((res, rej) => {
57
+ resolve = res;
58
+ reject = rej;
59
+ }),
60
+ resolve,
61
+ reject
62
+ };
63
+ };
64
+ //#endregion
48
65
  //#region src/wait.ts
49
66
  /**
50
67
  * Waits asynchronously for the specified amount of milliseconds before resolving.
@@ -273,4 +290,13 @@ const promisify = (fn, ...args) => {
273
290
  });
274
291
  };
275
292
  //#endregion
276
- export { Monitor, PerfmonError, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, promisify, resultify, trySettlePromises, wait, withPerfmon };
293
+ //#region src/clamp.ts
294
+ /**
295
+ * @param {number} num
296
+ * @param {number} min
297
+ * @param {number} max
298
+ * @returns {number}
299
+ */
300
+ const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
301
+ //#endregion
302
+ export { Monitor, PerfmonError, chunk, chunkStr, clamp, createInFlightDeduper, defer, ignorePromise, isAbortError, isObjectEmpty, noop, promisify, resultify, trySettlePromises, wait, withPerfmon };
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "@jsnw/common-utils",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Lightweight JavaScript/TypeScript utilities with async helpers, array and string operations",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
5
8
  "exports": {
6
9
  "import": {
7
10
  "types": "./dist/index.d.mts",
@@ -39,9 +42,9 @@
39
42
  "devDependencies": {
40
43
  "@types/jest": "^30.0.0",
41
44
  "@types/node": "^24.13.3",
42
- "jest": "^30.4.2",
43
- "ts-jest": "^29.4.11",
44
- "tsdown": "^0.22.8",
45
+ "jest": "^30.5.1",
46
+ "ts-jest": "^29.4.12",
47
+ "tsdown": "^0.22.14",
45
48
  "typescript": "^5.9.3"
46
49
  },
47
50
  "scripts": {