@coderich/util 0.0.6 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +21 -4
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.6",
4
+ "version": "0.0.8",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
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);
@@ -29,9 +35,11 @@ exports.flatten = (mixed, spread = true) => {
29
35
  }(el)));
30
36
  };
31
37
 
32
- exports.unflatten = (data) => {
38
+ exports.unflatten = (data, spread = true) => {
33
39
  return exports.map(data, (el) => {
34
- return typeof data === 'object' ? Object.entries(el).reduce((prev, [key, value]) => {
40
+ const type = Object.prototype.toString.call(data);
41
+ const types = spread ? ['[object Object]', '[object Array]'] : ['[object Object]'];
42
+ return types.includes(type) ? Object.entries(el).reduce((prev, [key, value]) => {
35
43
  return set(prev, key, value);
36
44
  }, {}) : el;
37
45
  });
@@ -45,12 +53,21 @@ exports.map = (mixed, fn) => {
45
53
  return isArray ? results : results[0];
46
54
  };
47
55
 
48
- exports.promiseChain = (promiseThunks) => {
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) => {
49
62
  return promiseThunks.reduce((chain, promiseThunk) => {
50
63
  return chain.then((chainResult) => {
51
64
  return promiseThunk(chainResult).then((promiseResult) => {
52
65
  return [...chainResult, promiseResult];
53
66
  });
54
67
  });
55
- }, Promise.resolve([]));
68
+ }, Promise.resolve([startValue]));
69
+ };
70
+
71
+ exports.pipeline = (thunks, startValue) => {
72
+
56
73
  };