UNPKG

1.07 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const treeify = require('treeify');
4class Tree {
5 constructor() {
6 this.nodes = {};
7 }
8 insert(child, value = new Tree()) {
9 this.nodes[child] = value;
10 return this;
11 }
12 search(key) {
13 for (let child of Object.keys(this.nodes)) {
14 if (child === key) {
15 return this.nodes[child];
16 }
17 else {
18 let c = this.nodes[child].search(key);
19 if (c)
20 return c;
21 }
22 }
23 }
24 // tslint:disable-next-line:no-console
25 display(logger = console.log) {
26 const addNodes = function (nodes) {
27 let tree = {};
28 for (let p of Object.keys(nodes)) {
29 tree[p] = addNodes(nodes[p].nodes);
30 }
31 return tree;
32 };
33 let tree = addNodes(this.nodes);
34 logger(treeify.asTree(tree));
35 }
36}
37exports.Tree = Tree;
38function tree() {
39 return new Tree();
40}
41exports.default = tree;