49 lines
No EOL
1.2 KiB
JavaScript
49 lines
No EOL
1.2 KiB
JavaScript
// re-inventing the wheel:
|
|
export function deepEqual(a, b) {
|
|
if (a === b) return true; // <- shallow equality and primitives
|
|
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) {
|
|
return false;
|
|
}
|
|
if (Array.isArray(a) && Array.isArray(b)) {
|
|
if (a.length !== b.length) return false;
|
|
for (let i = 0; i < a.length; i++) {
|
|
if (!deepEqual(a[i], b[i])) return false;
|
|
}
|
|
return true;
|
|
}
|
|
const keysA = Object.keys(a);
|
|
const keysB = Object.keys(b);
|
|
if (keysA.length !== keysB.length) return false;
|
|
for (let key of keysA) {
|
|
if (!keysB.includes(key) || !deepEqual(a[key], b[key])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
export class DefaultMap {
|
|
constructor(defaultValue, ...rest) {
|
|
this.defaultValue = defaultValue;
|
|
this.m = new Map(rest);
|
|
}
|
|
getdefault(key, addToMapping=false) {
|
|
return this.m.get(key) || (() => {
|
|
const val = this.defaultValue(key);
|
|
if (addToMapping)
|
|
this.m.set(key, val);
|
|
return val;
|
|
})();
|
|
}
|
|
entries() {
|
|
return this.m.entries();
|
|
}
|
|
}
|
|
|
|
|
|
import { inspect } from 'node:util';
|
|
|
|
export function pretty(obj) {
|
|
return inspect(obj, {colors: true, depth: null});
|
|
} |