hierarchy-model

2.0.2 • Public • Published

Hierarchy Data Model

Build Status

This is a data model for a hierarchy or tree, used by roles-hierarchy and permissions-hierarchy.

Given a definition of a hierarchy :

  let hierarchyObj = {
      "name": "Primate",
      "children": [
        {
          "name": "Hominidae",
          "children": [
            {
              "name": "Homo",
              "children": [
                {
                  "name": "Sapiens",
                  "children": [
                    {
                      "name": "Human"
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "name": "Pongidae",
          "children": [
            {
              "name": "Pan",
              "children": [
                {
                  "name": "Troglodytes",
                  "children": [
                    {
                      "name": "Chimpanzee"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    };

    let hierarchy = new Hierarchy(
      {
        "hierarchy": hierarchyObj),
        "loggingConfig": { level: "debug" },
        treeModelConfig: { "childrenPropertyName": "children" },
      }      
    );

You can find information about the node's descendants.

let subordinatesArr = hierarchy.getAllDescendantNodesAsArray('Primate'); // ["Hominidae", "Pongidae", "Homo", "Pan", "Sapiens", "Troglodytes", "Human", "Chimpanzee"]

You can retrieve a single node by name

let primate = hierarchy.findNodeInHierarchy("Primate"); // {"name":"Primate","children":[{.....}]}

And check if one node is a descendant of another

let subordinate = hierarchy.findDescendantNodeByName('Primate', 'Homo'); // {"name":"Homo","children":[....]}

Tests

Look in the test/test.js file, it gives you a pretty good idea of how to use this library.

To run the tests, simply :

npm test

API

Docs generated with jsdoc2md.

Functions

reparse(hierarchy)

re-create the hierarchy with a new object structure.

findNodeInHierarchy(nodeName)object

Find the model for a node in the hierarchy, by name

findNodeObj(nodeName, [startNode])

Find the node object for a node in the hierarchy, by name

findDescendantNodeByName(nodeName, descendantNodeName, [startNode])object

Return the descendent node of the given nodeName if found.

getAllDescendantNodesAsArray(nodeName, [startNode])Array

Get the names of subordinate nodes as an array

getTopiaryAsString(hierarchy)string

get a string suitable for printing, via the topiary library.

walkNodes(callback)

Process each node in the tree via a callback, halting when your callback returns false.

addNodeAsChildOfNode(parentNode, childNode)Object

Add a child to a parent.

getPathOfNode(node)Object

Get the array of Nodes representing the path from the root to this Node (inclusive).

getNamesOfNodePath(node)Array.<String>

Get the array of Node names representing the path from the root to this Node (inclusive).

deleteNodeFromHierarchy(node)Object

Drop the subtree starting at this node. Returns the node itself, which is now a root node.

getTreeModel()Object

get the underlying TreeModel instance

getNewNode(paramsObj)

Create Node (which is itself just a TreeModel)

reparse(hierarchy)

re-create the hierarchy with a new object structure.

Kind: global function

Param Type
hierarchy Object

findNodeInHierarchy(nodeName) ⇒ object

Find the model for a node in the hierarchy, by name

Kind: global function
Returns: object - - the model of the node in the tree that matches

Param Type Description
nodeName string the name of the node to find (i.e. 'name' property value)

findNodeObj(nodeName, [startNode])

Find the node object for a node in the hierarchy, by name

Kind: global function

Param Type Description
nodeName string the name of the node to find (i.e. 'name' property value)
[startNode] object the node in the hierarchy to start from

findDescendantNodeByName(nodeName, descendantNodeName, [startNode]) ⇒ object

Return the descendent node of the given nodeName if found.

Kind: global function
Returns: object - - the node of the descendant, or undefined or false if not found.

Param Type Description
nodeName string the name of the node underneath which we should search
descendantNodeName string the name of the descendant node to find
[startNode] object the node in the hierarchy to start from

getAllDescendantNodesAsArray(nodeName, [startNode]) ⇒ Array

Get the names of subordinate nodes as an array

Kind: global function
Returns: Array - - the subordinate node names if any, otherwise undefined.

Param Type Description
nodeName string the name of the senior node i.e. 'name' property value
[startNode] object the node in the hierarchy to start from

getTopiaryAsString(hierarchy) ⇒ string

get a string suitable for printing, via the topiary library.

Kind: global function
Returns: string - a string representation of the hierarchy

Param Type Description
hierarchy object a Hierarchy instance

walkNodes(callback)

Process each node in the tree via a callback, halting when your callback returns false.

Kind: global function

Param Type Description
callback function a function that takes a single parameter, 'node', which is the value of the node currently being processed. Return false from the callback to halt the traversal.

addNodeAsChildOfNode(parentNode, childNode) ⇒ Object

Add a child to a parent.

Kind: global function
Returns: Object - the child node.

Param Type Description
parentNode Object the node in the hierarchy to which the child should be added
childNode Object a node or tree

getPathOfNode(node) ⇒ Object

Get the array of Nodes representing the path from the root to this Node (inclusive).

Kind: global function
Returns: Object - the array of Nodes representing the path from the root to this Node (inclusive).

Param Type
node Object

getNamesOfNodePath(node) ⇒ Array.<String>

Get the array of Node names representing the path from the root to this Node (inclusive).

Kind: global function
Returns: Array.<String> - the array of Strings representing the path from the root to this Node (inclusive).

Param Type
node Object

deleteNodeFromHierarchy(node) ⇒ Object

Drop the subtree starting at this node. Returns the node itself, which is now a root node.

Kind: global function
Returns: Object - node the node that just got dropped.

Param Type Description
node Object the node in the hierarchy to drop.

getTreeModel() ⇒ Object

get the underlying TreeModel instance

Kind: global function
Returns: Object - the underlying TreeModel instance.

getNewNode(paramsObj)

Create Node (which is itself just a TreeModel)

Kind: global function

Param Type Description
paramsObj Object an object which has 'name' and 'children' properties

Readme

Keywords

none

Package Sidebar

Install

npm i hierarchy-model

Weekly Downloads

1

Version

2.0.2

License

ISC

Last publish

Collaborators

  • cunneen