Skip to content
Convert JSON object to the delightful string representation of a directory structure
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
bin
.editorconfig
.gitignore
LICENSE
README.md
index.js
package.json
test.input.json
test.js

README.md

hierarchy

Convert JSON object to a beautiful string representation of a directory structure

Like archy, but easier. Inspired in the text-tree found in an answer on StackOverflow.

Installation

npm i --save hierarchyjs

Usage

Considering the input.json file:

{
    "foo": {
        "bar": {
            "$files": ["baz.js", "piece.js"]
        },
        "paz": {}
    },
    "baz": {
        "$files": ["taz.js"]
    },
    "$files": ["Gruntfile.js", "package.json"]
}

From the command line

hierarchy input.json

In code

var hierarchy = require('hierarchy');
var input = require('input.json');
var tree = hierarchy(input);

console.log(tree);

Notation

The input is expected to be a tree-like object with optional $files arrays that describe a list of files in a directory. The $files construct can actually be avoided by treating files as empty directories, but its a little convenience sugar.

The following has the exact same output as the previous example.

{
    "foo": {
        "bar": {
            "$files": {
                "baz.js": {},
                "piece.js": {}
            },
        },
        "paz": {}
    },
    "baz": {
        "taz.js": {}
    },
    "Gruntfile.js": {},
    "package.json": {}
}

A file named $etc might be used to include a "and more.." '....' kind of message.

License

MIT

Something went wrong with that request. Please try again.