UNPKG

@rustable/utils

Version:

Essential utilities for object cloning, string manipulation, and value comparison in TypeScript, inspired by Rust's standard library.

96 lines (95 loc) 3.4 kB
/** * A robust object stringification system that handles circular references and complex object structures. * This module provides two main functions: * - stringifyObject: For complex object serialization with circular reference handling * - stringify: For general-purpose value to string conversion * * Key Features: * - Deterministic output with sorted object keys * - Circular reference detection and handling * - Support for special types (Map, Date, Symbol, etc.) * - Memory efficient with WeakMap for reference tracking */ /** * Converts any JavaScript value to its string representation. * Provides a consistent way to convert values to strings across different types. * * Conversion Rules: * | Type | Example Input | Output | * |---------------|-------------------|---------------------| * | null | null | '' | * | undefined | undefined | '' | * | string | 'hello' | 'hello' | * | number | 42 | '42' | * | boolean | true | 'true' | * | object | {x: 1} | '{x:1}' | * | array | [1, 2] | '[1,2]' | * | function | () => {} | 'function...' | * | symbol | Symbol('key') | 'Symbol(key)' | * | bigint | 42n | '42' | * * @example * ```typescript * // Primitive values * stringify('hello'); // 'hello' * stringify(42); // '42' * stringify(true); // 'true' * stringify(null); // '' * * // Objects and arrays * stringify({ x: 1 }); // '{x:1}' * stringify([1, 2, 3]); // '[1,2,3]' * * // Complex objects * const user = { * name: 'John', * info: { age: 30 } * }; * stringify(user); // '{info:{age:30},name:"John"}' * * // Special types * stringify(Symbol('key')); // 'Symbol(key)' * stringify(42n); // '42' * stringify(() => {}); // 'function...' * ``` * * @param obj Any JavaScript value to convert to string * @returns String representation of the value */ export declare function stringify(obj: any): string; /** * Generates a hash code for any JavaScript value. * Handles all primitive types and objects by converting them to strings first. * Uses the djb2 algorithm for string hashing. * * Hash values for different types: * - null/undefined: -1 * - string: djb2 hash of the string * - number: the number itself * - boolean: 1 for true, 0 for false * - object: djb2 hash of its stringified representation * - function: djb2 hash of its string representation * - symbol: djb2 hash of its string representation * - bigint: djb2 hash of its string representation * * @param obj Any JavaScript value to hash * @returns A number representing the hash code * * @example * ```typescript * // Primitive values * hash(null); // -1 * hash(true); // 1 * hash(42); // 42 * hash("hello"); // [32-bit hash value] * * // Objects * hash({x: 1}); // [32-bit hash value] * hash([1, 2, 3]); // [32-bit hash value] * * // Special values * hash(Symbol("key")); // [32-bit hash value] * hash(() => {}); // [32-bit hash value] * ``` */ export declare function hash(obj: any): number;