/*-------------------------------------------------------------------------- @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' // ---------------------------------------------------------------- // Errors // ---------------------------------------------------------------- export class TypeSetError extends TypeBoxError { constructor(message: string) { super(`${message}`) } } // ---------------------------------------------------------------- // TypeSet // ---------------------------------------------------------------- /** Runtime type checked Set collection */ export class TypeSet { readonly #valuecheck: TypeCheck readonly values: Map> constructor(schema: T, iterable: Array | Iterable = []) { this.#valuecheck = TypeCompiler.Compile(schema) this.values = new Map>() for (const value of iterable) { this.add(value) } } /** Adds a value to this set */ public add(value: Static): this { this.#assertValue(value) this.values.set(this.#encodeKey(value), value) return this } /** Clears the values in this set */ public clear(): void { this.values.clear() } /** * Removes a specified value from the Set. * @returns Returns true if an element in the Set existed and has been removed, or false if the element does not exist. */ public delete(value: Static): boolean { return this.values.delete(this.#encodeKey(value)) } /** Executes a provided function once per each value in the Set object, in insertion order. */ public forEach(callbackfn: (value: Static, value2: Static, set: TypeSet) => void, thisArg?: any): void { this.values.forEach((value, value2) => callbackfn(value, value2, this), thisArg) } /** * @returns a boolean indicating whether an element with the specified value exists in the Set or not. */ public has(value: Static): boolean { return this.values.has(this.#encodeKey(value)) } /** * @returns the number of (unique) elements in Set. */ public get size(): number { return this.values.size } // --------------------------------------------------- // Encoder // --------------------------------------------------- #encodeKey(value: Static) { return Value.Hash(value) } // --------------------------------------------------- // Assertions // --------------------------------------------------- /** Formats errors */ #formatError(errors: ValueError[]) { return errors .map((error) => `${error.message} ${error.path}`) .join('. ') .trim() } /** Asserts the key matches the value schema */ #assertValue(value: unknown): asserts value is Static { if (this.#valuecheck.Check(value)) return throw new TypeSetError(this.#formatError([...this.#valuecheck.Errors(value)])) } }