@coderich/util 0.1.1 → 0.1.3

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 +2 -1
  2. package/src/index.js +44 -1
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coderich/util",
3
3
  "main": "src/index.js",
4
- "version": "0.1.1",
4
+ "version": "0.1.3",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -16,6 +16,7 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "bson-objectid": "2.0.4",
19
+ "lodash.isequal": "4.5.0",
19
20
  "lodash.set": "4.3.2"
20
21
  },
21
22
  "devDependencies": {
package/src/index.js CHANGED
@@ -1,14 +1,26 @@
1
1
  const ChildProcess = require('child_process');
2
2
  const ObjectId = require('bson-objectid');
3
3
  const set = require('lodash.set');
4
+ const isEqual = require('lodash.isequal');
4
5
 
6
+ exports.isEqual = isEqual;
5
7
  exports.uvl = (...values) => values.reduce((prev, value) => (prev === undefined ? value : prev), undefined);
6
8
  exports.nvl = (...values) => values.reduce((prev, value) => (prev === null ? value : prev), null);
7
9
  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)));
10
+ exports.filterBy = (arr, fn) => arr.filter((b, index) => index === arr.findIndex(a => fn(a, b)));
9
11
  exports.ensureArray = a => (Array.isArray(a) ? a : [a].filter(el => el !== undefined));
10
12
  exports.timeout = ms => new Promise((resolve) => { setTimeout(resolve, ms); });
11
13
 
14
+ exports.filterRe = (arr, fn) => {
15
+ const map = new Map();
16
+ const $arr = arr.map(el => fn(el));
17
+ return arr.filter((el, i) => {
18
+ const re = $arr[i];
19
+ if (!map.has(re.source)) map.set(re.source, $arr.findIndex(({ source }) => source.match(re)));
20
+ return map.get(re.source) === i;
21
+ });
22
+ };
23
+
12
24
  exports.shellCommand = (cmd, ...args) => {
13
25
  const { status = 0, stdout = '', stderr = '' } = ChildProcess.spawnSync(cmd, args.flat(), { shell: true, encoding: 'utf8' });
14
26
  if (status !== 0) throw new Error(stderr);
@@ -48,6 +60,27 @@ exports.unflatten = (data, options = {}) => {
48
60
  });
49
61
  };
50
62
 
63
+ exports.changeset = (lhs, rhs) => {
64
+ lhs = lhs ?? {}; rhs = rhs ?? {};
65
+ const [$lhs, $rhs] = [exports.flatten(lhs), exports.flatten(rhs)];
66
+ const changeset = { added: {}, updated: {}, deleted: {} };
67
+
68
+ // Updated + Deleted
69
+ Object.entries($lhs).forEach(([key, value]) => {
70
+ if (Object.prototype.hasOwnProperty.call($rhs, key)) {
71
+ const $value = $rhs[key];
72
+ if (!exports.isEqual(value, $value)) changeset.updated[key] = $value;
73
+ delete $rhs[key];
74
+ } else {
75
+ changeset.deleted[key] = value;
76
+ }
77
+ });
78
+
79
+ // Added
80
+ changeset.added = $rhs;
81
+ return changeset;
82
+ };
83
+
51
84
  exports.map = (mixed, fn) => {
52
85
  if (mixed == null) return mixed;
53
86
  const isArray = Array.isArray(mixed);
@@ -62,6 +95,8 @@ exports.mapPromise = (mixed, fn) => {
62
95
  };
63
96
 
64
97
  exports.promiseChain = (thunks) => {
98
+ if (thunks == null) return Promise.resolve([]);
99
+
65
100
  return thunks.reduce((promise, thunk) => {
66
101
  return promise.then((values) => {
67
102
  return Promise.resolve(thunk(values)).then((result) => {
@@ -71,8 +106,16 @@ exports.promiseChain = (thunks) => {
71
106
  }, Promise.resolve([]));
72
107
  };
73
108
 
109
+ exports.promiseRetry = (fn, ms, retries = 5, cond = e => e) => {
110
+ return fn().catch((e) => {
111
+ if (!retries || !cond(e)) throw e;
112
+ return exports.timeout(ms).then(() => exports.promiseRetry(fn, ms, --retries, cond));
113
+ });
114
+ };
115
+
74
116
  exports.pipeline = (thunks, startValue) => {
75
117
  let $value = startValue;
118
+ if (thunks == null) return Promise.resolve($value);
76
119
 
77
120
  return thunks.reduce((promise, thunk) => {
78
121
  return promise.then((value) => {