2.0.2 • Published 7 years ago

hierarchy-model v2.0.2

Weekly downloads
1
License
ISC
Repository
github
Last release
7 years ago

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.

Kind: global function

ParamType
hierarchyObject

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

ParamTypeDescription
nodeNamestringthe 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

ParamTypeDescription
nodeNamestringthe name of the node to find (i.e. 'name' property value)
startNodeobjectthe 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.

ParamTypeDescription
nodeNamestringthe name of the node underneath which we should search
descendantNodeNamestringthe name of the descendant node to find
startNodeobjectthe 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.

ParamTypeDescription
nodeNamestringthe name of the senior node i.e. 'name' property value
startNodeobjectthe 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

ParamTypeDescription
hierarchyobjecta Hierarchy instance

walkNodes(callback)

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

Kind: global function

ParamTypeDescription
callbackfunctiona 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.

ParamTypeDescription
parentNodeObjectthe node in the hierarchy to which the child should be added
childNodeObjecta 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).

ParamType
nodeObject

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).

ParamType
nodeObject

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.

ParamTypeDescription
nodeObjectthe 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

ParamTypeDescription
paramsObjObjectan object which has 'name' and 'children' properties
2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago