UNPKG

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