@jsnw/common-utils 1.2.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 CHANGED
@@ -215,7 +215,42 @@ const isAbortError = (e, signals) => {
215
215
  return false;
216
216
  };
217
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
218
252
  exports.Monitor = Monitor;
253
+ exports.PerfmonError = PerfmonError;
219
254
  exports.chunk = chunk;
220
255
  exports.chunkStr = chunkStr;
221
256
  exports.createInFlightDeduper = createInFlightDeduper;
@@ -226,3 +261,4 @@ exports.noop = noop;
226
261
  exports.resultify = resultify;
227
262
  exports.trySettlePromises = trySettlePromises;
228
263
  exports.wait = wait;
264
+ exports.withPerfmon = withPerfmon;
package/dist/index.d.cts CHANGED
@@ -133,4 +133,25 @@ declare const trySettlePromises: <T extends (Promise<any> | DescribedPromise<any
133
133
  //#region src/isAbortError.d.ts
134
134
  declare const isAbortError: (e: unknown, signals?: (AbortSignal | undefined | null)[]) => boolean;
135
135
  //#endregion
136
- export { ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, resultify, trySettlePromises, wait };
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
@@ -133,4 +133,25 @@ declare const trySettlePromises: <T extends (Promise<any> | DescribedPromise<any
133
133
  //#region src/isAbortError.d.ts
134
134
  declare const isAbortError: (e: unknown, signals?: (AbortSignal | undefined | null)[]) => boolean;
135
135
  //#endregion
136
- export { ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, resultify, trySettlePromises, wait };
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
@@ -214,4 +214,38 @@ const isAbortError = (e, signals) => {
214
214
  return false;
215
215
  };
216
216
  //#endregion
217
- export { Monitor, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, resultify, trySettlePromises, wait };
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.2.0",
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.7",
43
+ "tsdown": "^0.22.8",
44
44
  "typescript": "^5.9.3"
45
45
  },
46
46
  "scripts": {