Skip to content

mahfuzsust/objectree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

objectree

NPM version Downloads apache license

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 contains child and parent concept
  • Loop through the structure with child and parent facilities.
  • Represent a Tree structured node.

Documentation

Methods


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 or false

Examples

var hasChild = treeObject.hasChild();

hasAttribute()

To check if the object has any attributes

Arguments

  • void

Returns

  • true or false

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 or null

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 or String or Number
  • Object and function are not supported

Returns

  • void

Examples

test.addAttribute('abcd'); 

getAttribute()

Add a node to current node child list

Arguments

  • void

Returns

  • attribute list or null

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 or null

Examples

var parent = treeObject.getParent();

setValue()

Set value of current node.

Arguments

  • String or Boolean or Number
  • function and Object is not supported

Returns

  • void

Examples

treeObject.setValue("Hello World");

getValue()

Get value of current node.

Arguments

  • void

Returns

  • Value or null

Examples

var value = treeObject.getValue();

hasChild()

To check if the object has any child

Arguments

  • void

Returns

  • true or false

Examples

var hasChild = treeObject.hasChild();

hasSibling()

To check if current node has any sibling or not

Arguments

  • void

Returns

  • true or false

Examples

var hasSibling = treeObject.hasSibling();

isLeaf()

To check if the object is leaf

Arguments

  • void

Returns

  • true or false

Examples

var isLeaf = treeObject.isLeaf();

serialize()

Serialize the object before sending over or string conversion.

Arguments

  • void

Returns

  • String

Examples

var isLeaf = treeObject.serialize();

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published