dope2/util/defaultmap.js

18 lines
394 B
JavaScript

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();
}
}