UNPKG

gis-tools-ts

Version:

A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.

97 lines 2.35 kB
import { compareIDs } from '../../index.js'; /** * # MultiMap Store * * ## Description * A local multimap store * * ## Usage * ```ts * import { MultiMap } from 'gis-tools-ts'; * * interface Data { name: string }; * * const mm = new MultiMap<Data>(); * // set a key * mm.set(1n, { name: 'test' }); * mm.set(1n, { name: 'test2' }); * // get a key * const { name } = mm.get(1n); // [{ name: 'test' }, { name: 'test2' }] * // check if a key exists * mm.has(1n); // true * // get length of the store * console.log(mm.length); // 2 * * // iterate over the store * for await (const entry of mm) console.log(entry); * * // close the store * mm.close(); * ``` */ export class MultiMap { #store; #count = 0; /** Builds a new MultiMap */ constructor() { this.#store = new Map(); } /** @returns - the length of the map */ get length() { return this.#count; } /** * Adds a value to the list of values associated with a key * @param key - the key * @param value - the value to store */ set(key, value) { const list = this.get(key); if (list === undefined) { this.#store.set(BigInt(key), [value]); } else { list.push(value); } this.#count++; } /** * Check if the key exists * @param key - the key * @returns true if the key exists */ has(key) { return this.#store.has(BigInt(key)); } /** * Gets the list of values associated with a key * @param key - the key * @returns the list of values if the map contains values for the key */ get(key) { return this.#store.get(BigInt(key)); } /** * iterate through the values * @yields {V} - the values iterator */ async *entries() { const entries = Array.from(this.#store.entries()).map(([id, value]) => [id, value]); entries.sort((a, b) => compareIDs(a[0], b[0])); for (const [key, value] of entries) { yield { key, value }; } } /** * iterate through the values * @returns - an iterator */ [Symbol.asyncIterator]() { return this.entries(); } /** Closes the store */ close() { this.#store.clear(); } } //# sourceMappingURL=index.js.map