pretty-tree
Advanced tools
Comparing version 0.1.2 to 0.2.0
99
index.js
var archy = require('archy'); | ||
var chalk = require('chalk'); | ||
var isAtomic = function(v) { | ||
return v === null || v === undefined || typeof v !== 'object'; | ||
}; | ||
var echo = function(val) { | ||
return val | ||
} | ||
var leaf = function(obj) { | ||
if (isAtomic(obj)) return [''+obj]; | ||
var tree = function(color) { | ||
var cyan = color ? chalk.cyan : echo | ||
var grey = color ? chalk.grey : echo | ||
var yellow = color ? chalk.yellow : echo | ||
var keys = Object.keys(obj); | ||
var isArray = Array.isArray(obj); | ||
var nodes = []; | ||
var isAtomic = function(v) { | ||
return v === null || v === undefined || typeof v !== 'object'; | ||
}; | ||
var atomic = keys.filter(function(key) { | ||
return isAtomic(obj[key]); | ||
}); | ||
var leaf = function(obj) { | ||
if (isAtomic(obj)) return [''+obj]; | ||
var nonAtomic = keys.filter(function(key) { | ||
return !isAtomic(obj[key]); | ||
}); | ||
var keys = Object.keys(obj); | ||
var isArray = Array.isArray(obj); | ||
var nodes = []; | ||
var pad = atomic.reduce(function(max, val) { | ||
return max.length >= val.length ? max : val.replace(/./g, ' '); | ||
}, ' '); | ||
var atomic = keys.filter(function(key) { | ||
return isAtomic(obj[key]); | ||
}); | ||
if (!atomic.length && !nonAtomic.length) return [chalk.grey('(empty)')]; | ||
var nonAtomic = keys.filter(function(key) { | ||
return !isAtomic(obj[key]); | ||
}); | ||
atomic.forEach(function(key) { | ||
var val = (obj[key]+'').replace(/\n/g, '\n '+pad); | ||
key = key+':'+pad.slice(key.length-pad.length-1); | ||
nodes.push(isArray ? val : (chalk.cyan(key)+val)); | ||
}); | ||
var pad = atomic.reduce(function(max, val) { | ||
return max.length >= val.length ? max : val.replace(/./g, ' '); | ||
}, ' '); | ||
nonAtomic.forEach(function(key) { | ||
nodes.push({label:isArray ? undefined : chalk.cyan(key), nodes:leaf(obj[key])}); | ||
}); | ||
if (!atomic.length && !nonAtomic.length) return [grey('(empty)')]; | ||
return nodes; | ||
}; | ||
atomic.forEach(function(key) { | ||
var val = (obj[key]+'').replace(/\n/g, '\n '+pad); | ||
key = key+':'+pad.slice(key.length-pad.length-1); | ||
nodes.push(isArray ? val : (cyan(key)+val)); | ||
}); | ||
var visit = function(node) { | ||
if (node.label) node.label = chalk.yellow(node.label); | ||
if (node.nodes) node.nodes = [].concat(node.nodes).map(visit); | ||
if (node.leaf) node.nodes = [].concat(node.nodes || [], leaf(node.leaf)); | ||
if (node.label && (!node.nodes || !node.nodes.length)) node.nodes = [chalk.grey('(empty)')]; | ||
return node; | ||
}; | ||
var tree = function(node) { | ||
return archy(visit(node)) | ||
.replace(/([├└])─┬ \n[│ ]+├/gm, '$1─┬') | ||
.replace(/([├└])─┬ \n[│ ]+└/gm, '$1──') | ||
.replace(/[┬├─└│┐]/g, function(_) { | ||
return chalk.grey(_); | ||
nonAtomic.forEach(function(key) { | ||
nodes.push({label:isArray ? undefined : cyan(key), nodes:leaf(obj[key])}); | ||
}); | ||
}; | ||
module.exports = tree; | ||
return nodes; | ||
}; | ||
var visit = function(node) { | ||
if (node.label) node.label = yellow(node.label); | ||
if (node.nodes) node.nodes = [].concat(node.nodes).map(visit); | ||
if (node.leaf) node.nodes = [].concat(node.nodes || [], leaf(node.leaf)); | ||
if (node.label && (!node.nodes || !node.nodes.length)) node.nodes = [grey('(empty)')]; | ||
return node; | ||
}; | ||
return function(node) { | ||
return archy(visit(node)) | ||
.replace(/([├└])─┬ \n[│ ]+├/gm, '$1─┬') | ||
.replace(/([├└])─┬ \n[│ ]+└/gm, '$1──') | ||
.replace(/[┬├─└│┐]/g, function(_) { | ||
return grey(_); | ||
}); | ||
}; | ||
} | ||
module.exports = tree(true); | ||
module.exports.plain = tree(false); |
{ | ||
"name": "pretty-tree", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Make colorful trees out of JSON objects using archy", | ||
@@ -5,0 +5,0 @@ "repository": "git://github.com/mafintosh/pretty-tree.git", |
@@ -45,4 +45,6 @@ # pretty-tree | ||
If you want to disable coloring (even when the terminal is a tty) use `tree.plain(options)` | ||
## License | ||
MIT |
14528
113
49