@coderich/util 0.1.13 → 0.1.15

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 +24 -0
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.13",
4
+ "version": "0.1.15",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
package/src/index.js CHANGED
@@ -101,6 +101,30 @@ exports.map = (mixed, fn) => {
101
101
  return isArray ? results : results[0];
102
102
  };
103
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] = fn(el[key])));
120
+ }
121
+ }
122
+ };
123
+
124
+ traverse(paths, mixed);
125
+ return mixed;
126
+ };
127
+
104
128
  exports.mapPromise = (mixed, fn) => {
105
129
  const map = exports.map(mixed, fn);
106
130
  return Array.isArray(map) ? Promise.all(map) : Promise.resolve(map);