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

oo-ascii-tree

v1.105.0

Published

object-oriented ascii tree renderer

Downloads

1,016,452

Readme

oo-ascii-tree

Renders ASCII trees from an object-oriented object graph.

Features:

  • Multiline text
  • Multiple root nodes

Roadmap:

  • Customization of tree formatting and indentation size

Install

$ npm i oo-ascii-tree

Basic Example

import { AsciiTree } from '../lib';

const tree = new AsciiTree('root');

tree.add(new AsciiTree('child1'));

tree.add(new AsciiTree('child2',
  new AsciiTree('grandchild1'),
  new AsciiTree('grandchild2')
));

tree.add(new AsciiTree('child3'));

tree.printTree();

Prints the following tree to stdout:

root
 ├── child1
 ├─┬ child2
 │ ├── grandchild1
 │ └── grandchild2
 └── child3

Advanced Usage

You can also subclass AsciiTree to encapsulate some model. The following example declares a TitleNode which formats a title with "====" underline and a KeyValueNode which formats text as key: value:

class TitleNode extends AsciiTree {
  constructor(title: string, ...children: AsciiTree[]) {
    super([
      title.toLocaleUpperCase(),
      '='.repeat(title.length)
    ].join('\n'), ...children);
  }
}

class KeyValueNode extends AsciiTree {
  constructor(key: string, value: string) {
    super(`${key}: ${value}`);
  }
}

const tree = new AsciiTree();

tree.add(new TitleNode('props',
  new KeyValueNode('shape', 'circle'),
  new KeyValueNode('color', 'red'),
  new KeyValueNode('background', 'blue')
));

tree.add(new TitleNode('dimensions',
  new KeyValueNode('width', '30px'),
  new KeyValueNode('height', '40px')
));

Will emit the following output:

PROPS
=====
 ├── shape: circle
 ├── color: red
 └── background: blue
DIMENSIONS
==========
 ├── width: 30px
 └── height: 40px

API

The AsciiTree class represents a tree/node/subtree. Each node in the tree includes text and children and can be printed to a Writable via printTree(stream).

new AsciiTree([text[, children...]])

Creates a new node with the specified text and optionally adds child nodes to it.

If text is not specified, the children of this node will all be considered roots (level 0) and the root node will get level -1. This allows modeling trees with a single root or with multiple roots.

asciiTree.add(children...)

Adds one or more children to the node.

asciiTree.printTree([writableStream])

Emits an ASCII print out of the tree to the specified Writable stream. The default is process.stdout.

asciiTree.toString()

Returns a string representation of the tree.

asciiTree.text

The node's text. If the text contains multiple line separated by \n, new lines will be aligned to the node's indentation.

asciiTree.children

Returns a copy of the array of children of this node. Use asciiTree.add to add children.

asciiTree.root

Returns true if this is the root node.

asciiTree.last

Returns true if this is the last child of a node.

asciiTree.level

Returns the node level. Root node(s) will have a level of 0.

If the root AsciiTree has text, it's level will be 0 and its children will get level 1. If the root AsciiTree does not have text, it's level will be -1 and all it's children will get level 0.

asciiTree.empty

Returns true if this node does not have any children.

asciiTree.ancestors

Returns all the nodes that are ancestors of this node ordered from root to the direct parent.

License

Distributed under the Apache License, Version 2.0.

See LICENSE and NOTICE for more information.