@jsnw/common-utils 1.3.0 → 1.4.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
@@ -188,6 +188,12 @@ var Monitor = class Monitor {
188
188
  };
189
189
  //#endregion
190
190
  //#region src/trySettlePromises.ts
191
+ /**
192
+ * @template {(Promise<any>|DescribedPromise<any>)[]} T
193
+ * @param {[...T]} promises
194
+ * @param {ErrorCallback} onError
195
+ * @returns {Promise<AwaitedTuple<T>>}
196
+ */
191
197
  const trySettlePromises = async (promises, onError) => {
192
198
  onError = onError && typeof onError === "function" ? onError : noop;
193
199
  const results = await Promise.allSettled(promises.map((v) => Array.isArray(v) ? v[0] : v));
@@ -249,6 +255,25 @@ const withPerfmon = (fn, ...args) => {
249
255
  }
250
256
  };
251
257
  //#endregion
258
+ //#region src/promisify.ts
259
+ /**
260
+ * @template {(...args: any[]) => any} F
261
+ * @param {F} fn
262
+ * @param {any} args
263
+ * @returns {Promise<Awaited<ReturnType<F>>>}
264
+ */
265
+ const promisify = (fn, ...args) => {
266
+ return new Promise((resolve, reject) => {
267
+ try {
268
+ const promiseMaybe = fn(...args);
269
+ if (promiseMaybe instanceof Promise) promiseMaybe.then(resolve).catch(reject);
270
+ else resolve(promiseMaybe);
271
+ } catch (e) {
272
+ reject(e);
273
+ }
274
+ });
275
+ };
276
+ //#endregion
252
277
  exports.Monitor = Monitor;
253
278
  exports.PerfmonError = PerfmonError;
254
279
  exports.chunk = chunk;
@@ -258,6 +283,7 @@ exports.ignorePromise = ignorePromise;
258
283
  exports.isAbortError = isAbortError;
259
284
  exports.isObjectEmpty = isObjectEmpty;
260
285
  exports.noop = noop;
286
+ exports.promisify = promisify;
261
287
  exports.resultify = resultify;
262
288
  exports.trySettlePromises = trySettlePromises;
263
289
  exports.wait = wait;
package/dist/index.d.cts CHANGED
@@ -128,6 +128,12 @@ type ErrorDetails = {
128
128
  message?: string;
129
129
  };
130
130
  type ErrorCallback = (details: ErrorDetails, index: number) => void;
131
+ /**
132
+ * @template {(Promise<any>|DescribedPromise<any>)[]} T
133
+ * @param {[...T]} promises
134
+ * @param {ErrorCallback} onError
135
+ * @returns {Promise<AwaitedTuple<T>>}
136
+ */
131
137
  declare const trySettlePromises: <T extends (Promise<any> | DescribedPromise<any>)[]>(promises: [...T], onError?: ErrorCallback) => Promise<AwaitedTuple<T>>;
132
138
  //#endregion
133
139
  //#region src/isAbortError.d.ts
@@ -154,4 +160,13 @@ declare class PerfmonError extends Error {
154
160
  */
155
161
  declare const withPerfmon: <F extends AnyFn>(fn: F, ...args: Parameters<F>) => WithPerfmonReturn<F>;
156
162
  //#endregion
157
- export { ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, resultify, trySettlePromises, wait, withPerfmon };
163
+ //#region src/promisify.d.ts
164
+ /**
165
+ * @template {(...args: any[]) => any} F
166
+ * @param {F} fn
167
+ * @param {any} args
168
+ * @returns {Promise<Awaited<ReturnType<F>>>}
169
+ */
170
+ declare const promisify: <F extends (...args: any[]) => any>(fn: F, ...args: Parameters<F>) => Promise<Awaited<ReturnType<F>>>;
171
+ //#endregion
172
+ export { ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, promisify, resultify, trySettlePromises, wait, withPerfmon };
package/dist/index.d.mts CHANGED
@@ -128,6 +128,12 @@ type ErrorDetails = {
128
128
  message?: string;
129
129
  };
130
130
  type ErrorCallback = (details: ErrorDetails, index: number) => void;
131
+ /**
132
+ * @template {(Promise<any>|DescribedPromise<any>)[]} T
133
+ * @param {[...T]} promises
134
+ * @param {ErrorCallback} onError
135
+ * @returns {Promise<AwaitedTuple<T>>}
136
+ */
131
137
  declare const trySettlePromises: <T extends (Promise<any> | DescribedPromise<any>)[]>(promises: [...T], onError?: ErrorCallback) => Promise<AwaitedTuple<T>>;
132
138
  //#endregion
133
139
  //#region src/isAbortError.d.ts
@@ -154,4 +160,13 @@ declare class PerfmonError extends Error {
154
160
  */
155
161
  declare const withPerfmon: <F extends AnyFn>(fn: F, ...args: Parameters<F>) => WithPerfmonReturn<F>;
156
162
  //#endregion
157
- export { ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, resultify, trySettlePromises, wait, withPerfmon };
163
+ //#region src/promisify.d.ts
164
+ /**
165
+ * @template {(...args: any[]) => any} F
166
+ * @param {F} fn
167
+ * @param {any} args
168
+ * @returns {Promise<Awaited<ReturnType<F>>>}
169
+ */
170
+ declare const promisify: <F extends (...args: any[]) => any>(fn: F, ...args: Parameters<F>) => Promise<Awaited<ReturnType<F>>>;
171
+ //#endregion
172
+ export { ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, promisify, resultify, trySettlePromises, wait, withPerfmon };
package/dist/index.mjs CHANGED
@@ -187,6 +187,12 @@ var Monitor = class Monitor {
187
187
  };
188
188
  //#endregion
189
189
  //#region src/trySettlePromises.ts
190
+ /**
191
+ * @template {(Promise<any>|DescribedPromise<any>)[]} T
192
+ * @param {[...T]} promises
193
+ * @param {ErrorCallback} onError
194
+ * @returns {Promise<AwaitedTuple<T>>}
195
+ */
190
196
  const trySettlePromises = async (promises, onError) => {
191
197
  onError = onError && typeof onError === "function" ? onError : noop;
192
198
  const results = await Promise.allSettled(promises.map((v) => Array.isArray(v) ? v[0] : v));
@@ -248,4 +254,23 @@ const withPerfmon = (fn, ...args) => {
248
254
  }
249
255
  };
250
256
  //#endregion
251
- export { Monitor, PerfmonError, chunk, chunkStr, createInFlightDeduper, ignorePromise, isAbortError, isObjectEmpty, noop, resultify, trySettlePromises, wait, withPerfmon };
257
+ //#region src/promisify.ts
258
+ /**
259
+ * @template {(...args: any[]) => any} F
260
+ * @param {F} fn
261
+ * @param {any} args
262
+ * @returns {Promise<Awaited<ReturnType<F>>>}
263
+ */
264
+ const promisify = (fn, ...args) => {
265
+ return new Promise((resolve, reject) => {
266
+ try {
267
+ const promiseMaybe = fn(...args);
268
+ if (promiseMaybe instanceof Promise) promiseMaybe.then(resolve).catch(reject);
269
+ else resolve(promiseMaybe);
270
+ } catch (e) {
271
+ reject(e);
272
+ }
273
+ });
274
+ };
275
+ //#endregion
276
+ export { Monitor, PerfmonError, chunk, chunkStr, createInFlightDeduper, 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.0",
3
+ "version": "1.4.0",
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",