-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_utils.js
54 lines (47 loc) · 1.08 KB
/
data_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
const crypto = require('crypto');
function cmp(a, b) {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}
function cmpKeys(keys) {
return (a, b) => {
for (const k of keys) {
const res = cmp(a[k], b[k]);
if (res !== 0) return res;
}
return 0;
};
}
function sha2(str) {
return crypto.createHash('sha256').update(str).digest('hex');
}
// From https://gist.github.com/egardner/efd34f270cc33db67c0246e837689cb9
function deepEqual(obj1, obj2) {
if (obj1 === obj2) {
return true;
} else if (_isObject(obj1) && _isObject(obj2)) {
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
return false;
}
for (const prop in obj1) {
if (!deepEqual(obj1[prop], obj2[prop])) {
return false;
}
}
return true;
}
function _isObject(obj) {
return typeof obj === 'object' && obj != null;
}
}
module.exports = {
cmp,
cmpKeys,
deepEqual,
sha2,
};