Create a tree hierarcy for console output
npm install text-treeviewspace for indenting the lines. But as the hierarchy became more complex it was difficult to follow.
npm install text-treeview
`
Basic usage
`
var tree = require('text-treeview');
console.log(tree([
{
text : "Girls",
children : [
"Anna",
"Lisa",
"Bea"
]
},
{
text : "Boys",
children : [
"Kalle",
"Åre",
"Asgar"
]
}
]));
`
Will give you the result:
`
├─ Girls
│ ├─ Anna
│ ├─ Lisa
│ └─ Bea
└─ Boys
├─ Kalle
├─ Åre
└─ Asgar
`
Tree array
Each item in the array must be either a string, or an object with a text property. You can also use the optional children property for an object to add child nodes to the item.
The following snippets will yield identical results:
`
console.log(tree([
"Hello",
"Hej",
"Hohejoj"
]));
console.log(tree([
{ text : "Hello" },
{ text : "Hej" },
{ text : "Hohejoj" }
]));
`
Children
Each child item follow the same pattern. A string or the object described above.
`
console.log(tree([
{
text : "Some items",
children : [
"Item 1",
"Item 2",
{
text : "Item 3",
children : [ "Item 3.1", "Item 3.2"]
}
]
}
]));
`
will give the result
`
└─ Some items
├─ Item 1
├─ Item 2
└─ Item 3
├─ Item 3.1
└─ Item 3.2
`
Options
You pass the options object as the second parameter. These are only a few options and these are the the default ones.
`
{
showRootLines : true,
format : (indents, treeNode, node) => {
return ${indents.join('')}${treeNode}${node.text}\n;
}
}
`
- Set showRootLines to false to remove the lines on the root level
- Use format to customize each node. See details below.
$3
format (indents, treeNode, node, parentNode)
The format function will create each node and will take four parameters
- indents is an array of each treeview "indent" that will build the treeview.
- This array contains all the node indicators further up the tree that other levels and nodes in the tree has created.
- These are either a NODE │ or EMPTY SPACE . These should be joined toghether and put in front of the string.
- treeNode is the actual node indicator for the current node (this is are either a LEAF_NODE (├─ ) or END NODE (└─ ).
- node is the node object
- parentNode is the parent node of the current node
The default function simply looks exactly like this:
`
function format(indents, treeNode, node) {
return ${indents.join('')}${treeNode}${node.text}\n;
}
`
Change log
- 2021-11-03` - Changed test library and cleaned up references