@coderich/util 1.1.3 → 1.2.1

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 +12 -4
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coderich/util",
3
3
  "main": "src/index.js",
4
- "version": "1.1.3",
4
+ "version": "1.2.1",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
package/src/index.js CHANGED
@@ -51,19 +51,20 @@ exports.shellCommand = (cmd, ...args) => {
51
51
 
52
52
  exports.flatten = (mixed, options = {}) => {
53
53
  const maxDepth = options.depth ?? Infinity;
54
+ const ignorePaths = options.ignorePaths || [];
54
55
  const typeFn = options.safe ? exports.isPlainObject : exports.isPlainObjectOrArray;
55
56
 
56
- return exports.map(mixed, el => (function flatten(data, obj = {}, path = [], depth = 0) {
57
- if (depth <= maxDepth && typeFn(data) && Object.keys(data).length) {
57
+ return exports.map(mixed, el => (function flatten(data, obj = {}, path = '', depth = 0) {
58
+ if (depth <= maxDepth && typeFn(data) && Object.keys(data).length && !ignorePaths.some(ip => path.startsWith(ip))) {
58
59
  return Object.entries(data).reduce((o, [key, value]) => {
59
60
  const $key = options.strict && key.split('.').length > 1 ? `['${key}']` : key; // Use for lodash
60
61
  // const $key = options.strict ? key.replaceAll('.', '\\.') : key; // Use for dot-prop
61
- return flatten(value, o, path.concat($key), depth + 1);
62
+ return flatten(value, o, path.concat($key, '.'), depth + 1);
62
63
  }, obj);
63
64
  }
64
65
 
65
66
  if (path.length) {
66
- obj[path.join('.')] = data;
67
+ obj[path.slice(0, -1)] = data;
67
68
  return obj;
68
69
  }
69
70
 
@@ -166,6 +167,13 @@ exports.pathmap = (paths, mixed, fn = v => v) => {
166
167
  return mixed;
167
168
  };
168
169
 
170
+ exports.traverse = (mixed, fn, info) => {
171
+ exports.map(mixed, (data) => {
172
+ const response = fn(data, info);
173
+ if (response) exports.traverse(response.value, fn, response.info);
174
+ });
175
+ };
176
+
169
177
  exports.mapPromise = (mixed, fn) => {
170
178
  const map = exports.map(mixed, fn);
171
179
  return Array.isArray(map) ? Promise.all(map) : Promise.resolve(map);