@coderich/util 1.0.6 → 1.1.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.
- package/package.json +1 -1
- package/src/index.js +22 -16
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -30,7 +30,7 @@ exports.findAndReplace = (arr, fn, ...items) => {
|
|
|
30
30
|
exports.isPlainObject = (obj) => {
|
|
31
31
|
if (obj == null || Array.isArray(obj)) return false;
|
|
32
32
|
const proto = Object.getPrototypeOf(obj);
|
|
33
|
-
return proto === Object.prototype || proto?.toString?.call?.(obj) === '[object Object]';
|
|
33
|
+
return (proto == null || proto === Object.prototype || proto?.toString?.call?.(obj) === '[object Object]');
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
exports.filterRe = (arr, fn) => {
|
|
@@ -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 if (path.includes('.js')) {
|
|
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
|
-
|
|
188
|
-
|
|
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) => {
|