All files TupleMap.ts

100% Statements 23/23
100% Branches 4/4
100% Functions 5/5
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 451x       1x       5x 5x 9x 2x   7x   3x       3x 3x 3x 3x 1x   2x 2x 2x     3x       5x 5x 9x 2x   7x   3x      
export class TupleMap<T extends any[], V> {
  private map: Map<any, any>;
 
  constructor() {
    this.map = new Map();
  }
 
  get(keys: T): V | undefined {
    let map = this.map;
    for (const key of keys) {
      if (!map.has(key)) {
        return undefined;
      }
      map = map.get(key);
    }
    return map as any;
  }
 
  set(keys: T, value: V): void {
    const lastKey = keys[keys.length - 1];
    let map = this.map;
    keys.slice(0, -1).forEach(key => {
      if (map.has(key)) {
        map = map.get(key);
      } else {
        const newMap = new Map();
        map.set(key, newMap);
        map = newMap;
      }
    });
    map.set(lastKey, value);
  }
 
  has(keys: T): boolean {
    let map = this.map;
    for (const key of keys) {
      if (!map.has(key)) {
        return false;
      }
      map = map.get(key);
    }
    return true;
  }
}