UNPKG

gis-tools-ts

Version:

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

85 lines 1.92 kB
import { compareIDs } from '../..'; /** * # Vector Store * * ## Description * A local vector store * * ## Usage * ```ts * import { Vector } from 'gis-tools-ts'; * import type { VectorKey } from 'gis-tools-ts'; * * interface Data extends VectorKey { name: string }; * * const vec = new Vector<Data>(); * // push an entry * vec.push({ cell: 1n, name: 'test' }); * vec.push({ cell: 1n, name: 'test2' }); * // check if a key exists * vec.has(1n); // true * // get length of the store * console.log(vec.length); // 2 * * // iterate over the store * for await (const entry of vec) console.log(entry); * * // close the store * vec.close(); * ``` */ export class Vector { #store = []; /** * Push a value into the store * @param value - the value to store */ push(value) { this.#store.push(value); } /** * @param index - the position in the store to get the value from * @returns the value */ async get(index) { return await this.#store[Number(index)]; } /** * Check if the key exists * @param key - the key * @returns true if the key exists */ has(key) { return this.#store[Number(key)] !== undefined; } /** @returns the length of the store */ get length() { return this.#store.length; } /** * iterate through the values * @yields an iterator */ async *values() { for (const value of this.#store) yield value; } /** Sort the store in place */ sort() { this.#store.sort((a, b) => { return compareIDs(a.cell, b.cell); }); } /** * iterate through the values * @returns an iterator */ [Symbol.asyncIterator]() { return this.values(); } /** Closes the store */ close() { this.#store = []; } } //# sourceMappingURL=index.js.map