All files / collections/src mappedSet.ts

94.44% Statements 17/18
50% Branches 1/2
88.88% Functions 8/9
94.11% Lines 16/17

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 45  2x 2x   17x 2x 2x     15x 15x 15x             22x 22x       1x       2x       2x       7x       14x     15x    
import { Mapper } from "@benneq/function";
import { map } from "@benneq/iterable";
import { AbstractSet } from "./abstractSet";
 
export class MappedSet<T> extends AbstractSet<T> {
  #mapper;
  #value;
 
  constructor(mapper: Mapper<T, unknown>, iterable?: Iterable<T>) {
    super();
    this.#mapper = mapper;
    this.#value = new Map(
      iterable &&
        map<T, [unknown, T]>((value) => [mapper(value), value])(iterable)
    );
  }
 
  add(value: T): this {
    this.#value.set(this.#mapper(value), value);
    return this;
  }
 
  clear(): void {
    this.#value.clear();
  }
 
  delete(value: T): boolean {
    return this.#value.delete(this.#mapper(value));
  }
 
  has(value: T): boolean {
    return this.#value.has(this.#mapper(value));
  }
 
  get size(): number {
    return this.#value.size;
  }
 
  values(): IterableIterator<T> {
    return this.#value.values();
  }
 
  [Symbol.toStringTag] = "MappedSet";
}