• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1module.exports = function archy (obj, prefix, opts) {
2    if (prefix === undefined) prefix = '';
3    if (!opts) opts = {};
4    var chr = function (s) {
5        var chars = {
6            '│' : '|',
7            '└' : '`',
8            '├' : '+',
9            '─' : '-',
10            '┬' : '-'
11        };
12        return opts.unicode === false ? chars[s] : s;
13    };
14
15    if (typeof obj === 'string') obj = { label : obj };
16
17    var nodes = obj.nodes || [];
18    var lines = (obj.label || '').split('\n');
19    var splitter = '\n' + prefix + (nodes.length ? chr('│') : ' ') + ' ';
20
21    return prefix
22        + lines.join(splitter) + '\n'
23        + nodes.map(function (node, ix) {
24            var last = ix === nodes.length - 1;
25            var more = node.nodes && node.nodes.length;
26            var prefix_ = prefix + (last ? ' ' : chr('│')) + ' ';
27
28            return prefix
29                + (last ? chr('└') : chr('├')) + chr('─')
30                + (more ? chr('┬') : chr('─')) + ' '
31                + archy(node, prefix_, opts).slice(prefix.length + 2)
32            ;
33        }).join('')
34    ;
35};
36