1.1.1 • Published 7 years ago

tree-printer v1.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

tree-printer

CLI module to pretty print a tree generated from a collection (array of objects).

Example

var treePrinter = require('tree-printer

console.log(treePrinter([
	{
		name: 'Foo',
		children: [
			{name: 'Foo-Foo'},
			{name: 'Foo-Bar'},
			{name: 'Foo-Baz'},
		],
	},
	{
		name: 'Bar',
		children: [
			{name: 'Bar-Foo'},
			{name: 'Bar-Bar'},
			{
				name: 'Bar-Baz',
				children: [
					{name: 'Bar-Baz-Foo'},
					{name: 'Bar-Baz-Bar'},
					{name: 'Bar-Baz-Baz'},
				],
			},
		],
	},
	{
		name: 'Baz',
		children: [
			{name: 'Baz-Foo'},
			{name: 'Baz-Bar'},
			{name: 'Baz-Baz'},
		],
	},
]));

Outputs:

┬
├─┬ Foo
│ ├── Foo-Foo
│ ├── Foo-Bar
│ └── Foo-Baz
├─┬ Bar
│ ├── Bar-Foo
│ ├── Bar-Bar
│ └─┬ Bar-Baz
│   ├── Bar-Baz-Foo
│   ├── Bar-Baz-Bar
│   └── Bar-Baz-Baz
└─┬ Baz
  ├── Baz-Foo
  ├── Baz-Bar
  └── Baz-Baz

Options

The main treePrinter(tree, options) function takes the collection and an object of options which can be any, all or none of the following:

OptionTypeDefaultDescription
depthNumber0Initial depth of the tree (auto-computed during recursion)
depthsArray[false]Whether each of the depths is on the last item - used to draw the correct '*last' formatter (auto-computed during recursion)
formatObjectsee belowObject of various formatters
format.rootString'┬'Formatter to use when drawing the root node
format.branchNoChildrenString'├── {{name}}'Formatter to use when drawing a branch that has no children
format.branchChildrenString'├─┬ {{name}}'Formatter to use when drawing a branch that has children
format.branchNoChildrenLastString'└── {{name}}'Formatter to use when drawing a branch that has no children and is also the last of the set
format.branchChildrenLastString'└─┬ {{name}}'Formatter to use when drawing a branch that has childre and is also the last of the set
format.depthString'│ 'Formatter to use when drawing a depth marker
format.depthLastString' 'Formatter to use when drawing a depth marker that is also the last of a set
format.eolString'\n'Formatter to use to end a line output
fieldsObjectsee belowObject to indicate which fields should be used during formatting
fields.nameString or Function'name'Which field to draw the nodes name from. If this is a function its called as callback(branch) for each branch and expected to return a string
fields.childrenString'children'Which field to draw the nodes childrens from. This should be an array
autoPrintBooleanfalseWhether to invoke console.log automatically when finished