-
Notifications
You must be signed in to change notification settings - Fork 6
/
Utils.js
60 lines (45 loc) · 1.15 KB
/
Utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module.exports = function (Promise, fsPromise, JSON) {
class Utils {
prop(prop) { return (ob) => ob[prop]; }
extendWith(ob1) { return (ob2) => Object.assign(ob2, ob1); }
capitalize(str) {
if (!str) {
return '';
}
return str.charAt(0).toUpperCase() + str.substring(1);
}
identity(val) { return () => val; }
mapObject(ob, fn) {
const newOb = {};
Object.keys(ob).forEach((key) => {
newOb[key] = fn(ob[key]);
});
return newOb;
}
deepClone(ob) {
return JSON.parse(JSON.stringify(ob));
}
promiseQueue(fns) {
let queue = Promise.resolve(true);
fns.forEach((fn) => {
queue = queue.then(fn);
});
return queue;
}
readFile(filePath) {
return fsPromise
.readFileAsync(filePath, { encoding: 'utf8' });
}
readJsonFile(filePath) {
return this.readFile(filePath)
.then((data) => {
try {
return JSON.parse(data);
} catch (err) {
return Promise.reject(new Error(`Error Parsing JSON: ${err}`));
}
});
}
}
return new Utils();
};