/** * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose */ export interface DefaultMap extends Map { /** Return the value for `key` when available or the default value otherwise. */ getDefault: (key: K) => V } /** A `Map` instance with a `getDefault` method added. */ export function DefaultMap(valueCtor: () => V) { const map = new Map() as DefaultMap; map.getDefault = (key: K) => { if (map.has(key)) return map.get(key)!; const value = valueCtor(); map.set(key, value); return value; }; return map; } // TODO currently not working, see https://github.com/Microsoft/TypeScript/issues/10853 // /** A `Map` with a `getDefault` method added. */ // export class DefaultMap extends Map { // constructor(private valueCtor: () => V, entries?: ReadonlyArray<[K, V]>) { // super(entries) // } // /** Return the value for `key` when available or the default value otherwise. */ // getDefault(key: K) { // if (this.has(key)) return this.get(key)! // const value = this.valueCtor() // this.set(key, value) // return value // } // }