utilitish - v0.0.10
    Preparing search index...
    interface Object {
        deepClone<T>(): T;
        deepMerge<T>(source: any): any;
        deepEquals(other: unknown): boolean;
        stableStringify(): string;
        stableHash(): string;
    }
    Index

    Methods

    • Creates a deep clone of the object using the structured clone algorithm. This preserves object types, reference integrity, and works with cyclic references.

      Type Parameters

      • T

        The type of the object being cloned

      Returns T

      A deep copy of the original object with all nested objects and arrays cloned

      If the object contains uncloneable values (functions, symbols, etc.)

      const original = { a: { b: 1 }, c: [2, 3] };
      const cloned = original.deepClone();
      cloned.a.b = 999;
      console.log(original.a.b); // 1 (original unchanged)

      Uses the native structuredClone() API which provides:

      • Support for Date, Map, Set, TypedArray objects
      • Preservation of prototype chains for built-in types
      • Proper handling of circular references
    • Deeply merges another object into the current object, recursively combining nested objects. Arrays and primitives are replaced (not merged). Modifies the current object in place.

      Type Parameters

      • T

        The type of the object being merged into

      Parameters

      • source: any

        The source object to merge from (must be a non-null object)

      Returns any

      The merged object (same as this)

      If source is not a non-null object

      const target = { a: 1, b: { c: 2 } };
      const source = { b: { d: 3 }, e: 4 };
      target.deepMerge(source);
      // target: { a: 1, b: { c: 2, d: 3 }, e: 4 }
      • Primitive values in the source overwrite target values
      • Arrays in the source completely replace target arrays (not merged element-wise)
      • Only enumerable own properties are merged
      • The merge is performed recursively for nested objects
    • Checks for deep equality with another object, comparing all nested properties recursively. Uses strict equality for primitives and deep comparison for objects.

      Parameters

      • other: unknown

        The object to compare with

      Returns boolean

      True if both objects are deeply equal, false otherwise

      const obj1 = { a: { b: 1 }, c: [2, 3] };
      const obj2 = { a: { b: 1 }, c: [2, 3] };
      console.log(obj1.deepEquals(obj2)); // true

      const obj3 = { a: { b: 2 }, c: [2, 3] };
      console.log(obj1.deepEquals(obj3)); // false
      • Primitives are compared with strict equality (===)
      • Objects are compared property by property recursively
      • Array length and element order are considered
      • only enumerable own properties are compared
      • null and undefined are handled correctly
    • Converts the object to a stable string representation with sorted keys. The resulting string is deterministic: the same object will always produce the same string.

      Returns string

      A stable string representation of the object

      const obj = { b: 2, a: 1 };
      obj.stableStringify(); // '{"a":1,"b":2}'

      const obj2 = { a: 1, b: 2 };
      obj2.stableStringify(); // '{"a":1,"b":2}' (same result, different key order)
      • Object keys are sorted alphabetically to ensure stable output
      • Arrays maintain their element order
      • Null and primitives are handled using JSON.stringify
    • Generates a stable hash of the object using FNV-1a algorithm. The hash is deterministic: the same object will always produce the same hash.

      Returns string

      A hexadecimal string representing the hash of the object

      const obj = { b: 2, a: 1 };
      obj.stableHash(); // '7a8c9f2b'

      const obj2 = { a: 1, b: 2 };
      obj2.stableHash(); // '7a8c9f2b' (same hash, different key order)
      • Uses FNV-1a (Fowler–Noll–Vo) hashing algorithm
      • The object is first converted to a stable string using stableStringify()
      • The hash is returned as a hexadecimal string