@coderich/util 0.1.12 → 0.1.14

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 +26 -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.12",
4
+ "version": "0.1.14",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
package/src/index.js CHANGED
@@ -9,13 +9,14 @@ exports.set = set;
9
9
  exports.isEqual = isEqual;
10
10
  exports.ObjectId = ObjectId;
11
11
 
12
+ exports.push = (arr, it) => arr[arr.push(it) - 1];
12
13
  exports.uvl = (...values) => values.reduce((prev, value) => (prev === undefined ? value : prev), undefined);
13
14
  exports.nvl = (...values) => values.reduce((prev, value) => (prev === null ? value : prev), null);
14
- exports.push = (arr, it) => arr[arr.push(it) - 1];
15
15
  exports.filterBy = (arr, fn) => arr.filter((b, index) => index === arr.findIndex(a => fn(a, b)));
16
16
  exports.ensureArray = a => (Array.isArray(a) ? a : [a].filter(el => el !== undefined));
17
17
  exports.timeout = ms => new Promise((resolve) => { setTimeout(resolve, ms); });
18
18
  exports.ucFirst = string => string.charAt(0).toUpperCase() + string.slice(1);
19
+ exports.isScalarValue = value => value !== Object(value);
19
20
  exports.isPlainObjectOrArray = obj => Array.isArray(obj) || exports.isPlainObject(obj);
20
21
 
21
22
  exports.isPlainObject = (obj) => {
@@ -100,6 +101,30 @@ exports.map = (mixed, fn) => {
100
101
  return isArray ? results : results[0];
101
102
  };
102
103
 
104
+ exports.pathmap = (paths, mixed, fn) => {
105
+ if (!exports.isPlainObjectOrArray(mixed)) return mixed;
106
+ if (typeof paths === 'string') paths = paths.split('.');
107
+
108
+ const traverse = (keys, parent) => {
109
+ if (exports.isPlainObjectOrArray(parent)) {
110
+ const key = keys.shift();
111
+ const isProperty = Object.prototype.hasOwnProperty.call(parent, key);
112
+
113
+ if (keys.length) {
114
+ if (isProperty) traverse(keys, parent[key]);
115
+ else if (Array.isArray(parent)) parent.forEach(el => traverse([key, ...keys], el));
116
+ } else if (isProperty) {
117
+ parent[key] = fn(parent[key]);
118
+ } else if (Array.isArray(parent)) {
119
+ parent.forEach(el => (el[key] = exports.map(el[key], v => fn(v))));
120
+ }
121
+ }
122
+ };
123
+
124
+ traverse(paths, mixed);
125
+ return mixed;
126
+ };
127
+
103
128
  exports.mapPromise = (mixed, fn) => {
104
129
  const map = exports.map(mixed, fn);
105
130
  return Array.isArray(map) ? Promise.all(map) : Promise.resolve(map);