This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
341 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const treeify = require('treeify') | ||
|
||
export class Tree { | ||
nodes: { [key: string]: Tree } = {} | ||
constructor() { } | ||
|
||
insert(child: string, value: Tree = new Tree()): Tree { | ||
this.nodes[child] = value | ||
return this | ||
} | ||
|
||
search(key: string): Tree | undefined { | ||
for (let child of Object.keys(this.nodes)) { | ||
if (child === key) { | ||
return this.nodes[child] | ||
} else { | ||
let c = this.nodes[child].search(key) | ||
if (c) return c | ||
} | ||
} | ||
} | ||
|
||
// tslint:disable-next-line:no-console | ||
display(logger: any= console.log) { | ||
const addNodes = function (nodes: any) { | ||
let tree: { [key: string]: any } = {} | ||
for (let p of Object.keys(nodes)) { | ||
tree[p] = addNodes(nodes[p].nodes) | ||
} | ||
return tree | ||
} | ||
|
||
let tree = addNodes(this.nodes) | ||
logger(treeify.asTree(tree)) | ||
} | ||
} | ||
|
||
export default function tree() { | ||
return new Tree() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {expect, fancy} from 'fancy-test' | ||
|
||
import cli from '../../src' | ||
|
||
describe('styled/tree', () => { | ||
fancy | ||
.stdout() | ||
.end('shows the tree', output => { | ||
let tree = cli.tree() | ||
tree.insert('foo') | ||
tree.insert('bar') | ||
|
||
let subtree = cli.tree() | ||
subtree.insert('qux') | ||
tree.nodes.bar.insert('baz', subtree) | ||
|
||
tree.display() | ||
expect(output.stdout).to.equal(`├─ foo | ||
└─ bar | ||
└─ baz | ||
└─ qux | ||
`) | ||
}) | ||
}) |
Oops, something went wrong.