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
|
'use strict';
export function create(obj, proto=Object.prototype) {
const props = Object.keys(obj)
.map((key) => [ key, { value: obj[key], enumerable: true } ])
.reduce((acc, [ key, value ]) => ({ ...acc, [key]: value }), {});
return Object.create(proto, props);
};
export function random_choice(collection, r) {
const idx = Math.floor(collection.length * r);
return collection[idx];
}
export function pairs(arr1, arr2) {
return arr1
.map((x, i) => arr2.map(y => [x, y]))
.flat();
}
export function deepEqual(a, b, debug=false) {
if (typeof(a) === 'object') {
if (typeof(b) === 'object') {
// do deep equality
return [...new Set(Object.keys(a).concat(Object.keys(b)))].reduce(
(acc, key) => {
return acc && deepEqual(a[key], b[key]);
},
true
);
} else {
// one object, one non-object
return false;
}
} else {
return a === b;
}
}
|