UNPKG

low-level

Version:
94 lines 2.27 kB
import { Uint } from "./uint.js"; import { AbstractIterator } from "./utils.js"; class BMapEntriesIterator extends AbstractIterator { CLS; constructor(CLS, mapEntries) { super(mapEntries); this.CLS = CLS; } _next(value) { return [this.CLS.from(value[0], "hex"), value[1]]; } } class BMapKeysIterator extends AbstractIterator { CLS; constructor(CLS, mapEntries) { super(mapEntries); this.CLS = CLS; } _next(value) { return this.CLS.from(value, "hex"); } } class BMapValuesIterator extends AbstractIterator { constructor(mapEntries) { super(mapEntries); } _next(value) { return value; } } export class BasicBinaryMap { CLS; store = {}; constructor(CLS, entries) { this.CLS = CLS; if (entries) { for (const [key, value] of entries) { this.set(key, value); } } } get size() { return Object.keys(this.store).length; } get(key) { return this.store[key.toHex()]; } set(key, value) { return this.store[key.toHex()] = value; } delete(key) { const hasKey = this.has(key); delete this.store[key.toHex()]; return hasKey; } has(key) { return key.toHex() in this.store; } [Symbol.iterator]() { return this.entries(); } entries() { return new BMapEntriesIterator(this.CLS, Object.entries(this.store).values()); } keys() { return new BMapKeysIterator(this.CLS, Object.keys(this.store).values()); } values() { return new BMapValuesIterator(Object.values(this.store).values()); } forEach(callbackfn, thisArg) { for (const [key, value] of this.entries()) { callbackfn.call(thisArg, value, key); } ; } clear() { for (const key of this.keys()) { this.delete(key); } } getStringTag() { return this.constructor.name; } get [Symbol.toStringTag]() { return this.getStringTag(); } } export class UintMap extends BasicBinaryMap { constructor(entries) { super(Uint, entries); } } //# sourceMappingURL=map.js.map