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

baobab-tree-logic

v0.1.2

Published

Tree Logic

Downloads

5

Readme

baobab-tree-logic

Tree Logic System.

Many tree management libraries are available for JavaScript. But these libraries are often complicated to customize. Here, I wanted to create a project where the tree logic would have come out of any interface context.

NPM Version NPM Downloads Build Status codecov

Installation

$ yarn add baobab-tree-logic

Getting start

To start, your structure will have to respect a defined type called TreeNode.

interface TreeNode {
  data: any; // Your business data
  children?: Array<TreeNode>;
  expanded?: boolean;
}

Here is an example of a tree we use for testing:

const nodes = [
  {
    data: "level 0"
  },
  {
    data: "level 1",
    children: [{ data: "child of level 1" }],
    expanded: false
  },
  {
    data: "level 2",
    children: [
      {
        data: "child of level 2",
        children: [{ data: "child of child of level 2" }]
      }
    ]
  }
];

This tree will need to be flattened so that you can use it in your applications. To do this, you can use the flattenNodes function.

import { flattenNodes } from "baobab-tree-logic";

const flattenedNodes = flattenNodes(nodes);

The flattenNodes function will return you an array of elements of type TreeNodeExtra.

interface TreeNodeExtra {
  data: any;
  children?: Array<string>;
  hasChildren: boolean;
  lastChild: boolean;
  expanded: boolean;
  visible: boolean;
  path: string;
  parent?: string;
  depth: number;
}

Here is what the function will have returned to you:

[
  {
    "path": "0",
    "data": "level 0",
    "children": [],
    "expanded": true,
    "visible": true,
    "hasChildren": false,
    "lastChild": false,
    "depth": 0
  },
  {
    "path": "1",
    "data": "level 1",
    "children": ["1.0"],
    "expanded": false,
    "visible": true,
    "hasChildren": true,
    "lastChild": false,
    "depth": 0
  },
  {
    "path": "1.0",
    "data": "child of level 1",
    "children": [],
    "expanded": true,
    "visible": false,
    "parent": "1",
    "hasChildren": false,
    "lastChild": true,
    "depth": 1
  },
  {
    "path": "2",
    "data": "level 2",
    "children": ["2.0"],
    "expanded": true,
    "visible": true,
    "hasChildren": true,
    "lastChild": true,
    "depth": 0
  },
  {
    "path": "2.0",
    "data": "child of level 2",
    "children": ["2.0.0"],
    "expanded": true,
    "visible": true,
    "parent": "2",
    "hasChildren": true,
    "lastChild": true,
    "depth": 1
  },
  {
    "path": "2.0.0",
    "data": "child of child of level 2",
    "children": [],
    "expanded": true,
    "visible": true,
    "parent": "2.0",
    "hasChildren": false,
    "lastChild": true,
    "depth": 2
  }
]

API

The following can be imported from baobab-tree-logic.

flattenNodes(nodes: Array<TreeNode>, parent: string = undefined, depth: number = 0, parentExpanded: boolean = true): Array<TreeNodeExtra>

Function that allows you to flatten the tree. It adds attributes to each node of the tree that will allow you to perform actions later.

unflattenNodes(nodes: Array<TreeNodeExtra>): Array<TreeNode>

Function that allows you to funlatten the tree. It deletes the attributes added by the method flattenNodes.

expandNode(node: TreeNodeExtra, nodes: Array<TreeNodeExtra>): Array<TreeNodeExtra>

Function that allows to modify the extended state of a node of the tree.

changeNodePath(node: TreeNodeExtra, nodes: Array<TreeNodeExtra>, newPath: string): Array<TreeNodeExtra>

Function that allows you to change the path of a node in the tree.