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

jsaturday_tree

v1.1.2

Published

Generic in-memory Tree data structure

Readme

jsaturday_tree

Description

Simple in-memory tree structure to represent hierarchical data.

The tree is defined by:

  • n nodes; each node is an Object contains arbitrary data, and two special fields: _id (unique node id) and _parent (id of parent node). If no parent node is given, it is automatically assigned to orphanParent (... I know...)
  • One root node, with _id = 'ROOT'
  • One orphanParent node, (default _id = 'ORPHANS') connected under 'ROOT' node.
  • One parent for each node, or zero parents (in this case node will be connected to orphan node).

New features

  • v 1.1.2 bug fix: in createNode(node), node is passed by copy.
  • v 1.1 added optional callbacks for addNode(), removeNode(), and updateNode(). Updated unit tests.

Installation

$ npm i jsaturday_tree

Test

$ npm test

Api

// Constructor
var Tree = require('jsaturday_tree');
var roles = new Tree(options);

// Add node
roles.addNode(nodeObject, <function(err, node){...}>);

// Remove node
roles.removeNode(nodeId, <newParentId>, <function(err, success){...}>);

// Update node
roles.updateNode(nodeObject, <function(err, node){...}>);

// Initialize / reinitialize
roles.initialize();

// Get copy of all nodes / a selected node after computing children
var nodesArray = roles.getAllCopy();
var nodeObject = roles.getNodeCopy(nodeId);

Please check the follow examples as API documentation.

Examples

// Require Tree object
var Tree = require('jsaturday_tree');

//-----------------------------------------------------------------
// Options
//-----------------------------------------------------------------
var options = {
    // How long (sec) data structure will keep as computed in the memory
    TTL: 60, // default = 0 => no cache
    
    // Parent nodes for nodes without parent, by default it is
    // 'ORPHANS', connected to 'ROOT'
    orphanParentId: 'MyORPHAN' // default = ORPHANS
};

//-----------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------
var roles = new Tree(options);

//-----------------------------------------------------------------
// Add nodes
//-----------------------------------------------------------------
// (for each added node cache is automatically flushed)
// Note: 'ROOT' node and orphan node will be automatically created before
// initialization, if they was not provided by user.
roles.addNode({
    _id: 'NODE1',
    _parent: 'ROOT',
    attribute1: ...
});
roles.addNode({
    _id: 'NODE2',
    _parent: 'NODE1',
    attribute1: ... ,
    attribute2: ... ,
});

// This is an ORPHAN node
roles.addNode({
    _id: 'NODE3'
});

roles.addNode({
    _id: 'NODE4',
    _parent: 'ROOT',
});

roles.addNode({
    _id: 'NODE5',
    _parent: 'NODE4',
    value: 1
});

//-----------------------------------------------------------------
// Initialize data structure
//-----------------------------------------------------------------
// It resets cache and computes children. This method is
// called automatically if new node was added / removed, so probably you
// will never use.
roles.initialize();

//-----------------------------------------------------------------
// Get data structure.
//-----------------------------------------------------------------
// Returns a COPY node objects, with _children attribute computed
var all = roles.getAllCopy();

//-----------------------------------------------------------------
// Up and Down chains
//-----------------------------------------------------------------
// Return array of node ids from required nodeId to ROOT
var node1UpIds = roles.getUp('NODE1');      // => ['NODE1', 'ROOT']
var node3UpIds = roles.getUp('NODE3');      // => ['NODE3', 'MyORPHAN']

// Return array of node ids from required nodeId to leaves
var node1DownIds = roles.getDown('NODE1');  // => ['NODE1', 'NODE2']

//-----------------------------------------------------------------
// Removing a node
//-----------------------------------------------------------------
// Removing a node and set a new parent for all the children
roles.removeNode('NODE1', 'ROOT');
var node2UpIdNow = roles.getUp('NODE2');      // => ['NODE2', 'ROOT']

// Removing a node and move all children to orphan node
roles.removeNode('NODE4');
var node5UpIdNow = roles.getUp('NODE5');      // => ['NODE5', 'MyORPHAN']

//-----------------------------------------------------------------
// Get node content (it will pass A COPY!)
//-----------------------------------------------------------------
var node5CloneA = roles.getNodeCopy(NODE5);
// node5CloneA = {_id: "NODE5", _parent: "MyORPHAN", value: 1}
node5CloneA.value = 2;
// node5Clone = {_id: "NODE5", _pare nt: "MyORPHAN", value: 2}
var node5CloneB = roles.getNodeCopy(NODE5);
// node5CloneB = {_id: "NODE5", _parent: "MyORPHAN", value: 1}

//-----------------------------------------------------------------
// Update node content (it will save a COPY of passed object!)
//-----------------------------------------------------------------
roles.updateNode(node5CloneA);
// Internal NODE5 = {_id: "NODE5", _pare nt: "MyORPHAN", value: 2}
node5CloneA.value = 3;
// Internal NODE5 = {_id: "NODE5", _pare nt: "MyORPHAN", value: 2}