arborea

Create, manage and traverse tree structures

Usage no npm install needed!

<script type="module">
  import arborea from 'https://cdn.skypack.dev/arborea';
</script>

README

Arborea

Overview

Arborea lets you create, manage and traverse tree structures. It's very basic and simple.

Quick start

First, make sure Node is installed on your machine. Then run this in your project folder:

$ npm i arborea

Create a tree:

const Tree = require('arborea')

const tree = new Tree()
tree.root.title = 'R00t'
tree.root.content = 'Hello world!'

const node = tree.createNode('foo', 'bar')
tree.root.appendChild(node)

Classes

Tree

Main class

Node

A node inside a tree. Nodes are created by instances of Tree; do not use this constructor to create Nodes!

Tree

Main class

Kind: global class
Properties

Name Type Description
root Node The root Node of the tree

new Tree([nodes])

Create a new tree or import one

Param Type Description
[nodes] string | Array Nodes to be imported. If a string is provided, it will be parsed as JSON. If no value is provided, a new Tree with a root Node will be created.

tree.getNode([id]) ⇒ Node

Takes a Node ID and returns the corresponding node.

Kind: instance method of Tree
Returns: Node - The Node that got got

Param Type Description
[id] number Node ID

tree.createNode([title], [content]) ⇒ Node

Creates a new Node in the tree.

Kind: instance method of Tree
Returns: Node - The created Node

Param Type Description
[title] string Node title. Defaults to ''.
[content] string Node content. Defaults to ''.

tree.deleteNode(node, recursive)

Deletes passed node from the tree.

Kind: instance method of Tree

Param Type Default Description
node Node | number The Node to be deleted or its ID
recursive boolean false If set to true, child nodes will also be deleted. If set to false, existing child nodes will throw an error. Defaults to false.

tree.import(nodes)

Imports a tree structure to replace the current one. If a string is provided, it will be parsed as JSON. This method is automatically called if an argument is provided to the Tree constructor.

Kind: instance method of Tree

Param Type Description
nodes string | Array The Nodes to be imported. Typically parsed JSON.

tree.export() ⇒ string

Exports the nodes in the tree as a JSON string. Data of this type can later be imported to recreate the tree.

Kind: instance method of Tree
Returns: string - Nodes in JSON format.

Node

A node inside a tree. Nodes are created by instances of Tree; do not use this constructor to create Nodes!

Kind: global class
Properties

Name Type Description
id number Node ID
title string Node title
content string Node content
children Array.<Node> Child Nodes
parent Node Parent Node

node.appendChild(node)

Appends the passed Node to this Node. If passed Node is child to another Node, it will be removed as a child from that one.

Kind: instance method of Node

Param Type Description
node Node the Node to be made child

node.removeChild(node)

Removes the passed Node as a child from this Node. The Node is not deleted from the Tree.

Kind: instance method of Node

Param Type Description
node Node the Node to be orphaned

node.createChild([title], [content]) ⇒ Node

Creates a new Node in the Tree and appends it as a child to this Node.

Kind: instance method of Node
Returns: Node - The created Node

Param Type Description
[title] string Node title. Defaults to ''.
[content] string Node content. Defaults to ''.

node.deleteChild(node)

Removes the passed Node as a child from this Node and deletes it from the Tree.

Kind: instance method of Node

Param Type Description
node Node the Node to be deleted