form-tree
v0.0.1
Published
build tree with simple API.
Downloads
1
Readme
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: []
}
]
}
*/