0.0.1 • Published 5 months ago

form-tree v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Tree-Util

Build tree with simple API.

Install

npm install form-tree

Usage

import { buildTree } from 'form-tree'

// This first element should not have parentId as it is the root of tree
const { addNode, tree, getNode } = buildTree([
  { id: 'root', someData: 'root node' },
  { id: 'foo', parentId: 'root' },
])
console.log(tree)
/*
  {
    id: 'root',
    someData: 'root node',
    children: [
      {
        id: 'foo',
        children: []
      }
    ]
  }
*/
console.log(getNode('foo'))
/*
  {
    id: 'foo',
    children: []
  }
*/

addNode({ id: 'baz', parentId: 'foo', data: 'new node' })
console.log(getNode('foo'))
/*
  {
    id: 'foo',
    children: [
      {
        id: 'baz',
        data: 'new node',
        children: []
      }
    ]
  }
*/
0.0.1

5 months ago