Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-tree-utils

Package Overview
Dependencies
Maintainers
0
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-tree-utils

Simple Tree Utils is the library to convert and manipulate with tree-like structures.

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

npm version docs npm bundle size NPM CircleCI Coverage Status npm GitHub Repo stars

Simple Tree Utils

Simple Tree Utils is the library to convert and manipulate with tree-like structures. Library provides converter from an array of objects to trees like structure and vice versa, and also methods to manipulate that tree and/or search in that tree.

It is created because I needed to convert the array of objects into a tree to visualize in Angular tree component. Surely there are plenty of similar libraries, but I think all of them work with their own models/classes, I needed to use my own data without no extra instantiating, or adding extra methods/properties into the working model.

Content

🚀 Instalation

Install library using npm npm install simple-tree-utils --save and import main class into your code base import {TreeUtils} from 'simple-tree-utils'; .

📚 Documentation

For more details and complete documentation check: https://simple-tree-utils.netlify.app/

💻 Usage

First instantiate class with config, or without config.

const treeUtils = new TreeUtils(); // without config, default values are used (id as idProp, parentId as parentIdProp, children as childrenProp)
const treeUtils2 = new TreeUtils({
  idProp: 'id', // the key of a unique identifier for an object (source object)
  parentIdProp: 'parentId', // the key of a unique parent identifier (source object)
  childrenProp: 'children', // the key, where child nodes are stored (destination object tree)
});

After instantiation, you can use tree utils. You can convert the array of the following objects into a tree using list2Tree method as following

const items = [
  {id: 1, parentId: null, name: 'Node 1'},
  {id: 2, parentId: null, name: 'Node 2'},
  {id: 3, parentId: 1, name: 'Node 3'},
  {id: 4, parentId: 1, name: 'Node 4'},
  {id: 5, parentId: 2, name: 'Node 5'},
  {id: 6, parentId: 3, name: 'Node 6'},
];
const output = treeUtils.list2Tree(items);

then the output of lift2Tree will be

const tree = [
  {
    id: 1, parentId: null, name: 'Node 1', children: [
      {
        id: 3, parentId: 1, name: 'Node 3', children: [
          {id: 6, parentId: 3, name: 'Node 6', children: []},
        ]
      },
      {id: 4, parentId: 1, name: 'Node 4', children: []},
    ]
  },
  {
    id: 2, parentId: null, name: 'Node 2', children: [
      {id: 5, parentId: 2, name: 'Node 5', children: []},
    ]
  },
];

When you got a structure like the above, you can use all the following methods:

NameDesriptionSignature
addNodeMethod to add new node to tree (mutable operation!)addNode(tree: any[], parentId: any, childData: any): void
deleteNodeMethod to delete node in tree by given id (mutable operation!)deleteNode(tree: any[], id: any): any
editNodeMethod to update node by id with given data in tree (mutable operation!)editNode(tree: any[], id: any, data: any): void
findAllChildrenNodesMethod to find all children nodes of given node in tree structurefindAllChildrenNodes(tree: any[], id: any): any[]
findAllParentNodesMethod to find all parents of given node in tree structurefindAllParentNodes(tree: any[], id: any): any[]
findAllTreeNodesMethod to find all nodes in tree structure by given callback functionfindAllTreeNodes(tree: any[], fn: ((item: any) => boolean)): any
findNodeParentMethod to find parent of given node in tree structurefindNodeParent(tree: any[], id: any, parent?: any): any
findTreeNodeMethod to find node in tree structure by given callback functionfindTreeNode(tree: any[], fn: ((item: any) => boolean)): any
findTreeNodeByIdMethod to find node in tree structure by given idfindTreeNodeById(tree: any[], id: any): any

For example, we can find node by giving callback

const node = treeUtils.findTreeNode(tree, item => item.id === 2);

If you need a list again, you can convert the tree back to a list using treeUtils.tree2List method

treeUtils.tree2List(tree);

🌐 Usage in browser

You can also use this library in the browser without compiling using jsDelivr. Import script into HTML file, and you can access classes through the global treeUtils object.

<script src="https://cdn.jsdelivr.net/npm/simple-tree-utils@1/dist/browser-bundle.min.js"></script>
<script>
    const utils = new treeUtils.TreeUtils();
    const tree = utils.list2Tree(items);
</script>

📖 License

MIT

Keywords

FAQs

Package last updated on 10 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc