UNPKG

435 BJavaScriptView Raw
1// @flow strict-local
2
3export default class DefaultMap<K, V> extends Map<K, V> {
4 _getDefault: K => V;
5
6 constructor(getDefault: K => V, entries?: Iterable<[K, V]>) {
7 super(entries);
8 this._getDefault = getDefault;
9 }
10
11 get(key: K): V {
12 let ret;
13 if (this.has(key)) {
14 ret = super.get(key);
15 } else {
16 ret = this._getDefault(key);
17 this.set(key, ret);
18 }
19
20 // $FlowFixMe
21 return ret;
22 }
23}