@coderich/util 0.1.3 → 0.1.5

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 +25 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coderich/util",
3
3
  "main": "src/index.js",
4
- "version": "0.1.3",
4
+ "version": "0.1.5",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
package/src/index.js CHANGED
@@ -1,15 +1,21 @@
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');
4
6
  const isEqual = require('lodash.isequal');
5
7
 
8
+ exports.set = set;
6
9
  exports.isEqual = isEqual;
10
+ exports.ObjectId = ObjectId;
11
+
7
12
  exports.uvl = (...values) => values.reduce((prev, value) => (prev === undefined ? value : prev), undefined);
8
13
  exports.nvl = (...values) => values.reduce((prev, value) => (prev === null ? value : prev), null);
9
14
  exports.push = (arr, it) => arr[arr.push(it) - 1];
10
15
  exports.filterBy = (arr, fn) => arr.filter((b, index) => index === arr.findIndex(a => fn(a, b)));
11
16
  exports.ensureArray = a => (Array.isArray(a) ? a : [a].filter(el => el !== undefined));
12
17
  exports.timeout = ms => new Promise((resolve) => { setTimeout(resolve, ms); });
18
+ exports.ucFirst = string => string.charAt(0).toUpperCase() + string.slice(1);
13
19
 
14
20
  exports.filterRe = (arr, fn) => {
15
21
  const map = new Map();
@@ -124,3 +130,22 @@ exports.pipeline = (thunks, startValue) => {
124
130
  });
125
131
  }, Promise.resolve(startValue));
126
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
+ };