UNPKG

@rimbu/bimap

Version:

A bidirectional immutable Map of keys and values for TypeScript

65 lines 2.31 kB
import { BiMapBuilder, BiMapEmpty, BiMapNonEmptyImpl, } from '@rimbu/bimap/custom'; import { Reducer } from '@rimbu/stream'; import { isEmptyStreamSourceInstance } from '@rimbu/stream/custom'; export class BiMapContext { constructor(keyValueContext, valueKeyContext) { this.keyValueContext = keyValueContext; this.valueKeyContext = valueKeyContext; this._empty = Object.freeze(new BiMapEmpty(this)); this.empty = () => { return this._empty; }; this.of = (...entries) => { return this.from(entries); }; this.from = (...sources) => { if (sources.length === 1) { const source = sources[0]; if (source instanceof BiMapNonEmptyImpl && source.context === this) return source; } let builder = this.builder(); let i = -1; const length = sources.length; while (++i < length) { const source = sources[i]; if (isEmptyStreamSourceInstance(source)) continue; if (builder.isEmpty && source instanceof BiMapNonEmptyImpl && source.context === this) { if (i === length - 1) return source; builder = source.toBuilder(); continue; } builder.addEntries(source); } return builder.build(); }; this.builder = () => { return new BiMapBuilder(this); }; this.reducer = (source) => { return Reducer.create(() => undefined === source ? this.builder() : this.from(source).toBuilder(), (builder, entry) => { builder.addEntry(entry); return builder; }, (builder) => builder.build()); }; } get typeTag() { return 'BiMap'; } get _types() { return undefined; } createNonEmptyImpl(keyValueMap, valueKeyMap) { return new BiMapNonEmptyImpl(this, keyValueMap, valueKeyMap); } createBuilder(source) { return new BiMapBuilder(this, source); } } //# sourceMappingURL=context.mjs.map