@coderich/util 0.0.7 → 0.0.9

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +28 -5
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coderich/util",
3
3
  "main": "src/index.js",
4
- "version": "0.0.7",
4
+ "version": "0.0.9",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
package/src/index.js CHANGED
@@ -2,6 +2,13 @@ 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
+ exports.timeout = ms => new Promise((resolve) => { setTimeout(resolve, ms); });
11
+
5
12
  exports.shellCommand = (cmd, ...args) => {
6
13
  const { status = 0, stdout = '', stderr = '' } = ChildProcess.spawnSync(cmd, args.flat(), { shell: true, encoding: 'utf8' });
7
14
  if (status !== 0) throw new Error(stderr);
@@ -47,12 +54,28 @@ exports.map = (mixed, fn) => {
47
54
  return isArray ? results : results[0];
48
55
  };
49
56
 
50
- exports.promiseChain = (promiseThunks) => {
51
- return promiseThunks.reduce((chain, promiseThunk) => {
52
- return chain.then((chainResult) => {
53
- return promiseThunk(chainResult).then((promiseResult) => {
54
- return [...chainResult, promiseResult];
57
+ exports.mapPromise = (mixed, fn) => {
58
+ const map = exports.map(mixed, fn);
59
+ return Array.isArray(map) ? Promise.all(map) : Promise.resolve(map);
60
+ };
61
+
62
+ exports.promiseChain = (thunks) => {
63
+ return thunks.reduce((promise, thunk) => {
64
+ return promise.then((values) => {
65
+ return Promise.resolve(thunk(values)).then((result) => {
66
+ return [...values, result];
55
67
  });
56
68
  });
57
69
  }, Promise.resolve([]));
58
70
  };
71
+
72
+ exports.pipeline = (thunks, startValue) => {
73
+ let $value = startValue;
74
+
75
+ return thunks.reduce((promise, thunk) => {
76
+ return promise.then((value) => {
77
+ if (value !== undefined) $value = value;
78
+ return Promise.resolve(thunk($value));
79
+ });
80
+ }, Promise.resolve(startValue));
81
+ };