UNPKG

@decaf-ts/decorator-validation

Version:
83 lines (82 loc) 2.84 kB
/** * @summary Mimics Java's String's Hash implementation * * @param {string | number | symbol | Date} obj * @return {number} hash value of obj * * @function hashCode * @memberOf module:decorator-validation * @category Model */ export declare function hashCode(obj: string | number | symbol | Date): string; /** * @summary Defines teh type for a Hashing function * @memberOf module:decorator-validation * @category Model */ export type HashingFunction = (value: any, ...args: any[]) => string; /** * @summary Hashes an object by combining the hash of all its properties * * @param {Record<string, any>} obj * @return {string} the resulting hash * * @function hashObj * @memberOf module:decorator-validation * @category Model */ export declare function hashObj(obj: Record<string, any> | any[]): string; export declare const DefaultHashingMethod = "default"; /** * @description Manages hashing methods and provides a unified hashing interface * @summary A utility class that provides a registry for different hashing functions and methods to hash objects. * The class maintains a cache of registered hashing functions and allows setting a default hashing method. * It prevents direct instantiation and provides static methods for registration and hashing. * * @class Hashing * @category Model * * @example * ```typescript * // Register a custom hashing function * Hashing.register('md5', (obj) => createMD5Hash(obj), true); * * // Hash an object using default method * const hash1 = Hashing.hash(myObject); * * // Hash using specific method * const hash2 = Hashing.hash(myObject, 'md5'); * ``` */ export declare class Hashing { /** * @description Current default hashing method identifier * @private */ private static current; /** * @description Cache of registered hashing functions * @private */ private static cache; private constructor(); /** * @description Retrieves a registered hashing function * @summary Fetches a hashing function from the cache by its key. Throws an error if the method is not registered. * * @param {string} key - The identifier of the hashing function to retrieve * @return {HashingFunction} The requested hashing function * @private */ private static get; /** * @description Registers a new hashing function * @summary Adds a new hashing function to the registry. Optionally sets it as the default method. * Throws an error if a method with the same key is already registered. * * @param {string} key - The identifier for the hashing function */ static register(key: string, func: HashingFunction, setDefault?: boolean): void; static hash(obj: any, method?: string, ...args: any[]): any; static setDefault(method: string): void; }