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

@mangolibs/treejs

v1.0.2

Published

A simple javascript lib for handling tree.

Downloads

5

Readme

Englist | 中文

Install

npm install @mangolibs/treejs

Usage

// example data
const treeData = [
  {
    id: 1,
    name: "node1",
    children: [
      {
        id: 2,
        name: "node1-1",
        children: [
          { id: 3, name: "node1-1-1" },
          { id: 4, name: "node1-1-2" },
        ],
      },
    ],
  },
  {
    id: 5,
    name: "node2",
    children: [
      { id: 6, name: "node2-1" },
      { id: 7, name: "node2-2" },
    ],
  },
  { id: 8, name: "node3" },
];
import { createTreeInstance } from "@mangolibs/treejs";

// Create an instance
// A tree instance is returned, and the instance method can be called
const tree = createTreeInstance(treeData, {
  nodeKey: "id", // node key
  childNodeKey: "children", // child node key
});

API

  • find(): find the node and return the first matching node object

    const result = tree.find((node) => node.id === 4);
    // result: {id: 4, name: 'node1-1-2'}
  • findPath(): find the node path, and return the path of the first matching node, returned as a flattened array

    const result = tree.findPath((node) => node.id === 4);
    // result: [
    //    {id: 1, name: 'node1', children:[...]},
    //    {id: 2, name: 'node1-1', children:[...]},
    //    {id: 4, name: 'node1-1-2'}
    // ]
  • filter(): filter nodes, return the filtered array of nodes, and return as a flattened array

    const result = tree.filter((node) => node.id > 5);
    // result: [
    //    {id: 7, name: 'node2-2'},
    //    {id: 6, name: 'node2-1'},
    //    {id: 8, name: 'node3'}
    // ]
  • search(): search for a node, return a matching node, return it in the original tree structure, and keep its parent path

    const result = tree.search((node) => node.name === "node2-1");
    // result: [
    //    {
    //        "id": 5,
    //        "name": "node2",
    //        "children": [
    //            {  "id": 6, "name": "node2-1" }
    //        ]
    //    }
    // ]
  • flat(): flatten the tree, returning the flattened array of nodes

    const result = tree.flat();
    // result: [
    //    {id: 1, name: 'node1', children:[...]},
    //    {id: 2, name: 'node1-1', children:[...]},
    //    {...}
    //    {id: 8, name: 'node3'}
    // ]
  • parent(): finds the parent node of the target node and returns the parent node object (only the parent node of the first matching node is found, the first matching node may also be different depending on the algorithm)

    const result = tree.parent((node) => node.id === 4);
    // result: {id: 21, name: 'node1-1', children:[...]}
  • siblings(): find the sibling of the target node and return an array of sibling nodes (only the sibling of the first matching node is found, the first matching node may also be different depending on the algorithm)

    const result = tree.siblings((node) => node.id === 4);
    // result: [
    //    {id: 3, name: 'node1-1-1'},
    //    {id: 4, name: 'node1-1-2'}
    // ]
  • remove(): remove the target node and return the removed node (only the first matching node is removed, the first matching node may also be different depending on the algorithm)

    const result = tree.remove((node) => node.id === 4);
    // result: {id: 4, name: 'node1-1-2'}
  • modify(): modify the target node and return the modified node (only the first matching node is modified, the first matching node may also be different depending on the algorithm)

    const result = tree
      .modify({ name: "node1-1-2-modify" })
      .which((node) => node.id === 4);
    // result: {id: 4, name: 'node1-1-2-modify'}
  • insert(): inserta a node, and return the inserted node (only the first matching node is inserted, the first matching node may also be different depending on the algorithm)

    // insert the new node before the target node
    const result1 = tree
      .insert({ id: 100, name: "insert node before" })
      .before((m) => m.id === 4);
    // result1: { id: 100, name: 'insert node before' }
    
    // insert the new node after the target node
    const result2 = tree
      .insert({ id: 101, name: "insert node after" })
      .after((m) => m.id === 4);
    // result2: { id: 101, name: 'insert node after' }
    
    // the sibling node of the target node: [
    //  { "id": 3, "name": "node1-1-1" },
    //  { "id": 100, "name": "insert node before" },
    //  { "id": 4,  "name": "node1-1-2" },
    //  { "id": 101, "name": "insert node after" }
    // ]
  • appendChild(): append a child node and return the target node (only append the first matching node, the first matching node may also be different depending on the algorithm)

    const result = tree
      .appendChild({ id: 100, name: "append child" })
      .which((node) => node.id === 4);
    // result: {
    //  "id": 4,
    //  "name": "node1-1-2",
    //  "children": [{ "id": 100, "name": "append child" }]
    // }
  • keyToKey(): key conversion, replace the specified key with a new key, and return a new tree ...

    const result = tree.keyToKey({ id: "key", name: "value" });
    // result: [
    //    {
    //        key:1,  // new key
    //        value: "node1", // new key
    //        children:[{...}],
    //        __rawData:{ id, name, ...}  // the data of the original key
    //    }
    //    {...}
    // ]

Others

  • flat(options)
  • find(predicate, options)
  • findPath(predicate, options)
predicate: (node) => boolean;
// optional
options?: {
    use?: 'DFS'|'BFS'; // default:DFS(Stack), DFS:DFS(Recursive),BFS:BFS(Queue)
    onComplete?:(res) => void;
}