objectree
v1.1.1
Published
Objectree is a javascript node.js module that can convert a javascript or JSON object to a tree and access as a tree structure.
Downloads
4
Readme
objectree
About
Objectree is a javascript node.js module that can convert a javascript or JSON object to a tree and access as a tree structure.
Installation
npm install objectree
Usage
var objectree = require("objectree");
var obj = {
"a": "test",
"b": true,
"c": 20,
"d": {
"e": {
"f": "test_a",
"g": "test_b"
}
}
};
var fullTree = objectree.createTree(obj);
.....
Features
- A
tree
structure is created from javascript/json object. tree
is a node that containschild
andparent
concept- Loop through the structure with
child
andparent
facilities. - Represent a Tree structured node.
Documentation
Methods
createTree
newNode
hasChild
hasAttribute
getName
addChild
getChilds
getChildByIndex
getFirstChild
addAttribute
getAttribute
setParent
getParent
setValue
getValue
hasSibling
isLeaf
serialize
createTree()
Create a tree
node of the given obj
to have the tree
structure
Arguments
obj
- A javascript object that is need to be converted to tree structure.
Returns
Tree object
Examples
// obj -> is a javascript object.
var treeObject = objectree.createTree(obj);
newNode()
Explicitly create a new Node to add or remove.
Arguments
void
Returns
Node
Examples
var newNode = objectree.newNode();
hasChild()
To check if the object has any child
Arguments
void
Returns
true
orfalse
Examples
var hasChild = treeObject.hasChild();
hasAttribute()
To check if the object has any attributes
Arguments
void
Returns
true
orfalse
Examples
var hasAttribute = treeObject.hasAttribute();
getName()
Get name of the current node.
Arguments
void
Returns
string
name
Examples
var name = treeObject.getName();
addChild()
Add a node to current node child list
Arguments
Node
to add as a child
Returns
void
Note
- Get a child from node or create a node with
objectree.newNode()
Examples
test.addChild(treeObject.getFirstChild());
getChilds()
Get all childs list of current node.
Arguments
void
No argument
Returns
child list
Examples
var childs = test.getChilds();
getFirstChild()
Get first child of current node.
Arguments
void
Returns
Node
first child ornull
Examples
var firstChild = test.getFirstChild();
getChildByIndex()
Get child by index number
Arguments
number
0 based
Returns
Node
child of given index or 'null'
Examples
var child = test.getChildByIndex(2);
addAttribute()
Add attribute to current node.
Arguments
Array
orString
orNumber
Object
andfunction
are not supported
Returns
void
Examples
test.addAttribute('abcd');
getAttribute()
Add a node to current node child list
Arguments
void
Returns
attribute list
ornull
Examples
var attributes = test.getAttribute();
setParent()
Set parent of current node explicitly. ( Not suggested )
Arguments
Node
Returns
void
Examples
var newParent = objectree.newNode();
treeObject.setParent(newParent);
getParent()
Get parent node of current node.
Arguments
void
Returns
- parent
Node
ornull
Examples
var parent = treeObject.getParent();
setValue()
Set value of current node.
Arguments
String
orBoolean
orNumber
function
andObject
is not supported
Returns
void
Examples
treeObject.setValue("Hello World");
getValue()
Get value of current node.
Arguments
void
Returns
Value
ornull
Examples
var value = treeObject.getValue();
hasChild()
To check if the object has any child
Arguments
void
Returns
true
orfalse
Examples
var hasChild = treeObject.hasChild();
hasSibling()
To check if current node has any sibling or not
Arguments
void
Returns
true
orfalse
Examples
var hasSibling = treeObject.hasSibling();
isLeaf()
To check if the object is leaf
Arguments
void
Returns
true
orfalse
Examples
var isLeaf = treeObject.isLeaf();
serialize()
Serialize the object before sending over or string conversion.
Arguments
void
Returns
String
Examples
var isLeaf = treeObject.serialize();