@coderich/util 0.1.1 → 0.1.2
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/package.json +1 -1
- package/src/index.js +21 -1
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -5,10 +5,20 @@ const set = require('lodash.set');
|
|
|
5
5
|
exports.uvl = (...values) => values.reduce((prev, value) => (prev === undefined ? value : prev), undefined);
|
|
6
6
|
exports.nvl = (...values) => values.reduce((prev, value) => (prev === null ? value : prev), null);
|
|
7
7
|
exports.push = (arr, it) => arr[arr.push(it) - 1];
|
|
8
|
-
exports.filterBy = (arr, fn) => arr.filter((b, index
|
|
8
|
+
exports.filterBy = (arr, fn) => arr.filter((b, index) => index === arr.findIndex(a => fn(a, b)));
|
|
9
9
|
exports.ensureArray = a => (Array.isArray(a) ? a : [a].filter(el => el !== undefined));
|
|
10
10
|
exports.timeout = ms => new Promise((resolve) => { setTimeout(resolve, ms); });
|
|
11
11
|
|
|
12
|
+
exports.filterRe = (arr, fn) => {
|
|
13
|
+
const map = new Map();
|
|
14
|
+
const $arr = arr.map(el => fn(el));
|
|
15
|
+
return arr.filter((el, i) => {
|
|
16
|
+
const re = $arr[i];
|
|
17
|
+
if (!map.has(re.source)) map.set(re.source, $arr.findIndex(({ source }) => source.match(re)));
|
|
18
|
+
return map.get(re.source) === i;
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
12
22
|
exports.shellCommand = (cmd, ...args) => {
|
|
13
23
|
const { status = 0, stdout = '', stderr = '' } = ChildProcess.spawnSync(cmd, args.flat(), { shell: true, encoding: 'utf8' });
|
|
14
24
|
if (status !== 0) throw new Error(stderr);
|
|
@@ -62,6 +72,8 @@ exports.mapPromise = (mixed, fn) => {
|
|
|
62
72
|
};
|
|
63
73
|
|
|
64
74
|
exports.promiseChain = (thunks) => {
|
|
75
|
+
if (thunks == null) return Promise.resolve([]);
|
|
76
|
+
|
|
65
77
|
return thunks.reduce((promise, thunk) => {
|
|
66
78
|
return promise.then((values) => {
|
|
67
79
|
return Promise.resolve(thunk(values)).then((result) => {
|
|
@@ -71,8 +83,16 @@ exports.promiseChain = (thunks) => {
|
|
|
71
83
|
}, Promise.resolve([]));
|
|
72
84
|
};
|
|
73
85
|
|
|
86
|
+
exports.promiseRetry = (fn, ms, retries = 5, cond = e => e) => {
|
|
87
|
+
return fn().catch((e) => {
|
|
88
|
+
if (!retries || !cond(e)) throw e;
|
|
89
|
+
return exports.timeout(ms).then(() => exports.promiseRetry(fn, ms, --retries, cond));
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
|
|
74
93
|
exports.pipeline = (thunks, startValue) => {
|
|
75
94
|
let $value = startValue;
|
|
95
|
+
if (thunks == null) return Promise.resolve($value);
|
|
76
96
|
|
|
77
97
|
return thunks.reduce((promise, thunk) => {
|
|
78
98
|
return promise.then((value) => {
|