Traqula docs
    Preparing search index...

    Class TransformerObject

    Base transformer class for recursively visiting and transforming object trees. Operates on plain JavaScript objects without requiring specific type structure.

    Uses an iterative (stack-based) algorithm instead of recursion to handle deep trees safely. transformObject and visitObject traverse depth-first, processing deeper objects before their parents (post-order), so a callback sees the already transformed descendants of the object it maps. transformObjectPreOrder traverses the same tree in the opposite order - transforming an object before its descendants (pre-order) - so a callback decides what the descendants we iterate into are.

    For type-aware traversal based on type and subType fields, see TransformerTyped and TransformerSubTyped.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    defaultContext: TransformContext = {}
    maxNodeRewrites: number = 1_000

    The number of times transformObjectPreOrder may hand the same position in the tree back to the traversal - by remapping it, or by wrapping it in an array - before we assume the rules do not converge.

    maxStackSize: number = 1_000_000

    Methods

    • Recursively transforms all objects that are not arrays. Mapper is called on deeper objects first.

      Parameters

      • startObject: object

        object to start iterating from

      • postMapper: (copy: object, orig: object) => unknown

        postMapper to transform the various objects - argument is a copy of the original

      • preVisitor: (orig: object) => TransformContext = ...

        callback that is evaluated before iterating deeper. If continues is false, we do not iterate deeper, current object is still mapped. - default: true If shortcut is true, we do not iterate deeper, nor do we branch out, this postMapper will be the last one called.

        • Default false

      Returns unknown

    • Recursively transforms all objects that are not arrays, calling the preMapper on an object before iterating into its descendants, and iterating into the result of that preMapper. It should be noted that the preMapper is called using a shallow copy of the object. Meaning manipulation of nested objects changes the original!

      Parameters

      • startObject: object

        object to start iterating from

      • preMapper: (copy: object, orig: object) => PreOrderMappingReturn

        mapper to transform the various objects - first argument is a copy of the original if default setup says to copy. It returns a PreOrderMappingReturn: the value taking the place of the object - the value we iterate into - together with the TransformContext steering that iteration. Since the mapper is the one deciding what the descendants of that value are, it hands us that context itself, there is no separate preVisitor. Whether we copy is therefore not up to the mapper either, the default context of this transformer decides that. The returned value is only handed back to the traversal - and thus mapped again - when the mapper asked for TransformContext.reTransform, in any other case we iterate straight into its descendants. An array is the exception: it is never mapped as a whole, its elements are handed back and mapped in turn. Either way the rules have to settle a position within maxNodeRewrites hand-backs of that position, or we assume they do not converge and throw. A VisitContext.shortcut simply ends the traversal - since an object is already mapped when we iterate into it, there is nothing left to unwind - leaving the objects still on the stack in the place they have in the (shallow) copy of their parent.

      Returns unknown

    • Visitor that visits all objects. Visits deeper objects first.

      Parameters

      • startObject: object
      • visitor: (orig: object) => void
      • preVisitor: (orig: object) => VisitContext = ...

      Returns void