@orioro/util 0.4.0 → 0.6.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.mjs CHANGED
@@ -384,9 +384,15 @@ function pickPaths(sourceObj, paths) {
384
384
  return paths.reduce(function (acc, path) {
385
385
  var _a = Array.isArray(path) ? path : [path, path],
386
386
  targetPath = _a[0],
387
- resolver = _a[1];
387
+ resolver = _a[1],
388
+ defaultValue = _a[2];
388
389
  var value = typeof resolver === 'string' ? getProperty(sourceObj, resolver) : resolver(sourceObj);
389
- return setProperty(acc, targetPath, value);
390
+ var valueAfterDefault = typeof value === 'undefined' ? defaultValue : value;
391
+ //
392
+ // Undefined values are skipped in order to avoid nested
393
+ // setting undefined values
394
+ //
395
+ return typeof valueAfterDefault !== 'undefined' ? setProperty(acc, targetPath, valueAfterDefault) : acc;
390
396
  }, {});
391
397
  }
392
398
 
@@ -477,6 +483,38 @@ function arrayChunk(array, chunkSize) {
477
483
  return chunks;
478
484
  }
479
485
 
486
+ var TimeoutError = /** @class */function (_super) {
487
+ __extends(TimeoutError, _super);
488
+ function TimeoutError(message) {
489
+ var _this = _super.call(this, message) || this;
490
+ _this.name = 'TimeoutError';
491
+ _this.code = 'ETIMEDOUT';
492
+ _this.timestamp = new Date();
493
+ // Maintain proper stack trace (only available in V8 engines, e.g., Chrome and Node.js)
494
+ if (Error.captureStackTrace) {
495
+ Error.captureStackTrace(_this, TimeoutError);
496
+ }
497
+ return _this;
498
+ }
499
+ return TimeoutError;
500
+ }(Error);
501
+ function withTimeout(fn, timeout) {
502
+ return function () {
503
+ var args = [];
504
+ for (var _i = 0; _i < arguments.length; _i++) {
505
+ args[_i] = arguments[_i];
506
+ }
507
+ return new Promise(function (resolve, reject) {
508
+ var timer = setTimeout(function () {
509
+ reject(new TimeoutError("Function timed out (max: ".concat(timeout, "ms)")));
510
+ }, timeout);
511
+ fn.apply(void 0, args).then(resolve)["catch"](reject)["finally"](function () {
512
+ return clearTimeout(timer);
513
+ });
514
+ });
515
+ };
516
+ }
517
+
480
518
  var SKIPPED = Symbol();
481
519
  function batchFn(itemFn, _a) {
482
520
  var _b = _a === void 0 ? {} : _a,
@@ -484,7 +522,12 @@ function batchFn(itemFn, _a) {
484
522
  batchSize = _c === void 0 ? 10 : _c,
485
523
  skip = _b.skip,
486
524
  _d = _b.retry,
487
- retry = _d === void 0 ? false : _d;
525
+ retry = _d === void 0 ? false : _d,
526
+ timeout = _b.timeout;
527
+ //
528
+ // Optionally wrap itemFn into timeout
529
+ //
530
+ itemFn = timeout ? withTimeout(itemFn, timeout) : itemFn;
488
531
  return function batchExec(items) {
489
532
  var _this = this;
490
533
  var batches = arrayChunk(items, batchSize);
@@ -1479,4 +1522,4 @@ validateAsync.or = or;
1479
1522
  validateAsync.not = not;
1480
1523
  validateAsync.assertValid = assertValidAsync;
1481
1524
 
1482
- export { PromiseLikeEventEmitter, SKIPPED, ValidationError, batchFn, debugFn, deepFreeze, interpolate, makeTypeOf, maybeFn, parseBatchedResults, pickPaths, promiseReduce, resolveNestedPromises, switchExec, switchValue, typeMap, typeOf, validate, validateAsync, wait };
1525
+ export { PromiseLikeEventEmitter, SKIPPED, TimeoutError, ValidationError, batchFn, debugFn, deepFreeze, interpolate, makeTypeOf, maybeFn, parseBatchedResults, pickPaths, promiseReduce, resolveNestedPromises, switchExec, switchValue, typeMap, typeOf, validate, validateAsync, wait, withTimeout };
@@ -1,4 +1,4 @@
1
1
  type Resolver = string | ((sourceObj: Record<string, any>) => any);
2
- type PathSpec = string | [string, Resolver];
2
+ type PathSpec = string | [string, Resolver, any?];
3
3
  export declare function pickPaths(sourceObj: Record<string, any>, paths: PathSpec[]): Record<string, any>;
4
4
  export {};
@@ -4,6 +4,7 @@ type BatchFnOptions = {
4
4
  skip?: (input: any) => Promise<boolean> | boolean;
5
5
  batchSize?: number;
6
6
  retry?: BackoffOptions | boolean;
7
+ timeout?: number | false;
7
8
  };
8
9
  type Batch = {
9
10
  index: number;
@@ -39,7 +40,7 @@ type EventTypes = {
39
40
  }) => void;
40
41
  error: (err: Error) => void;
41
42
  };
42
- export declare function batchFn(itemFn: (input: any) => Promise<any> | any, { batchSize, skip, retry }?: BatchFnOptions): (items: any[]) => PromiseLikeEventEmitter<any[], EventTypes>;
43
+ export declare function batchFn(itemFn: (input: any) => Promise<any> | any, { batchSize, skip, retry, timeout }?: BatchFnOptions): (items: any[]) => PromiseLikeEventEmitter<any[], EventTypes>;
43
44
  type ParsedBatchResults = {
44
45
  results: any[];
45
46
  errors: Error[];
@@ -1,3 +1,4 @@
1
1
  export * from './promiseReduce';
2
2
  export * from './resolveNestedPromises';
3
3
  export * from './batchFn';
4
+ export * from './withTimeout';
@@ -0,0 +1,6 @@
1
+ export declare class TimeoutError extends Error {
2
+ code: string;
3
+ timestamp: Date;
4
+ constructor(message: string);
5
+ }
6
+ export declare function withTimeout<T extends (...args: any[]) => Promise<any>>(fn: T, timeout: number): (...args: Parameters<T>) => Promise<ReturnType<T>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orioro/util",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "packageManager": "yarn@4.0.2",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",