1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Tree = void 0;
|
4 | const treeify = require('object-treeify');
|
5 | class 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 |
|
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 | }
|
36 | exports.Tree = Tree;
|
37 | function tree() {
|
38 | return new Tree();
|
39 | }
|
40 | exports.default = tree;
|