Represents the context of a node in a tree structure. Provides methods to interact with the node and its position in the tree.

Implements

Constructors

  • Parameters

    • key: string | number
    • value: any
    • context: Record<string | number, any>
    • depth: number
    • breakEmitter: (() => void)
        • (): void
        • Returns void

    • path: (string | number)[]
    • rootContext: Record<string | number, any>
    • changeEmitter: (() => void)
        • (): void
        • Returns void

    Returns TreeContext

Properties

_context: Record<string | number, any>
_key: string | number
_rootContext: Record<string | number, any>
_value: any
breakEmitter: (() => void)

Type declaration

    • (): void
    • Returns void

changeEmitter: (() => void)

Type declaration

    • (): void
    • Returns void

depth: number

The depth of the current node in the tree, starting from 0 at the root context.

path: readonly (string | number)[]

The path from the root node to the current node.

This does not represent the path of the traversal, but rather the path to access the node from the root node.

Example

const tree = {
value: 1,
depth: 0,
left: {
value: 2,
depth: 1,
left: { depth: 2, value: 4, left: null, right: null },
right: { depth: 2, value: 5, left: null, right: null },
},
right: {
value: 3,
depth: 1,
left: { depth: 2, value: 6, left: null, right: null },
right: {
depth: 2,
value: 7, // <-- target
left: null,
right: null,
UNIQUE_KEY: "UNIQUE_VALUE",
},
},
} as const;

let path: Readonly<(string | number)[]> | undefined
treeBFS(tree, (ctx) => {
if (ctx.isRecord() && ctx.key === "value" && ctx.value === 7) {
path = ctx.path;
}
});

expect(path).toStrictEqual(["right", "right", "value"]);

Accessors

  • get ancestors(): (any[] | Record<string, any>)[]
  • Returns an array of ancestors of the current context. An ancestor is a parent or a grandparent of the current context. Each ancestor is represented as a record or an array.

    Returns (any[] | Record<string, any>)[]

    The array of ancestors.

  • get context(): any[] | Record<string, any>
  • Gets the current context.

    Mutating the context's structure may cause unexpected behavior.

    Returns any[] | Record<string, any>

    The current context, either as a record or an array.

  • get parent(): any[] | Record<string, any>
  • Gets the parent context of the current node.

    Returns any[] | Record<string, any>

    The parent context.

  • get rootContext(): Readonly<Record<string | number, any>>
  • Gets the root context of the tree.

    Returns Readonly<Record<string | number, any>>

    The root context.

  • get value(): any
  • Gets the value of the current context node.

    May cause unexpected behavior in traversal if mutated.

    Returns any

    The value of the node.

  • set value(value): void
  • Sets the value of the current context node.

    If the original value or the new value is an object, neither will be traversed through after setting a value.

    Parameters

    • value: any

      The new value to be set.

    Returns void

Methods

Generated using TypeDoc