1.0.1 • Published 7 years ago
class-trees v1.0.1
class Trees
Install
npm install class-treesUsing
import Tree from 'class-trees';
tree = new Tree('html', { body: 'htmlTagValue' });
tree.addChild('head', { body: '' });
tree.getKey(); // htmlDocumentation
Table of Contents
Tree
Class tree
Parameters
keymetaparent
addChild
add to Node's children map a child
Parameters
keymeta
Examples
const tree = new TreeReturns 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; // trueReturns 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'); // falseReturns boolean
hasChildren
check if Node has children
Examples
const tree = new Tree('html');
tree.hasChildren(); // false
tree.addChild('head');
tree.hasChildren(); // trueReturns 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'); // trueReturns 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']); // undefinedReturns any