@coderich/util 0.1.4 → 0.1.6
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 +23 -1
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const FS = require('fs');
|
|
2
|
+
const Path = require('path');
|
|
1
3
|
const ChildProcess = require('child_process');
|
|
2
4
|
const ObjectId = require('bson-objectid');
|
|
3
5
|
const set = require('lodash.set');
|
|
@@ -13,6 +15,7 @@ exports.push = (arr, it) => arr[arr.push(it) - 1];
|
|
|
13
15
|
exports.filterBy = (arr, fn) => arr.filter((b, index) => index === arr.findIndex(a => fn(a, b)));
|
|
14
16
|
exports.ensureArray = a => (Array.isArray(a) ? a : [a].filter(el => el !== undefined));
|
|
15
17
|
exports.timeout = ms => new Promise((resolve) => { setTimeout(resolve, ms); });
|
|
18
|
+
exports.ucFirst = string => string.charAt(0).toUpperCase() + string.slice(1);
|
|
16
19
|
|
|
17
20
|
exports.filterRe = (arr, fn) => {
|
|
18
21
|
const map = new Map();
|
|
@@ -37,7 +40,7 @@ exports.flatten = (mixed, options = {}) => {
|
|
|
37
40
|
const type = Object.prototype.toString.call(data);
|
|
38
41
|
const types = options.safe ? ['[object Object]'] : ['[object Object]', '[object Array]'];
|
|
39
42
|
|
|
40
|
-
if (depth <= maxDepth && types.includes(type) && !ObjectId.isValid(data)) {
|
|
43
|
+
if (depth <= maxDepth && types.includes(type) && Object.keys(data).length && !ObjectId.isValid(data)) {
|
|
41
44
|
return Object.entries(data).reduce((o, [key, value]) => {
|
|
42
45
|
const $key = options.strict && key.split('.').length > 1 ? `['${key}']` : key;
|
|
43
46
|
return flatten(value, o, path.concat($key), depth + 1);
|
|
@@ -127,3 +130,22 @@ exports.pipeline = (thunks, startValue) => {
|
|
|
127
130
|
});
|
|
128
131
|
}, Promise.resolve(startValue));
|
|
129
132
|
};
|
|
133
|
+
|
|
134
|
+
exports.requireDir = (dir) => {
|
|
135
|
+
const data = {};
|
|
136
|
+
dir = Path.resolve(dir);
|
|
137
|
+
|
|
138
|
+
FS.readdirSync(dir).forEach((filename) => {
|
|
139
|
+
const { name } = Path.parse(filename);
|
|
140
|
+
const path = `${dir}/${filename}`;
|
|
141
|
+
const stat = FS.statSync(path);
|
|
142
|
+
|
|
143
|
+
if (stat && stat.isDirectory()) {
|
|
144
|
+
data[name] = exports.requireDir(path);
|
|
145
|
+
} else if (path.includes('.js')) {
|
|
146
|
+
data[name] = require(path); // eslint-disable-line import/no-dynamic-require, global-require
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
return data;
|
|
151
|
+
};
|