@orioro/util 0.3.0 → 0.5.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 +18 -5
- package/dist/pickPaths/index.d.ts +1 -1
- package/dist/promise/batchFn.d.ts +3 -1
- package/package.json +2 -1
package/dist/index.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import EventEmitter from 'eventemitter3';
|
|
|
2
2
|
import copy from 'fast-copy';
|
|
3
3
|
import { getProperty, setProperty } from 'dot-prop';
|
|
4
4
|
import traverse from 'traverse';
|
|
5
|
+
import { backOff } from 'exponential-backoff';
|
|
5
6
|
|
|
6
7
|
function _typeof(o) {
|
|
7
8
|
"@babel/helpers - typeof";
|
|
@@ -383,9 +384,15 @@ function pickPaths(sourceObj, paths) {
|
|
|
383
384
|
return paths.reduce(function (acc, path) {
|
|
384
385
|
var _a = Array.isArray(path) ? path : [path, path],
|
|
385
386
|
targetPath = _a[0],
|
|
386
|
-
resolver = _a[1]
|
|
387
|
+
resolver = _a[1],
|
|
388
|
+
defaultValue = _a[2];
|
|
387
389
|
var value = typeof resolver === 'string' ? getProperty(sourceObj, resolver) : resolver(sourceObj);
|
|
388
|
-
|
|
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;
|
|
389
396
|
}, {});
|
|
390
397
|
}
|
|
391
398
|
|
|
@@ -477,11 +484,13 @@ function arrayChunk(array, chunkSize) {
|
|
|
477
484
|
}
|
|
478
485
|
|
|
479
486
|
var SKIPPED = Symbol();
|
|
480
|
-
function batchFn(
|
|
487
|
+
function batchFn(itemFn, _a) {
|
|
481
488
|
var _b = _a === void 0 ? {} : _a,
|
|
482
489
|
_c = _b.batchSize,
|
|
483
490
|
batchSize = _c === void 0 ? 10 : _c,
|
|
484
|
-
skip = _b.skip
|
|
491
|
+
skip = _b.skip,
|
|
492
|
+
_d = _b.retry,
|
|
493
|
+
retry = _d === void 0 ? false : _d;
|
|
485
494
|
return function batchExec(items) {
|
|
486
495
|
var _this = this;
|
|
487
496
|
var batches = arrayChunk(items, batchSize);
|
|
@@ -526,7 +535,11 @@ function batchFn(fn, _a) {
|
|
|
526
535
|
_a = SKIPPED;
|
|
527
536
|
return [3 /*break*/, 4];
|
|
528
537
|
case 2:
|
|
529
|
-
return [4 /*yield*/,
|
|
538
|
+
return [4 /*yield*/, retry === true ? backOff(function () {
|
|
539
|
+
return itemFn(item);
|
|
540
|
+
}) : _typeof(retry) === 'object' && retry !== null ? backOff(function () {
|
|
541
|
+
return itemFn(item);
|
|
542
|
+
}, retry) : itemFn(item)];
|
|
530
543
|
case 3:
|
|
531
544
|
_a = _b.sent();
|
|
532
545
|
_b.label = 4;
|
|
@@ -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 {};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { PromiseLikeEventEmitter } from '../PromiseLikeEventEmitter';
|
|
2
|
+
import { BackoffOptions } from 'exponential-backoff';
|
|
2
3
|
type BatchFnOptions = {
|
|
3
4
|
skip?: (input: any) => Promise<boolean> | boolean;
|
|
4
5
|
batchSize?: number;
|
|
6
|
+
retry?: BackoffOptions | boolean;
|
|
5
7
|
};
|
|
6
8
|
type Batch = {
|
|
7
9
|
index: number;
|
|
@@ -37,7 +39,7 @@ type EventTypes = {
|
|
|
37
39
|
}) => void;
|
|
38
40
|
error: (err: Error) => void;
|
|
39
41
|
};
|
|
40
|
-
export declare function batchFn(
|
|
42
|
+
export declare function batchFn(itemFn: (input: any) => Promise<any> | any, { batchSize, skip, retry }?: BatchFnOptions): (items: any[]) => PromiseLikeEventEmitter<any[], EventTypes>;
|
|
41
43
|
type ParsedBatchResults = {
|
|
42
44
|
results: any[];
|
|
43
45
|
errors: Error[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orioro/util",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"packageManager": "yarn@4.0.2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.mjs",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"dot-prop": "^8.0.2",
|
|
36
36
|
"eventemitter3": "^5.0.1",
|
|
37
|
+
"exponential-backoff": "^3.1.1",
|
|
37
38
|
"fast-copy": "^3.0.2",
|
|
38
39
|
"traverse": "^0.6.9",
|
|
39
40
|
"type-fest": "^4.18.1"
|