@jsnw/common-utils 1.3.0 → 1.4.1
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/dist/index.cjs +44 -0
- package/dist/index.d.cts +28 -1
- package/dist/index.d.mts +28 -1
- package/dist/index.mjs +43 -1
- package/package.json +2 -1
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.
|
|
@@ -188,6 +205,12 @@ var Monitor = class Monitor {
|
|
|
188
205
|
};
|
|
189
206
|
//#endregion
|
|
190
207
|
//#region src/trySettlePromises.ts
|
|
208
|
+
/**
|
|
209
|
+
* @template {(Promise<any>|DescribedPromise<any>)[]} T
|
|
210
|
+
* @param {[...T]} promises
|
|
211
|
+
* @param {ErrorCallback} onError
|
|
212
|
+
* @returns {Promise<AwaitedTuple<T>>}
|
|
213
|
+
*/
|
|
191
214
|
const trySettlePromises = async (promises, onError) => {
|
|
192
215
|
onError = onError && typeof onError === "function" ? onError : noop;
|
|
193
216
|
const results = await Promise.allSettled(promises.map((v) => Array.isArray(v) ? v[0] : v));
|
|
@@ -249,15 +272,36 @@ const withPerfmon = (fn, ...args) => {
|
|
|
249
272
|
}
|
|
250
273
|
};
|
|
251
274
|
//#endregion
|
|
275
|
+
//#region src/promisify.ts
|
|
276
|
+
/**
|
|
277
|
+
* @template {(...args: any[]) => any} F
|
|
278
|
+
* @param {F} fn
|
|
279
|
+
* @param {any} args
|
|
280
|
+
* @returns {Promise<Awaited<ReturnType<F>>>}
|
|
281
|
+
*/
|
|
282
|
+
const promisify = (fn, ...args) => {
|
|
283
|
+
return new Promise((resolve, reject) => {
|
|
284
|
+
try {
|
|
285
|
+
const promiseMaybe = fn(...args);
|
|
286
|
+
if (promiseMaybe instanceof Promise) promiseMaybe.then(resolve).catch(reject);
|
|
287
|
+
else resolve(promiseMaybe);
|
|
288
|
+
} catch (e) {
|
|
289
|
+
reject(e);
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
};
|
|
293
|
+
//#endregion
|
|
252
294
|
exports.Monitor = Monitor;
|
|
253
295
|
exports.PerfmonError = PerfmonError;
|
|
254
296
|
exports.chunk = chunk;
|
|
255
297
|
exports.chunkStr = chunkStr;
|
|
256
298
|
exports.createInFlightDeduper = createInFlightDeduper;
|
|
299
|
+
exports.defer = defer;
|
|
257
300
|
exports.ignorePromise = ignorePromise;
|
|
258
301
|
exports.isAbortError = isAbortError;
|
|
259
302
|
exports.isObjectEmpty = isObjectEmpty;
|
|
260
303
|
exports.noop = noop;
|
|
304
|
+
exports.promisify = promisify;
|
|
261
305
|
exports.resultify = resultify;
|
|
262
306
|
exports.trySettlePromises = trySettlePromises;
|
|
263
307
|
exports.wait = wait;
|
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.
|
|
@@ -128,6 +140,12 @@ type ErrorDetails = {
|
|
|
128
140
|
message?: string;
|
|
129
141
|
};
|
|
130
142
|
type ErrorCallback = (details: ErrorDetails, index: number) => void;
|
|
143
|
+
/**
|
|
144
|
+
* @template {(Promise<any>|DescribedPromise<any>)[]} T
|
|
145
|
+
* @param {[...T]} promises
|
|
146
|
+
* @param {ErrorCallback} onError
|
|
147
|
+
* @returns {Promise<AwaitedTuple<T>>}
|
|
148
|
+
*/
|
|
131
149
|
declare const trySettlePromises: <T extends (Promise<any> | DescribedPromise<any>)[]>(promises: [...T], onError?: ErrorCallback) => Promise<AwaitedTuple<T>>;
|
|
132
150
|
//#endregion
|
|
133
151
|
//#region src/isAbortError.d.ts
|
|
@@ -154,4 +172,13 @@ declare class PerfmonError extends Error {
|
|
|
154
172
|
*/
|
|
155
173
|
declare const withPerfmon: <F extends AnyFn>(fn: F, ...args: Parameters<F>) => WithPerfmonReturn<F>;
|
|
156
174
|
//#endregion
|
|
157
|
-
|
|
175
|
+
//#region src/promisify.d.ts
|
|
176
|
+
/**
|
|
177
|
+
* @template {(...args: any[]) => any} F
|
|
178
|
+
* @param {F} fn
|
|
179
|
+
* @param {any} args
|
|
180
|
+
* @returns {Promise<Awaited<ReturnType<F>>>}
|
|
181
|
+
*/
|
|
182
|
+
declare const promisify: <F extends (...args: any[]) => any>(fn: F, ...args: Parameters<F>) => Promise<Awaited<ReturnType<F>>>;
|
|
183
|
+
//#endregion
|
|
184
|
+
export { DeferredPromise, ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, 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.
|
|
@@ -128,6 +140,12 @@ type ErrorDetails = {
|
|
|
128
140
|
message?: string;
|
|
129
141
|
};
|
|
130
142
|
type ErrorCallback = (details: ErrorDetails, index: number) => void;
|
|
143
|
+
/**
|
|
144
|
+
* @template {(Promise<any>|DescribedPromise<any>)[]} T
|
|
145
|
+
* @param {[...T]} promises
|
|
146
|
+
* @param {ErrorCallback} onError
|
|
147
|
+
* @returns {Promise<AwaitedTuple<T>>}
|
|
148
|
+
*/
|
|
131
149
|
declare const trySettlePromises: <T extends (Promise<any> | DescribedPromise<any>)[]>(promises: [...T], onError?: ErrorCallback) => Promise<AwaitedTuple<T>>;
|
|
132
150
|
//#endregion
|
|
133
151
|
//#region src/isAbortError.d.ts
|
|
@@ -154,4 +172,13 @@ declare class PerfmonError extends Error {
|
|
|
154
172
|
*/
|
|
155
173
|
declare const withPerfmon: <F extends AnyFn>(fn: F, ...args: Parameters<F>) => WithPerfmonReturn<F>;
|
|
156
174
|
//#endregion
|
|
157
|
-
|
|
175
|
+
//#region src/promisify.d.ts
|
|
176
|
+
/**
|
|
177
|
+
* @template {(...args: any[]) => any} F
|
|
178
|
+
* @param {F} fn
|
|
179
|
+
* @param {any} args
|
|
180
|
+
* @returns {Promise<Awaited<ReturnType<F>>>}
|
|
181
|
+
*/
|
|
182
|
+
declare const promisify: <F extends (...args: any[]) => any>(fn: F, ...args: Parameters<F>) => Promise<Awaited<ReturnType<F>>>;
|
|
183
|
+
//#endregion
|
|
184
|
+
export { DeferredPromise, ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, 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.
|
|
@@ -187,6 +204,12 @@ var Monitor = class Monitor {
|
|
|
187
204
|
};
|
|
188
205
|
//#endregion
|
|
189
206
|
//#region src/trySettlePromises.ts
|
|
207
|
+
/**
|
|
208
|
+
* @template {(Promise<any>|DescribedPromise<any>)[]} T
|
|
209
|
+
* @param {[...T]} promises
|
|
210
|
+
* @param {ErrorCallback} onError
|
|
211
|
+
* @returns {Promise<AwaitedTuple<T>>}
|
|
212
|
+
*/
|
|
190
213
|
const trySettlePromises = async (promises, onError) => {
|
|
191
214
|
onError = onError && typeof onError === "function" ? onError : noop;
|
|
192
215
|
const results = await Promise.allSettled(promises.map((v) => Array.isArray(v) ? v[0] : v));
|
|
@@ -248,4 +271,23 @@ const withPerfmon = (fn, ...args) => {
|
|
|
248
271
|
}
|
|
249
272
|
};
|
|
250
273
|
//#endregion
|
|
251
|
-
|
|
274
|
+
//#region src/promisify.ts
|
|
275
|
+
/**
|
|
276
|
+
* @template {(...args: any[]) => any} F
|
|
277
|
+
* @param {F} fn
|
|
278
|
+
* @param {any} args
|
|
279
|
+
* @returns {Promise<Awaited<ReturnType<F>>>}
|
|
280
|
+
*/
|
|
281
|
+
const promisify = (fn, ...args) => {
|
|
282
|
+
return new Promise((resolve, reject) => {
|
|
283
|
+
try {
|
|
284
|
+
const promiseMaybe = fn(...args);
|
|
285
|
+
if (promiseMaybe instanceof Promise) promiseMaybe.then(resolve).catch(reject);
|
|
286
|
+
else resolve(promiseMaybe);
|
|
287
|
+
} catch (e) {
|
|
288
|
+
reject(e);
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
};
|
|
292
|
+
//#endregion
|
|
293
|
+
export { Monitor, PerfmonError, chunk, chunkStr, createInFlightDeduper, defer, ignorePromise, isAbortError, isObjectEmpty, noop, promisify, resultify, trySettlePromises, wait, withPerfmon };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsnw/common-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "Lightweight JavaScript/TypeScript utilities with async helpers, array and string operations",
|
|
5
5
|
"exports": {
|
|
6
6
|
"import": {
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"homepage": "https://github.com/pvbaliuk/jsn-utils#readme",
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@types/jest": "^30.0.0",
|
|
41
|
+
"@types/node": "^24.13.3",
|
|
41
42
|
"jest": "^30.4.2",
|
|
42
43
|
"ts-jest": "^29.4.11",
|
|
43
44
|
"tsdown": "^0.22.8",
|