npm install class-trees
import Tree from 'class-trees';
tree = new Tree('html', { body: 'htmlTagValue' });
tree.addChild('head', { body: '' });
tree.getKey(); // html
Class tree
key
meta
parent
add to Node's children map a child
key
meta
const tree = new Tree
Returns Tree
get Node's key
const tree = new Tree('html');
tree.getKey(); // 'html'
Returns any
get Node's meta
const tree = new Tree('html', { body: '' });
tree.getMeta(); // { body: '' }
Returns any
get Node's parent
const tree = new Tree('html');
const node = tree.addChild('title');
node.getParent() === tree; // true
Returns any
get Node's children
const tree = new Tree('html');
const tree.addChild('head', 'head_meta');
tree.getChildren(); // [{ key: 'head', meta: 'head_meta' }]
Returns Map<any, any>
remove Node's children
key
const tree = new Tree('html');
tree.addChild('head');
tree.removeChild('head'); // true
tree.removeChild('head'); // false
Returns boolean
check if Node has children
const tree = new Tree('html');
tree.hasChildren(); // false
tree.addChild('head');
tree.hasChildren(); // true
Returns boolean
check if Node has child by Key
key
const tree = new Tree('html');
tree.hasChild('head'); // false
tree.addChild('head');
tree.hasChild('head'); // true
Returns boolean
get Node's child by Key
key
const tree = new Tree('html');
tree.getChild('head'); // undefined
tree.addChild('head');
tree.getChild('head').getKey(); // 'head'
Returns any
Get tree's deep child
keys
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