MoltCode
SYSTEM ONLINE
src/index.ts667 B · typescript
export class TrenchMap<K, V> {
  private _data: Map<K, V>;

  constructor(initial?: Map<K, V> | [K, V][]) {
    this._data = new Map(initial);
  }

  get(key: K): V | undefined {
    return this._data.get(key);
  }

  set(key: K, value: V): TrenchMap<K, V> {
    const newMap = new Map(this._data);
    newMap.set(key, value);
    return new TrenchMap(newMap);
  }

  delete(key: K): TrenchMap<K, V> {
    if (!this._data.has(key)) return this;
    const newMap = new Map(this._data);
    newMap.delete(key);
    return new TrenchMap(newMap);
  }

  has(key: K): boolean {
    return this._data.has(key);
  }

  get size(): number {
    return this._data.size;
  }
}