Options
All
  • Public
  • Public/Protected
  • All
Menu

Class to transform and manipulate tree like structures

Hierarchy

  • TreeUtils

Index

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

  • addNode(tree: any[], parentId: any, childData: any): void
  • 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

  • deleteNode(tree: any[], id: any): any
  • 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

  • editNode(tree: any[], id: any, data: any): void
  • 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

  • findAllChildrenNodes(tree: any[], id: any): any[]
  • Method to find all children nodes of given node in tree structure

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any[]

    all found children nodes

  • findAllParentNodes(tree: any[], id: any): any[]
  • Method to find all parents of given node in tree structure

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any[]

    all found parent nodes

  • findAllTreeNodes(tree: any[], fn: ((item: any) => boolean)): any
  • Method to find all nodes in tree structure by given callback function

    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

    all found nodes

  • findNodeParent(tree: any[], id: any, parent?: any): any
  • Method to find parent of given node in tree structure

    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

    found parent node

  • findTreeNode(tree: any[], fn: ((item: any) => boolean)): any
  • Method to find node in tree structure by given callback function

    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

    found node

  • findTreeNodeById(tree: any[], id: any): any
  • Method to find node in tree structure by given id

    Parameters

    • tree: any[]

      tree structure to search in

    • id: any

      identifier of node

    Returns any

    found node

  • getChildrenNodes(node: any): any[]
  • Helper method to recursively get all children nodes of given node in tree structure

    Parameters

    • node: any

      we want to get all of its children

    Returns any[]

    all found children nodes

  • list2Tree(list: any[], parentId?: any): 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[]

  • tree2List(tree: any[], parentId?: any): 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[]

  • deepCopy(obj: any): any
  • Helper method to deep clone object

    Parameters

    • obj: any

      object to be cloned

    Returns any

    deep cloned object

Generated using TypeDoc