UNPKG

scats

Version:

Useful scala classes in typescript

71 lines (70 loc) 1.92 kB
import { mutable } from './index.js'; import { AbstractMap } from './abstract-map.js'; export class HashMap extends AbstractMap { map; fromArray(array) { return HashMap.of(...array); } constructor(map) { super(map); this.map = map; } static of(...values) { return new HashMap(new Map(values)); } static from(values) { return HashMap.of(...Array.from(values)); } static empty = new HashMap(new Map()); appendedAll(map) { return this.concat(map); } appended(key, value) { return this.set(key, value); } updated(key, value) { return this.set(key, value); } removed(key) { const next = new Map(this.map); next.delete(key); return new HashMap(next); } removedAll(keys) { const next = new Map(this.map); for (const key of keys) { next.delete(key); } return new HashMap(next); } concat(map) { const mergedMap = new Map([ ...this.entries.toArray, ...map.entries.toArray ]); return new HashMap(mergedMap); } set(key, value) { const next = new Map(this.map); next.set(key, value); return new HashMap(new Map(next)); } updatedWith(key) { const previousValue = this.get(key); return (remappingFunction) => { const nextValue = remappingFunction(previousValue); if (previousValue.isEmpty && nextValue.isEmpty) { return this; } else if (previousValue.isDefined && nextValue.isEmpty) { return this.removed(key); } else { return this.updated(key, nextValue.get); } }; } get toMutable() { return mutable.HashMap.of(...Array.from(this.map.entries())); } }