-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.js
39 lines (33 loc) · 872 Bytes
/
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
// Returns the value of an object's key if it exists.
exports.getKey = (obj, key) => {
if (obj && obj[key]) {
return obj[key];
} else {
return obj;
}
};
// Returns a new instance of the module with a wrapper applied.
exports.wrap = (target, wrapper) => {
let wrapped = { _get: target._get };
for (let prop in target) {
if (wrapped[prop] === undefined) {
wrapped[prop] = wrapper(target[prop]);
}
}
return wrapped;
};
// A wrapper to initialize the options object.
exports.initParams = (fn) => {
return (options, callback) => {
options.id = options.id || options.name;
options._query = {};
return fn(options, callback);
};
};
// Returns new object with specific keys copied from passed object
exports.pick = (obj, keys) => {
return keys.reduce((acc, key) => {
acc[key] = obj[key];
return acc;
}, {});
};