File size: 304 Bytes
4327358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
export class DefaultMap<K, T> extends Map<K, T> {
  private readonly factory: (key: K) => T;

  constructor(factory: (key: K) => T) {
    super();
    this.factory = factory;
  }

  get(key: K): T {
    if (!this.has(key)) {
      this.set(key, this.factory(key));
    }
    return super.get(key);
  }
}