@coderich/util 0.0.8 → 0.1.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +19 -11
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.8",
4
+ "version": "0.1.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
package/src/index.js CHANGED
@@ -7,6 +7,7 @@ exports.nvl = (...values) => values.reduce((prev, value) => (prev === null ? val
7
7
  exports.push = (arr, it) => arr[arr.push(it) - 1];
8
8
  exports.filterBy = (arr, fn) => arr.filter((b, index, self) => index === self.findIndex(a => fn(a, b)));
9
9
  exports.ensureArray = a => (Array.isArray(a) ? a : [a].filter(el => el !== undefined));
10
+ exports.timeout = ms => new Promise((resolve) => { setTimeout(resolve, ms); });
10
11
 
11
12
  exports.shellCommand = (cmd, ...args) => {
12
13
  const { status = 0, stdout = '', stderr = '' } = ChildProcess.spawnSync(cmd, args.flat(), { shell: true, encoding: 'utf8' });
@@ -14,14 +15,14 @@ exports.shellCommand = (cmd, ...args) => {
14
15
  return (stderr || stdout).trim();
15
16
  };
16
17
 
17
- exports.flatten = (mixed, spread = true) => {
18
+ exports.flatten = (mixed, options = {}) => {
18
19
  return exports.map(mixed, el => (function flatten(data, obj = {}, path = []) {
19
20
  const type = Object.prototype.toString.call(data);
20
- const types = spread ? ['[object Object]', '[object Array]'] : ['[object Object]'];
21
+ const types = options.safe ? ['[object Object]'] : ['[object Object]', '[object Array]'];
21
22
 
22
23
  if (types.includes(type) && !ObjectId.isValid(data)) {
23
24
  return Object.entries(data).reduce((o, [key, value]) => {
24
- const $key = key.split('.').length > 1 ? `['${key}']` : key;
25
+ const $key = options.strict && key.split('.').length > 1 ? `['${key}']` : key;
25
26
  return flatten(value, o, path.concat($key));
26
27
  }, obj);
27
28
  }
@@ -35,10 +36,10 @@ exports.flatten = (mixed, spread = true) => {
35
36
  }(el)));
36
37
  };
37
38
 
38
- exports.unflatten = (data, spread = true) => {
39
+ exports.unflatten = (data, options = {}) => {
39
40
  return exports.map(data, (el) => {
40
41
  const type = Object.prototype.toString.call(data);
41
- const types = spread ? ['[object Object]', '[object Array]'] : ['[object Object]'];
42
+ const types = options.safe ? ['[object Object]'] : ['[object Object]', '[object Array]'];
42
43
  return types.includes(type) ? Object.entries(el).reduce((prev, [key, value]) => {
43
44
  return set(prev, key, value);
44
45
  }, {}) : el;
@@ -58,16 +59,23 @@ exports.mapPromise = (mixed, fn) => {
58
59
  return Array.isArray(map) ? Promise.all(map) : Promise.resolve(map);
59
60
  };
60
61
 
61
- exports.promiseChain = (promiseThunks, startValue) => {
62
- return promiseThunks.reduce((chain, promiseThunk) => {
63
- return chain.then((chainResult) => {
64
- return promiseThunk(chainResult).then((promiseResult) => {
65
- return [...chainResult, promiseResult];
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];
66
67
  });
67
68
  });
68
- }, Promise.resolve([startValue]));
69
+ }, Promise.resolve([]));
69
70
  };
70
71
 
71
72
  exports.pipeline = (thunks, startValue) => {
73
+ let $value = startValue;
72
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));
73
81
  };