/*-------------------------------------------------------------------------- @alkdev/typebox/collections The MIT License (MIT) Copyright (c) 2017-2026 Haydn Paterson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ---------------------------------------------------------------------------*/ import { TypeCheck, TypeCompiler, ValueError } from '@alkdev/typebox/compiler' import { TSchema, Static, TypeBoxError } from '@alkdev/typebox' import { Value } from '@alkdev/typebox/value' // ---------------------------------------------------------------- // TypeMapKeyError // ---------------------------------------------------------------- export class TypeMapKeyError extends TypeBoxError { constructor(message: string) { super(`${message} for key`) } } export class TypeMapValueError extends TypeBoxError { constructor(key: unknown, message: string) { super(`${message} for key ${JSON.stringify(key)}`) } } // ---------------------------------------------------------------- // TypeMap // ---------------------------------------------------------------- // prettier-ignore type TypeMapEntries = | Iterable<[Static, Static]> | Array<[Static, Static]> /** Runtime type checked Map collection */ export class TypeMap { readonly #keycheck: TypeCheck readonly #valuecheck: TypeCheck readonly #keys: Map> readonly #values: Map> /** Constructs a new HashMap of the given key and value types. */ constructor(key: K, value: V, entries: TypeMapEntries = []) { this.#keycheck = TypeCompiler.Compile(key) this.#valuecheck = TypeCompiler.Compile(value) this.#keys = new Map>() this.#values = new Map>() for (const [key, value] of entries) { this.set(key, value) } } /** Iterator for this TypeMap */ public *[Symbol.iterator](): IterableIterator<[Static, Static]> { for (const [key, value] of this.#values) { yield [this.#keys.get(key)!, value] } } /** Iterator for the keys in this TypeMap */ public *keys(): IterableIterator> { yield* this.#keys.values() } /** Iterator for the values in this TypeMap */ public *values(): IterableIterator> { yield* this.#values.values() } /** Clears all entries in this map */ public clear(): void { this.#values.clear() this.#keys.clear() } /** Executes a provided function once per each key/value pair in the Map, in insertion order. */ public forEach(callbackfn: (value: Static, key: Static, map: TypeMap) => void, thisArg?: any): void { this.#values.forEach((value, key) => callbackfn(value, this.#keys.get(key)!, this)) } /** @returns the number of elements in the TypeMap. */ public get size(): number { return this.#values.size } /** * @returns boolean indicating whether an element with the specified key exists or not. */ public has(key: Static): boolean { this.#assertKey(key) return this.#values.has(this.#encodeKey(key)) } /** * Returns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map. * @returns Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned. */ public get(key: Static): Static | undefined { this.#assertKey(key) return this.#values.get(this.#encodeKey(key))! } /** * Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated. */ public set(key: Static, value: Static) { this.#assertKey(key) this.#assertValue(key, value) const encodedKey = this.#encodeKey(key) this.#keys.set(encodedKey, key) this.#values.set(encodedKey, value) } /** * @returns true if an element in the Map existed and has been removed, or false if the element does not exist. */ public delete(key: Static): boolean { this.#assertKey(key) const encodedKey = this.#encodeKey(key) this.#keys.delete(encodedKey) return this.#values.delete(encodedKey) } // --------------------------------------------------- // Encoder // --------------------------------------------------- /** Encodes the key as a 64bit numeric */ #encodeKey(key: Static) { return Value.Hash(key) } // --------------------------------------------------- // Assertions // --------------------------------------------------- #formatError(errors: ValueError[]) { return errors .map((error) => `${error.message} ${error.path}`) .join('. ') .trim() } /** Asserts the key matches the key schema */ #assertKey(key: unknown): asserts key is Static { if (this.#keycheck.Check(key)) return throw new TypeMapKeyError(this.#formatError([...this.#keycheck.Errors(key)])) } /** Asserts the key matches the value schema */ #assertValue(key: Static, value: unknown): asserts value is Static { if (this.#valuecheck.Check(value)) return throw new TypeMapValueError(key, this.#formatError([...this.#valuecheck.Errors(value)])) } }