@jsnw/common-utils 1.1.0 → 1.3.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/dist/index.cjs +48 -0
- package/dist/index.d.cts +25 -1
- package/dist/index.d.mts +25 -1
- package/dist/index.mjs +46 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -204,13 +204,61 @@ const trySettlePromises = async (promises, onError) => {
|
|
|
204
204
|
return results;
|
|
205
205
|
};
|
|
206
206
|
//#endregion
|
|
207
|
+
//#region src/isAbortError.ts
|
|
208
|
+
const isAbortError = (e, signals) => {
|
|
209
|
+
if (!!e && typeof e === "object" && "name" in e && e.name === "AbortError") return true;
|
|
210
|
+
const filtered = (signals ?? []).filter((v) => v !== void 0 && v !== null);
|
|
211
|
+
for (const signal of filtered) {
|
|
212
|
+
if (!signal.aborted) continue;
|
|
213
|
+
if (e === signal.reason) return true;
|
|
214
|
+
}
|
|
215
|
+
return false;
|
|
216
|
+
};
|
|
217
|
+
//#endregion
|
|
218
|
+
//#region src/withPerfmon.ts
|
|
219
|
+
var PerfmonError = class extends Error {
|
|
220
|
+
execTime;
|
|
221
|
+
error;
|
|
222
|
+
/**
|
|
223
|
+
* @param {number} execTime
|
|
224
|
+
* @param error
|
|
225
|
+
*/
|
|
226
|
+
constructor(execTime, error) {
|
|
227
|
+
super();
|
|
228
|
+
this.name = "PerfmonError";
|
|
229
|
+
this.execTime = execTime;
|
|
230
|
+
this.error = error;
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* @template {AnyFn} F
|
|
235
|
+
* @param {F} fn
|
|
236
|
+
* @param {any} [args]
|
|
237
|
+
* @return {WithPerfmonReturn<F>}
|
|
238
|
+
*/
|
|
239
|
+
const withPerfmon = (fn, ...args) => {
|
|
240
|
+
const startTime = performance.now();
|
|
241
|
+
try {
|
|
242
|
+
const promiseMaybe = fn(...args);
|
|
243
|
+
if (promiseMaybe instanceof Promise) return promiseMaybe.then((res) => [performance.now() - startTime, res], (e) => {
|
|
244
|
+
throw new PerfmonError(performance.now() - startTime, e);
|
|
245
|
+
});
|
|
246
|
+
return [performance.now() - startTime, promiseMaybe];
|
|
247
|
+
} catch (e) {
|
|
248
|
+
throw new PerfmonError(performance.now() - startTime, e);
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
//#endregion
|
|
207
252
|
exports.Monitor = Monitor;
|
|
253
|
+
exports.PerfmonError = PerfmonError;
|
|
208
254
|
exports.chunk = chunk;
|
|
209
255
|
exports.chunkStr = chunkStr;
|
|
210
256
|
exports.createInFlightDeduper = createInFlightDeduper;
|
|
211
257
|
exports.ignorePromise = ignorePromise;
|
|
258
|
+
exports.isAbortError = isAbortError;
|
|
212
259
|
exports.isObjectEmpty = isObjectEmpty;
|
|
213
260
|
exports.noop = noop;
|
|
214
261
|
exports.resultify = resultify;
|
|
215
262
|
exports.trySettlePromises = trySettlePromises;
|
|
216
263
|
exports.wait = wait;
|
|
264
|
+
exports.withPerfmon = withPerfmon;
|
package/dist/index.d.cts
CHANGED
|
@@ -130,4 +130,28 @@ type ErrorDetails = {
|
|
|
130
130
|
type ErrorCallback = (details: ErrorDetails, index: number) => void;
|
|
131
131
|
declare const trySettlePromises: <T extends (Promise<any> | DescribedPromise<any>)[]>(promises: [...T], onError?: ErrorCallback) => Promise<AwaitedTuple<T>>;
|
|
132
132
|
//#endregion
|
|
133
|
-
|
|
133
|
+
//#region src/isAbortError.d.ts
|
|
134
|
+
declare const isAbortError: (e: unknown, signals?: (AbortSignal | undefined | null)[]) => boolean;
|
|
135
|
+
//#endregion
|
|
136
|
+
//#region src/withPerfmon.d.ts
|
|
137
|
+
type AnyFn = (...args: any[]) => any;
|
|
138
|
+
type PerfmonResult<T> = [execTime: number, result: T];
|
|
139
|
+
type WithPerfmonReturn<F extends AnyFn> = ReturnType<F> extends Promise<any> ? Promise<PerfmonResult<Awaited<ReturnType<F>>>> : PerfmonResult<ReturnType<F>>;
|
|
140
|
+
declare class PerfmonError extends Error {
|
|
141
|
+
readonly execTime: number;
|
|
142
|
+
readonly error: unknown;
|
|
143
|
+
/**
|
|
144
|
+
* @param {number} execTime
|
|
145
|
+
* @param error
|
|
146
|
+
*/
|
|
147
|
+
constructor(execTime: number, error: unknown);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* @template {AnyFn} F
|
|
151
|
+
* @param {F} fn
|
|
152
|
+
* @param {any} [args]
|
|
153
|
+
* @return {WithPerfmonReturn<F>}
|
|
154
|
+
*/
|
|
155
|
+
declare const withPerfmon: <F extends AnyFn>(fn: F, ...args: Parameters<F>) => WithPerfmonReturn<F>;
|
|
156
|
+
//#endregion
|
|
157
|
+
export { ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, resultify, trySettlePromises, wait, withPerfmon };
|
package/dist/index.d.mts
CHANGED
|
@@ -130,4 +130,28 @@ type ErrorDetails = {
|
|
|
130
130
|
type ErrorCallback = (details: ErrorDetails, index: number) => void;
|
|
131
131
|
declare const trySettlePromises: <T extends (Promise<any> | DescribedPromise<any>)[]>(promises: [...T], onError?: ErrorCallback) => Promise<AwaitedTuple<T>>;
|
|
132
132
|
//#endregion
|
|
133
|
-
|
|
133
|
+
//#region src/isAbortError.d.ts
|
|
134
|
+
declare const isAbortError: (e: unknown, signals?: (AbortSignal | undefined | null)[]) => boolean;
|
|
135
|
+
//#endregion
|
|
136
|
+
//#region src/withPerfmon.d.ts
|
|
137
|
+
type AnyFn = (...args: any[]) => any;
|
|
138
|
+
type PerfmonResult<T> = [execTime: number, result: T];
|
|
139
|
+
type WithPerfmonReturn<F extends AnyFn> = ReturnType<F> extends Promise<any> ? Promise<PerfmonResult<Awaited<ReturnType<F>>>> : PerfmonResult<ReturnType<F>>;
|
|
140
|
+
declare class PerfmonError extends Error {
|
|
141
|
+
readonly execTime: number;
|
|
142
|
+
readonly error: unknown;
|
|
143
|
+
/**
|
|
144
|
+
* @param {number} execTime
|
|
145
|
+
* @param error
|
|
146
|
+
*/
|
|
147
|
+
constructor(execTime: number, error: unknown);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* @template {AnyFn} F
|
|
151
|
+
* @param {F} fn
|
|
152
|
+
* @param {any} [args]
|
|
153
|
+
* @return {WithPerfmonReturn<F>}
|
|
154
|
+
*/
|
|
155
|
+
declare const withPerfmon: <F extends AnyFn>(fn: F, ...args: Parameters<F>) => WithPerfmonReturn<F>;
|
|
156
|
+
//#endregion
|
|
157
|
+
export { ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, resultify, trySettlePromises, wait, withPerfmon };
|
package/dist/index.mjs
CHANGED
|
@@ -203,4 +203,49 @@ const trySettlePromises = async (promises, onError) => {
|
|
|
203
203
|
return results;
|
|
204
204
|
};
|
|
205
205
|
//#endregion
|
|
206
|
-
|
|
206
|
+
//#region src/isAbortError.ts
|
|
207
|
+
const isAbortError = (e, signals) => {
|
|
208
|
+
if (!!e && typeof e === "object" && "name" in e && e.name === "AbortError") return true;
|
|
209
|
+
const filtered = (signals ?? []).filter((v) => v !== void 0 && v !== null);
|
|
210
|
+
for (const signal of filtered) {
|
|
211
|
+
if (!signal.aborted) continue;
|
|
212
|
+
if (e === signal.reason) return true;
|
|
213
|
+
}
|
|
214
|
+
return false;
|
|
215
|
+
};
|
|
216
|
+
//#endregion
|
|
217
|
+
//#region src/withPerfmon.ts
|
|
218
|
+
var PerfmonError = class extends Error {
|
|
219
|
+
execTime;
|
|
220
|
+
error;
|
|
221
|
+
/**
|
|
222
|
+
* @param {number} execTime
|
|
223
|
+
* @param error
|
|
224
|
+
*/
|
|
225
|
+
constructor(execTime, error) {
|
|
226
|
+
super();
|
|
227
|
+
this.name = "PerfmonError";
|
|
228
|
+
this.execTime = execTime;
|
|
229
|
+
this.error = error;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
/**
|
|
233
|
+
* @template {AnyFn} F
|
|
234
|
+
* @param {F} fn
|
|
235
|
+
* @param {any} [args]
|
|
236
|
+
* @return {WithPerfmonReturn<F>}
|
|
237
|
+
*/
|
|
238
|
+
const withPerfmon = (fn, ...args) => {
|
|
239
|
+
const startTime = performance.now();
|
|
240
|
+
try {
|
|
241
|
+
const promiseMaybe = fn(...args);
|
|
242
|
+
if (promiseMaybe instanceof Promise) return promiseMaybe.then((res) => [performance.now() - startTime, res], (e) => {
|
|
243
|
+
throw new PerfmonError(performance.now() - startTime, e);
|
|
244
|
+
});
|
|
245
|
+
return [performance.now() - startTime, promiseMaybe];
|
|
246
|
+
} catch (e) {
|
|
247
|
+
throw new PerfmonError(performance.now() - startTime, e);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
//#endregion
|
|
251
|
+
export { Monitor, PerfmonError, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, 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.3.0",
|
|
4
4
|
"description": "Lightweight JavaScript/TypeScript utilities with async helpers, array and string operations",
|
|
5
5
|
"exports": {
|
|
6
6
|
"import": {
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"@types/jest": "^30.0.0",
|
|
41
41
|
"jest": "^30.4.2",
|
|
42
42
|
"ts-jest": "^29.4.11",
|
|
43
|
-
"tsdown": "^0.22.
|
|
43
|
+
"tsdown": "^0.22.8",
|
|
44
44
|
"typescript": "^5.9.3"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|