print-tree

0.1.5 • Public • Published

print-tree Build Status Coverage Status

prints any js data structure as a tree

Usage

$ npm install print-tree
const printTree = require('print-tree');
 
const customTree = {
  name: 'head',
  children: [
    {
      name: 'branchA'
      children: [
        { name: 'branchC' },
      ],
    },
    { name: 'branchB' },
  ],
};
 
printTree(
  customTree,
  node => node.name.toUpperCase(),
  node => node.children,
);
 
// HEAD
// ├─┬ BRANCHA
// | └── BRANCHC
// └── BRANCHB

printTree(tree, [printNode], [getChildren])

Arguments

tree: T

The object to traverse.

printNode: function(node: T, branch: string): ?string

A function to customize the output. You can pass in either:

  • a function that takes the current node and returns the string representation.
  • a function that takes the current node and the branchGraphic and returns nothing - this lets you customize the ouput method

getChildren: function(node: T): Array

A function to help print-tree traverse custom data structures. It takes the current head of the tree/sub-tree and returns a list of the children

Package Sidebar

Install

npm i print-tree

Weekly Downloads

137

Version

0.1.5

License

MIT

Unpacked Size

3.88 kB

Total Files

3

Last publish

Collaborators

  • alex-e-leon