-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (29 loc) · 856 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var chars = {
space: ' ',
pre: '│ ',
first: '├── ',
last: '└── '
};
var toString = function(tree, pre) {
var string = [], childrenPre = [];
tree.forEach(function(node, index) {
var last = index === tree.length - 1;
string.push([].concat(pre, last ? chars.last : chars.first, node.name).join(''));
if (node.children && node.children.length) {
if (pre.length) {
childrenPre = pre.concat(last ? chars.space : chars.pre);
} else {
childrenPre = [last ? chars.space: chars.pre];
}
string = string.concat(toString(node.children, childrenPre));
}
});
return string;
};
module.exports = function(tree) {
var string = [tree.name];
if (tree.children && tree.children.length) {
string = string.concat(toString(tree.children, []));
}
return string.join('\n');
};