@coderich/util 0.0.7 → 0.0.8
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 +17 -2
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -2,6 +2,12 @@ const ChildProcess = require('child_process');
|
|
|
2
2
|
const ObjectId = require('bson-objectid');
|
|
3
3
|
const set = require('lodash.set');
|
|
4
4
|
|
|
5
|
+
exports.uvl = (...values) => values.reduce((prev, value) => (prev === undefined ? value : prev), undefined);
|
|
6
|
+
exports.nvl = (...values) => values.reduce((prev, value) => (prev === null ? value : prev), null);
|
|
7
|
+
exports.push = (arr, it) => arr[arr.push(it) - 1];
|
|
8
|
+
exports.filterBy = (arr, fn) => arr.filter((b, index, self) => index === self.findIndex(a => fn(a, b)));
|
|
9
|
+
exports.ensureArray = a => (Array.isArray(a) ? a : [a].filter(el => el !== undefined));
|
|
10
|
+
|
|
5
11
|
exports.shellCommand = (cmd, ...args) => {
|
|
6
12
|
const { status = 0, stdout = '', stderr = '' } = ChildProcess.spawnSync(cmd, args.flat(), { shell: true, encoding: 'utf8' });
|
|
7
13
|
if (status !== 0) throw new Error(stderr);
|
|
@@ -47,12 +53,21 @@ exports.map = (mixed, fn) => {
|
|
|
47
53
|
return isArray ? results : results[0];
|
|
48
54
|
};
|
|
49
55
|
|
|
50
|
-
exports.
|
|
56
|
+
exports.mapPromise = (mixed, fn) => {
|
|
57
|
+
const map = exports.map(mixed, fn);
|
|
58
|
+
return Array.isArray(map) ? Promise.all(map) : Promise.resolve(map);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
exports.promiseChain = (promiseThunks, startValue) => {
|
|
51
62
|
return promiseThunks.reduce((chain, promiseThunk) => {
|
|
52
63
|
return chain.then((chainResult) => {
|
|
53
64
|
return promiseThunk(chainResult).then((promiseResult) => {
|
|
54
65
|
return [...chainResult, promiseResult];
|
|
55
66
|
});
|
|
56
67
|
});
|
|
57
|
-
}, Promise.resolve([]));
|
|
68
|
+
}, Promise.resolve([startValue]));
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
exports.pipeline = (thunks, startValue) => {
|
|
72
|
+
|
|
58
73
|
};
|