@coderich/util 1.1.2 → 1.2.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 +11 -10
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.2",
4
+ "version": "1.2.0",
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
 
@@ -110,19 +111,19 @@ exports.map = (mixed, fn) => {
110
111
  return isArray ? results : results[0];
111
112
  };
112
113
 
113
- exports.dirmap = (dir, fn = v => v) => {
114
+ exports.dirmap = (dir, fn) => {
114
115
  const data = {};
115
116
  dir = Path.resolve(dir);
116
117
 
117
118
  FS.readdirSync(dir).forEach((filename) => {
118
119
  const { name } = Path.parse(filename);
119
120
  const path = `${dir}/${filename}`;
120
- const stat = FS.statSync(path);
121
+ const stats = FS.statSync(path);
121
122
 
122
- if (stat && stat.isDirectory()) {
123
- data[name] = exports.dirmap(path, fn);
124
- } else {
125
- data[name] = fn(path);
123
+ if (stats?.isDirectory()) {
124
+ data[name] = { ...fn?.({ stats, path }), ...exports.dirmap(path, fn) };
125
+ } else if (fn) {
126
+ data[name] = fn({ stats, path });
126
127
  }
127
128
  });
128
129