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

@golden-tiger/tree

v0.0.5

Published

@golden-tiger/tree

Downloads

10

Readme

#golden-tiger/tree

Using

import Tree from '@golden-tiger/tree'; // mjs
const Tree = require('@golden-tiger/tree'); // cjs

Tree.from

Construct trees from flatten data.

  1. @param1 nodes: Tree node array

  2. @param2 option: option.key: unique property's name for node. option.parentKey: unique property's name for parent node. When this value is null or undefined, it will be a root node. option.childrenKey: unique property's name for children node.

const a = [
  { id: 0, parentId: undefined },
  { id: 1, parentId: 0 },
  { id: 11, parentId: 1 },
  { id: 12, parentId: 1 },
  { id: 2, parentId: 0 },
  { id: 21, parentId: 2 },
  { id: 22, parentId: 2 },
];

// construct trees from flatten data
const trees = Tree.from(a, { key: 'id', parentKey: 'parentId' });
console.log(JSON.stringify(trees, null, 4));
// [
//     {
//         "id": 0,
//         "children": [
//             {
//                 "id": 1,
//                 "parentId": 0,
//                 "children": [
//                     {
//                         "id": 11,
//                         "parentId": 1
//                     },
//                     {
//                         "id": 12,
//                         "parentId": 1
//                     }
//                 ]
//             },
//             {
//                 "id": 2,
//                 "parentId": 0,
//                 "children": [
//                     {
//                         "id": 21,
//                         "parentId": 2
//                     },
//                     {
//                         "id": 22,
//                         "parentId": 2
//                     }
//                 ]
//             }
//         ]
//     }
// ]

Tree.traverse

Traverse a tree in pre-order or post-order.

  1. @param1 tree: tree root
  2. @param2 reducer: Handler function with parameters of current tree node, accumulator state and current tree's parent node. If handler function returns false, it will terminate the traverse flow.
  3. @param3 option option.childrenKey: specify the tree's children key (default value: 'children'). option.order: ['pre'|'post'|'pre-reverse'|'post-reverse'], traverse a tree in pre-order, post-order, pre-order-reverse, or post-order-reverse. option.state: the state when traverse.

pre

// use pre-order traverse
const preOrderState = {
  result: [],
};
trees.forEach(tree => Tree.traverse(tree, (node, state, parent) => {
  state.result.push(node.id);
  return state;
}, {
  order: 'pre', // pre-order
  state: preOrderState,
}));
console.log(preOrderState.result);
// [ 0, 1, 11, 12, 2, 21, 22 ]

pre-reverse

// use pre-order-reverse traverse
const preOrderReverseState = {
  result: [],
};
trees.forEach(tree => Tree.traverse(tree, (node, state, parent) => {
  state.result.push(node.id);
}, {
  order: 'pre-reverse',
  state: preOrderReverseState,
}));
console.log(JSON.stringify(preOrderReverseState.result));
// [ 0, 2, 22, 21, 1, 12, 11 ]

post

// use post-order traverse
const postOrderState = {
  result: [],
};
trees.forEach(tree => Tree.traverse(tree, (node, state, parent) => {
  state.result.push(node.id);
  return state;
}, {
  order: 'post', // post-order
  state: postOrderState,
}));
console.log(postOrderState.result);
// [ 11, 12, 1, 21, 22,  2, 0 ]

post-reverse

// use post-order-reverse traverse
const postOrderReverseState = {
  result: [],
};
trees.forEach(tree => Tree.traverse(tree, (node, state, parent) => {
  state.result.push(node.id);
}, {
  order: 'post-reverse',
  state: postOrderReverseState,
}));
console.log(JSON.stringify(postOrderReverseState.result));
// [ 22, 21, 2, 12, 11, 1, 0 ]

terminable

// use terminable traverse
const terminableState = Tree.traverse(trees[0], (node, state, parent) => {
  state.traveled.push(node.id);
  if (node.id === 12) {
    return false;
  }
}, {
  state: {
    traveled: [],
  },
});
console.log(terminableState);
// { traveled: [ 0, 1, 11, 12 ] }