UNPKG

1.04 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 (const child of Object.keys(this.nodes)) {
14 if (child === key) {
15 return this.nodes[child];
16 }
17 const c = this.nodes[child].search(key);
18 if (c)
19 return c;
20 }
21 }
22 // tslint:disable-next-line:no-console
23 display(logger = console.log) {
24 const addNodes = function (nodes) {
25 const tree = {};
26 for (const p of Object.keys(nodes)) {
27 tree[p] = addNodes(nodes[p].nodes);
28 }
29 return tree;
30 };
31 const tree = addNodes(this.nodes);
32 logger(treeify.asTree(tree));
33 }
34}
35exports.Tree = Tree;
36function tree() {
37 return new Tree();
38}
39exports.default = tree;