UNPKG

@rimbu/proximity

Version:

Immutable ProximityMap implementation for TypeScript

104 lines 3.11 kB
import { OptLazy, } from '@rimbu/common'; import { findNearestKeyMatch } from '@rimbu/proximity/common'; import { wrapHashMap } from '../wrapping.mjs'; const toStringBeginning = /^[^(]+/; export class ProximityMapNonEmpty { constructor(context, internalMap) { this.context = context; this.internalMap = internalMap; this.isEmpty = false; } plugInternalMap(newInternalMap) { if (newInternalMap == this.internalMap) { return this; } return wrapHashMap(this.context, newInternalMap); } get size() { return this.internalMap.size; } stream() { return this.internalMap.stream(); } addEntries(entries) { return this.plugInternalMap(this.internalMap.addEntries(entries)); } updateAt(key, update) { return this.plugInternalMap(this.internalMap.updateAt(key, update)); } nonEmpty() { return true; } assumeNonEmpty() { return this; } asNormal() { return this; } streamKeys() { return this.internalMap.streamKeys(); } streamValues() { return this.internalMap.streamValues(); } mapValues(mapFun) { return new ProximityMapNonEmpty(this.context, this.internalMap.mapValues(mapFun)); } toArray() { return this.internalMap.toArray(); } get(key, otherwise) { const keyMatch = findNearestKeyMatch(this.context.distanceFunction, key, this.internalMap); return keyMatch ? keyMatch.value : OptLazy(otherwise); } hasKey(key) { return this.internalMap.hasKey(key); } removeKey(key) { return this.plugInternalMap(this.internalMap.removeKey(key)); } removeKeys(keys) { return this.plugInternalMap(this.internalMap.removeKeys(keys)); } removeKeyAndGet(key) { const internalResult = this.internalMap.removeKeyAndGet(key); if (!internalResult) { return undefined; } const [newInternalMap, value] = internalResult; return [this.plugInternalMap(newInternalMap), value]; } forEach(f, options = {}) { return this.internalMap.forEach(f, options); } filter(pred, options = {}) { return this.plugInternalMap(this.internalMap.filter(pred, options)); } toString() { return this.internalMap .toString() .replace(toStringBeginning, this.context.typeTag); } toJSON() { return { dataType: this.context.typeTag, value: this.toArray(), }; } [Symbol.iterator]() { return this.internalMap[Symbol.iterator](); } set(key, value) { return this.plugInternalMap(this.internalMap.set(key, value)); } addEntry(entry) { return this.plugInternalMap(this.internalMap.addEntry(entry)); } modifyAt(atKey, options) { return this.plugInternalMap(this.internalMap.modifyAt(atKey, options)); } toBuilder() { return this.context.createBuilder(this); } } //# sourceMappingURL=NonEmpty.mjs.map