The JavaScript function that converts an object to the string, feel free to steal
function treeFormat(obj = {}, rootName){
const nl = /\r\n|\r|\n/,
INDENT = " │", CHILD = " ├",
LAST_CHILD = " └", BLANK = " ",
formatTreeChildren = (node, indentStr) => {
const keys = Object.keys(node);
let output = "";
for (let i = 0; i < keys.length; i++) {
const isLastChild = i === keys.length - 1,
key = keys[i],
val = node[key],
// support keys with newlines in them
keylines = key.split(nl),
child = isLastChild ? LAST_CHILD : CHILD,
nextIndent = indentStr + (isLastChild ? BLANK : INDENT);
output += `${indentStr}${child}${keylines[0]}`;
for (let j = 1; j < keylines.length; j++)
output += nextIndent + keylines[j];
if (typeof val === "object") output += formatTreeChildren(val, nextIndent);
else if (val) output += " : " + val;
}
return output;
},
// If our object has only one property, and no explicit name for the root
// has been given, we turn that one property into the root of the tree
rootKeys = Object.keys(obj);
if (rootKeys.length === 1 && !rootName && typeof obj[rootKeys[0]] === "object") {
obj = obj[rootName = rootKeys[0]];
} else if (!rootName) { rootName = "▓▓▓"; }
return rootName + formatTreeChildren(obj, "\n");
}