-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
35 lines (31 loc) · 922 Bytes
/
utils.js
File metadata and controls
35 lines (31 loc) · 922 Bytes
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
'use strict';
const getValue = (obj, requiredKey) => {
const entries = Object.entries(obj);
const l = entries.length;
for (let i = 0; i < l; i++) {
const [ key, value ] = entries[i];
if (requiredKey.toLowerCase() === key.toLowerCase()) return value;
}
};
const isEmptyObject = (obj) => {
return Object.keys(obj).length === 0;
};
const diff = (obj1, obj2, keysOnly = false) => {
const result = {};
let change;
Object.keys(obj1).forEach(key => {
if (obj2[key] && obj1[key] && typeof obj2[key] === 'object' && typeof obj1[key] === 'object') {
change = diff(obj1[key], obj2[key], keysOnly);
if (isEmptyObject(change) === false) result[key] = change;
}
else if ((keysOnly && typeof obj2[key] === 'undefined') || (!keysOnly && obj2[key] !== obj1[key])) {
result[key] = obj2[key];
}
});
return result;
};
module.exports = {
getValue,
diff,
isEmptyObject
};