@coderich/util 1.0.7 → 1.1.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 +21 -15
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coderich/util",
3
3
  "main": "src/index.js",
4
- "version": "1.0.7",
4
+ "version": "1.1.1",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
package/src/index.js CHANGED
@@ -110,6 +110,25 @@ exports.map = (mixed, fn) => {
110
110
  return isArray ? results : results[0];
111
111
  };
112
112
 
113
+ exports.dirmap = (dir, fn = v => v) => {
114
+ const data = {};
115
+ dir = Path.resolve(dir);
116
+
117
+ FS.readdirSync(dir).forEach((filename) => {
118
+ const { name } = Path.parse(filename);
119
+ const path = `${dir}/${filename}`;
120
+ const stat = FS.statSync(path);
121
+
122
+ if (stat && stat.isDirectory()) {
123
+ data[name] = exports.dirmap(path);
124
+ } else {
125
+ data[name] = fn(path);
126
+ }
127
+ });
128
+
129
+ return data;
130
+ };
131
+
113
132
  exports.pathmap = (paths, mixed, fn = v => v) => {
114
133
  if (!exports.isPlainObjectOrArray(mixed)) return mixed;
115
134
  if (typeof paths === 'string') paths = paths.split('.');
@@ -184,22 +203,9 @@ exports.pipeline = (thunks, startValue) => {
184
203
  };
185
204
 
186
205
  exports.requireDir = (dir) => {
187
- const data = {};
188
- dir = Path.resolve(dir);
189
-
190
- FS.readdirSync(dir).forEach((filename) => {
191
- const { name } = Path.parse(filename);
192
- const path = `${dir}/${filename}`;
193
- const stat = FS.statSync(path);
194
-
195
- if (stat && stat.isDirectory()) {
196
- data[name] = exports.requireDir(path);
197
- } else if (path.includes('.js')) {
198
- data[name] = require(path); // eslint-disable-line import/no-dynamic-require, global-require
199
- }
206
+ return exports.dirmap(dir, (path) => {
207
+ return path.includes('.js') ? require(path) : path; // eslint-disable-line import/no-dynamic-require, global-require
200
208
  });
201
-
202
- return data;
203
209
  };
204
210
 
205
211
  exports.parseRegExp = (mixed) => {