0.0.2 • Published 6 years ago

arborea v0.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

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

Kind: global class
Properties

NameTypeDescription
rootNodeThe root Node of the tree

new Tree(nodes)

Create a new tree or import one

ParamTypeDescription
nodesstring | ArrayNodes 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

ParamTypeDescription
idnumberNode ID

tree.createNode(title, content) ⇒ Node

Creates a new Node in the tree.

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

ParamTypeDescription
titlestringNode title. Defaults to ''.
contentstringNode content. Defaults to ''.

tree.deleteNode(node, recursive)

Deletes passed node from the tree.

Kind: instance method of Tree

ParamTypeDefaultDescription
nodeNode | numberThe Node to be deleted or its ID
recursivebooleanfalseIf 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

ParamTypeDescription
nodesstring | ArrayThe 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

NameTypeDescription
idnumberNode ID
titlestringNode title
contentstringNode content
childrenArray.<Node>Child Nodes
parentNodeParent 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

ParamTypeDescription
nodeNodethe 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

ParamTypeDescription
nodeNodethe 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

ParamTypeDescription
titlestringNode title. Defaults to ''.
contentstringNode 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

ParamTypeDescription
nodeNodethe Node to be deleted
0.0.2

6 years ago

0.0.1

6 years ago