npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

hierarchy-model

v2.0.2

Published

A data model for a hierarchy of nodes, used by roles-hierarchy and permissions-hierarchy

Readme

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

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