
Security News
Socket Releases Free Certified Patches for Critical vm2 Sandbox Escape
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.
text-treeview
Advanced tools
node.js library to write a tree hierarchy for console output.
When writing another library I was working with a deep hierarchy of objects that I had to verify.
I iterated the objects and wrote some metadata using space for indenting the lines. But as the hierarchy became more complex it was difficult to follow.
So I wrote a little helper function that could take an array of objects and create a treeview-like experience, making it a lot easer to see the actual structure.

Then I decided it was an excellent little function to share with everyone.
npm install text-treeview
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
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" }
]));
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
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`;
}
}
showRootLines to false to remove the lines on the root levelformat to customize each node. See details below.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.
│ 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 objectparentNode is the parent node of the current nodeThe default function simply looks exactly like this:
function format(indents, treeNode, node) {
return `${indents.join('')}${treeNode}${node.text}\n`;
}
2021-11-03 - Changed test library and cleaned up referencesFAQs
Create a tree hierarcy for console output
We found that text-treeview demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.

Research
Five malicious NuGet packages impersonate Chinese .NET libraries to deploy a stealer targeting browser credentials, crypto wallets, SSH keys, and local files.

Security News
pnpm 11 turns on a 1-day Minimum Release Age and blocks exotic subdeps by default, adding safeguards against fast-moving supply chain attacks.