dope2/util.js

76 lines
2.1 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;
}
// zip two arrays
export function zip(a, b) {
return a.map((k, i) => [k, b[i]]);
}
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';
import { symbolFunction, symbolList, symbolProduct, symbolSum } from './structures/types.js';
export function pretty(obj) {
return inspect(obj, {colors: true, depth: null});
}
export function prettyT(type) {
if (type.typeVars) {
return `${[...type.typeVars].map(prettyT).join(", ")}: ${prettyT(type.type)}`;
}
if (type.symbol === symbolFunction) {
return `${prettyT(type.params[0])} -> ${prettyT(type.params[1])}`;
}
if (type.symbol === symbolList) {
return `[${prettyT(type.params[0])}]`;
}
if (type.symbol === symbolProduct) {
return `(${prettyT(type.params[0])}, ${prettyT(type.params[1])})`;
}
if (type.symbol === symbolSum) {
return `(${prettyT(type.params[0])} | ${prettyT(type.params[1])})`;
}
if (type.params.length === 0) {
return type.symbol.description;
}
return `${type.symbol.description}(${type.params.map(prettyT).join(", ")})`;
}