gis-tools-ts
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
82 lines • 1.83 kB
JavaScript
/**
* # Key-Value Store
*
* ## Description
* A local key-value store
*
* ## Usage
* ```ts
* import { KV } from 'gis-tools-ts';
*
* interface Data { name: string };
*
* const kv = new KV<Data>();
* // set a key
* kv.set(1n, { name: 'test' });
* // get a key
* const { name } = kv.get(1n); // { name: 'test' }
* // check if a key exists
* kv.has(1n); // true
* // get length of the store
* console.log(kv.length); // 1
*
* // iterate over the store
* for await (const value of kv) console.log(value);
*
* // clear the store
* kv.close();
* ```
*/
export class KV {
#store = new Map();
/** @returns - the length of the map */
get length() {
return this.#store.size;
}
/**
* 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));
}
/**
* Check if the key exists
* @param key - the key
* @returns true if the key exists
*/
has(key) {
return this.#store.has(BigInt(key));
}
/**
* 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) {
this.#store.set(BigInt(key), value);
}
/**
* iterate through the values
* @yields {V} - the values iterator
*/
async *values() {
for await (const [, value] of this.#store.entries())
yield value;
}
/**
* iterate through the values
* @returns an iterator
*/
[Symbol.asyncIterator]() {
return this.values();
}
/**
* Closes the store
*/
close() {
this.#store.clear();
}
}
//# sourceMappingURL=index.js.map