mobx-bonsai
    Preparing search index...

    Function substituteNodeKeys

    • Deeply substitutes node keys in a data structure with new keys generated by a provided function.

      This function recursively traverses the input data structure and replaces all node keys with new ones according to the provided key generator function. The function creates a deep clone of the original data structure, ensuring no references to the original remain.

      Node keys are unique identifiers that are used internally to track and identify nodes in the data tree. Substituting these keys is useful when cloning nodes to ensure each node has a unique identity.

      The function handles:

      • Primitives: returned as-is
      • Arrays: recursively processes each element
      • Plain objects: identifies node keys using getNodeTypeAndKey and substitutes them

      Type Parameters

      • T

        The type of the input value

      Parameters

      • value: T

        The value to process (primitive, array, or plain object)

      • newNodeKeyGenerator: (oldKey: NodeKeyValue) => NodeKeyValue = ...

        Function to generate new node keys from old ones. Defaults to the global key generator from configuration.

      Returns T

      A deep clone of the original value with all node keys substituted

      Will throw an error if encountering a value type that isn't a primitive, array, or plain object