Class TreeUtils

Class to transform and manipulate tree like structures

Hierarchy

  • TreeUtils

Constructors

  • Constructor of class

    Parameters

    • Optional config: IConfig

      to configure class, if configuration option is omitted, default one is used

    Returns TreeUtils

Properties

childrenProp: string

Name of property where child nodes are stored (default value is children)

idProp: string

Name of unique identifier property in nodes (default value is id)

parentIdProp: string

Name of parent identifier property in nodes (default value is parentId)

Methods

  • Method to add new node to tree (mutable operation!)

    Parameters

    • tree: any[]

      tree structure for node adding

    • parentId: any

      identifier of parent node, null if new node should be on root level

    • childData: any

      data of new node

    Returns void

  • Method to delete node in tree by given id (mutable operation!)

    Parameters

    • tree: any[]

      tree structure for node deleting

    • id: any

      identifier of node to delete

    Returns any

  • Method to update node by id with given data in tree (mutable operation!)

    Parameters

    • tree: any[]

      tree structure for node editing

    • id: any

      identifier of node to be updated

    • data: any

      new data of node (you should also pass children if you want to keep it)

    Returns void

  • Method to find all children nodes of given node in tree structure

    Returns

    all found children nodes

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any[]

  • Method to find all parents of given node in tree structure

    Returns

    all found parent nodes

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any[]

  • Method to find all nodes in tree structure by given callback function

    Returns

    all found nodes

    Example

    utils.findAllTreeNodes(tree, item => item.id === myId);
    

    Parameters

    • tree: any[]

      tree structure to search in

    • fn: ((item: any) => boolean)

      callback function to find all nodes

        • (item: any): boolean
        • Parameters

          • item: any

          Returns boolean

    Returns any

  • Method to find parent of given node in tree structure

    Returns

    found parent node

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    • parent: any = null

      parent node, if we found something (for recursion only)

    Returns any

  • Method to find node in tree structure by given callback function

    Returns

    found node

    Example

    utils.findTreeNode(tree, item => item.id === myId);
    

    Parameters

    • tree: any[]

      tree structure to search in

    • fn: ((item: any) => boolean)

      callback function to find node

        • (item: any): boolean
        • Parameters

          • item: any

          Returns boolean

    Returns any

  • Method to find node in tree structure by given id

    Returns

    found node

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any

  • Private

    Helper method to recursively get all children nodes of given node in tree structure

    Returns

    all found children nodes

    Parameters

    • node: any

      we want to get all of its children

    Returns any[]

  • Convert list to tree like structure

    Parameters

    • list: any[]

      list of objects, objects need to have id (as you configured, or 'id' by default) and parentId property (as you configured, or 'parentId' by default)

    • parentId: any = null

      id of parent node

    Returns any[]

  • Convert tree like structure to list

    Parameters

    • tree: any[]

      tree of objects, objects need to have children (as you configured, or 'children' by default) and parentId property (as you configured, or 'parentId' by default)

    • parentId: any = null

    Returns any[]

  • Private

    Helper method to deep clone object

    Returns

    deep cloned object

    Parameters

    • obj: any

      object to be cloned

    Returns any

Generated using TypeDoc