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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ima-worldhealth/tree

v2.6.0

Published

Build a tree from an adjacency list and operate on it

Downloads

157

Readme

Tree

Build Status Coverage Status

This module provides a simple API to create trees from adjacency lists. Specifically, given an array of JSON objects with ids and pointers to their parent ids, we can construct a tree structure and furnish operations on that structure.

Example

/**
 *  This tree looks like this:
 *            ROOT
 *         /    |    \
 *       id:1  id:4  id:6
 *       /           /   \
 *    id:2          id:7   id:3
 *     /
 *   id:5
 */
const nodes = [{
  id : 1,
  parent : 0,
}, {
  id : 2,
  parent : 1,
}, {
  id : 3,
  parent : 6,
  valueA : 10,
  valueB : 2,
}, {
  id : 4,
  parent : 0,
  valueA : 30,
  valueB : 4,
}, {
  id : 5,
  parent : 2,
  valueA : 9,
  valueB : 7,
}, {
  id : 6,
  parent : 0,
}, {
  id : 7,
  parent : 6,
  valueA : 10,
  valueB : 19,
}];

const tree = new Tree(nodes);

tree.walk((node) => console.log('node.id:', node.id));
// => 1, 2, 5, 4, 6, 7, 3

API

The API is simple but powerful. Most operations on trees can be defined as recursive functions, where a property of a node is either determined by a parent property or an aggregation of the child properties. For example, the depth of node N is simply the depth of N's parent plus 1. Similarly, to compute a sum on the parent nodes, simply update the parent node's value for every child in the tree.

The following API functions are supported:

walk(fn, callFnBeforeRecurse = true)

Walks the tree in order and applies fn() either before or after the recursive (descent) step. The fn function can take in two properties

find(id)

Finds a node in the tree by its id.

sort(fn)

Sorts the tree in place using the comparison function fn. The fn function is internally passed to Array.sort().

Common Functions

To keep operations simple, the library expects you to use walk() for the majority of your operations. However, common walk() functions are exposed through the Tree.common object. These are documented below:

computeNodeDepth

Sets the depth all nodes as a function of their parents depths property. The root node is depth = 0 and subsequent levels are childNode.depth = parentNode.depth + 1.

Usage:

const tree = new Tree(nodes);
tree.walk(Tree.common.computeNodeDepth);
// each node now has node.depth set on it!
const node = tree.find(12);
console.log('depth is:', node.depth);

sumOnProperty(property, defaultValue = 0)

Aggregates the value of parent nodes as a function of their children. For example, if a parent has two children with values 3 and 5, the parent's value will be 8.

Usage:

// an adjency tree of one parent, two children
const nodes = [
  { id: 1, value: null },
  { id: 2, value: 3, parent : 1 },
  { id: 3, value: 10, parent : 1 },
];

const tree = new Tree(nodes);
tree.walk(Tree.common.sumOnProperty('value'));

const node2 = tree.find(2);
console.log(node.value); //  => 3
const node1 = tree.find(1);
console.log(node.value); // => 13

License

MIT