@adviser/cement - v0.0.0
    Preparing search index...

    Function toSorted

    • Recursively sorts object keys and normalizes values for deterministic serialization.

      Deep sorts all object keys alphabetically, converts Dates to ISO strings, symbols to strings, and recursively processes nested structures. Handles circular references by returning undefined for cycles. Optional callback tracks all values encountered during traversal.

      Type Parameters

      • T

        The input value type

      Parameters

      • arrayOrObject: T

        Value to sort (object, array, or primitive)

      • OptionaltouchFn: TouchFn

        Optional callback invoked for each value with its type

      Returns T

      Sorted/normalized copy of the input

      const obj = {
      z: { nested: true, another: false },
      a: [3, 1, 2],
      date: new Date('2024-01-01')
      };

      const sorted = toSorted(obj);
      // {
      // a: [3, 1, 2],
      // date: '2024-01-01T00:00:00.000Z',
      // z: { another: false, nested: true }
      // }

      // Track value types
      const types: string[] = [];
      toSorted(obj, (value, type) => types.push(type));
      // types: ['Key', 'Array', 'Number', ..., 'Date', ...]