class-trees
v1.0.1
Published
tree data structure
Downloads
4
Readme
class Trees
Install
npm install class-trees
Using
import Tree from 'class-trees';
tree = new Tree('html', { body: 'htmlTagValue' });
tree.addChild('head', { body: '' });
tree.getKey(); // html
Documentation
Table of Contents
Tree
Class tree
Parameters
key
meta
parent
addChild
add to Node's children map a child
Parameters
key
meta
Examples
const tree = new Tree
Returns Tree
getKey
get Node's key
Examples
const tree = new Tree('html');
tree.getKey(); // 'html'
Returns any
getMeta
get Node's meta
Examples
const tree = new Tree('html', { body: '' });
tree.getMeta(); // { body: '' }
Returns any
getParent
get Node's parent
Examples
const tree = new Tree('html');
const node = tree.addChild('title');
node.getParent() === tree; // true
Returns any
getChildren
get Node's children
Examples
const tree = new Tree('html');
const tree.addChild('head', 'head_meta');
tree.getChildren(); // [{ key: 'head', meta: 'head_meta' }]
Returns Map<any, any>
removeChild
remove Node's children
Parameters
key
Examples
const tree = new Tree('html');
tree.addChild('head');
tree.removeChild('head'); // true
tree.removeChild('head'); // false
Returns boolean
hasChildren
check if Node has children
Examples
const tree = new Tree('html');
tree.hasChildren(); // false
tree.addChild('head');
tree.hasChildren(); // true
Returns boolean
hasChild
check if Node has child by Key
Parameters
key
Examples
const tree = new Tree('html');
tree.hasChild('head'); // false
tree.addChild('head');
tree.hasChild('head'); // true
Returns boolean
getChild
get Node's child by Key
Parameters
key
Examples
const tree = new Tree('html');
tree.getChild('head'); // undefined
tree.addChild('head');
tree.getChild('head').getKey(); // 'head'
Returns any
getDeepChild
Get tree's deep child
Parameters
keys
Examples
const tree = new Tree('html');
const headNode = tree.addChild('head');
const metaNode = headNode.addChild('meta');
metaNode === tree.getDeepChild(['head', 'meta']);
headNode === tree.getDeepChild(['head']);
tree.getDeepChild(['head', 'wrongKey']); // undefined
Returns any