npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

text-treeview

v1.0.2

Published

Create a tree hierarcy for console output

Downloads

1,291

Readme

text-treeview

node.js library to write a tree hierarchy for console output.

Build Status codecov Maintainability

The why

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.

treeview

Then I decided it was an excellent little function to share with everyone.

Installation

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.

format function

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