progress and some refactoring

This commit is contained in:
Joeri Exelmans 2025-03-31 15:35:02 +02:00
parent d236eca5e5
commit d8ca2f3999
25 changed files with 376 additions and 163 deletions

18
util/defaultmap.js Normal file
View file

@ -0,0 +1,18 @@
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();
}
}

7
util/pretty.js Normal file
View file

@ -0,0 +1,7 @@
import { inspect } from 'node:util';
export function pretty(obj) {
return inspect(obj, { colors: true, depth: null });
}

31
util/util.js Normal file
View file

@ -0,0 +1,31 @@
// 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;
}
// zip two arrays
export function zip(a, b) {
return a.map((k, i) => [k, b[i]]);
}