-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenumerable-keys-if-same.mjs
71 lines (51 loc) · 1.7 KB
/
enumerable-keys-if-same.mjs
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
61
62
63
64
65
66
67
68
69
70
// @ts-check
const basePropertyIsEnumerable = Object.prototype.propertyIsEnumerable;
export function enumerableKeysIfSame(a, b) {
if (a === undefined) {
throw new TypeError('can\'t convert undefined to object');
}
if (a === null) {
throw new TypeError('can\'t convert null to object');
}
if (b === undefined) {
throw new TypeError('can\'t convert undefined to object');
}
if (b === null) {
throw new TypeError('can\'t convert null to object');
}
if (typeof a !== typeof b) {
throw new TypeError('Type mismatch: `a` is of type "' + (typeof a) + '" but `b` is of type "' + (typeof b) + '"');
}
const keysA = Object.keys(a);
const keysB = Object.keys(b);
const keysCount = keysA.length >>> 0;
if (keysCount !== keysB.length) {
return null;
}
if (keysCount !== 0) {
if (keysCount !== (new Set(keysA.concat(keysB)).size)) {
return null;
}
}
if ((typeof a !== 'object') && (typeof a !== 'function')) {
// Primitive types can't have own symbol keys
return keysA;
}
const allSyms = new Set(Object.getOwnPropertySymbols(a).concat(Object.getOwnPropertySymbols(b)));
if (allSyms.size === 0) {
return keysA;
}
const aHasEnumerable = basePropertyIsEnumerable.bind(a);
const bHasEnumerable = basePropertyIsEnumerable.bind(b);
const enumerableKeys = (keysCount !== 0) ? keysA : [];
for (const sym of allSyms) {
const aHas = aHasEnumerable(sym);
if (aHas !== bHasEnumerable(sym)) {
return null;
}
if (aHas) {
enumerableKeys.push(sym);
}
}
return enumerableKeys;
}