All files LruMap.ts

100% Statements 18/18
85.71% Branches 6/7
100% Functions 3/3
100% Lines 18/18

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  2x 2x         22x 22x     108x 108x 108x 19x 108x     70x 70x 11x 1x 11x   59x 59x 59x
export class LruMap<K, V> extends Map<K, V> {
  constructor(
    // 2^30 - 1 (a SMI in V8, for 32-bit platforms)
    public readonly limit: number = 1073741823,
  ) {
    super();
  }
 
  public set(key: K, value: V): this {
    super.delete(key);
    super.set(key, value);
    if (super.size > this.limit) super.delete(super.keys().next().value!);
    return this;
  }
 
  public get(key: K): V | undefined {
    const value = super.get(key)!;
    if (value === void 0) {
      if (super.delete(key)) super.set(key, value);
      return value;
    }
    super.delete(key);
    super.set(key, value);
    return value;
  }
}