Giter VIP home page Giter VIP logo

treeify's Introduction

treeify

Build Status

treeify converts a JS object into a nice, visible depth-indented tree for console printing. The structure generated is similar to what you get by running the tree command on Unixy platforms.

{
    oranges: {
        'mandarin': {                                          ├─ oranges
            clementine: null,                                  │  └─ mandarin
            tangerine: 'so cheap and juicy!'        -=>        │     ├─ clementine
        }                                                      │     └─ tangerine: so cheap and juicy!
    },                                                         └─ apples
    apples: {                                                     ├─ gala
        'gala': null,                                             └─ pink lady
        'pink lady': null
    }
}

It also works well with larger nested hierarchies such as file system directory trees. In fact, the fs_tree example does a pretty good job of imitating tree. Try it out!

See the other included examples or the test suite for usage scenarios.

Getting it

For use with node.js

First you'll want to run this command in your project's root folder:

$ npm install treeify

Then proceed to use it in your project:

var treeify = require('treeify');
console.log(
   treeify.asTree({
      apples: 'gala',      //  ├─ apples: gala
      oranges: 'mandarin'  //  └─ oranges: mandarin
   }, true)
);

For use in a browser

Treeify cooperates with Node, AMD or browser globals to create a module. This means it'll work in a browser regardless of whether you have an AMD-compliant module loader or not. If such a loader isn't found when the script is executed, you may access Treeify at window.treeify.

Usage

The methods exposed to you are as follows, in a strange kind of signature notation:

asLines()

treeify.asLines(obj, showValues (boolean), [hideFunctions (boolean),] lineCallback (function))
// NOTE: hideFunctions is optional and may be safely omitted - this was done to ensure we don't break uses of the previous form

asTree()

treeify.asTree(obj, showValues (boolean), hideFunctions (boolean)): String

Running the tests

There's a pretty extensive suite of Vows tests included.

$ npm test

treeify's People

Contributors

darvin avatar justusjonas74 avatar notatestuser avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

treeify's Issues

Date type is not supported

I want to treeify an object which consists of dates. But it seems, that dates are ignored.
Example:

const treeify = require('treeify');
const today = new Date();
const obj = { string: 'hello', date: today};
console.log(treeify.asTree(obj, true));

Expected:

├─ string: hello
└─ date:  2018-01-01T00:00:00.000Z

Actual:

├─ string: hello
└─ date

Add unicode or embolden keys

I would like to add unicode to the tree and also use something like chalk.bold() on the tree. For example, I have this:

     └─ npm-link-up (root)
           └─ node_modules
              ├─ @oresoftware/shell
              ├─ json-stdio
              ├─ prepend-transform
              └─ residence

and it would be really awesome to have this:

     └─ npm-link-up (root)
           └─ node_modules
              ├─ @oresoftware/shell  ⏰
              ├─ json-stdio  ⏰
              ├─ prepend-transform ⏰
              └─ residence ⏰

and have bold branches using chalk.bold(), etc, is it possible somehow?

Arrays

It seems that array values are not supported:

treeify.asTree({"foo": ["bar", "baz"]})

returns:

└─ foo
   ├─ 0
   └─ 1

it would be nice if it show:

└─ foo
   ├─ bar
   └─ baz

I'm using version 1.0.1 from unpkg.com

feat: auto coloring

I think it would be cool if there's optional auto coloring mechanism that makes the tree more readable. For example, items of the same level share the same color.

Thanks

Exclude examples and test from npm package

Could examples and test directories be excluded from the npm package? I doubt they are needed by developers when using this library, and examples may require manual removal if library is used in commercial project.

// Based on information taken from the Tree of Life web project
// http://tolweb.org/Eukaryotes/3

I didn't research licensing in-depth, but at a glance it may be problematic as it mentions:

The TEXT of this page is licensed under the Creative Commons Attribution-NonCommercial License - Version 3.0
For the general terms and conditions of ToL material reuse and redistribution, please see the Tree of Life Copyright Policies.

Create LICENSE file

Can you please create a LICENSE file with your copyright information and the text of your license. It would really help people like me who work on packaging modules for Linux distributions, because we need to check the licenses of all components we package.

TypeError: obj.hasOwnProperty is not a function at filterKeys

err in nodejs http.reques

const http = require("http");
const treeify = require('treeify');
let r = http.request({
    hostname: 'ya.ru'
}, (_r) => {
    let str = treeify.asTree(_r);
    console.log(str);
});
r.on('error', console.error);
r.write('');
r.end();

https://github.com/notatestuser/treeify/blob/master/treeify.js#L32

  let count = ((count = 0, timer = null) => () => {
    count++;
    clearTimeout(timer)
    timer = setTimeout(() => {
      console.log(`"if (!obj.hasOwnProperty)" counted:${count}`)
    }, 1e3);
  })()
  function filterKeys(obj, hideFunctions) {
    var keys = [];
    for (var branch in obj) {
      // always exclude anything in the object's prototype
      if (!obj.hasOwnProperty) count()
      if (!obj.hasOwnProperty || !obj.hasOwnProperty(branch)) {
        continue;
      }
      // ... and hide any keys mapped to functions if we've been told to
      if (hideFunctions && ((typeof obj[branch]) === "function")) {
        continue;
      }
      keys.push(branch);
    }
    return keys;
  }

result:
"if (!obj.hasOwnProperty)" counted:71

callback is not a function

This works:

treeify.asTree({foo: 10})

but this throws an error

treeify.asLines({foo: 10})

Uncaught TypeError: callback is not a function
at growBranch (treeify.js:67)
at treeify.js:78
at Array.forEach ()
at growBranch (treeify.js:73)
at Object.Treeify.asLines (treeify.js:94)
at :1:9

I'm using version from npm via unpkg.com from browser (with my terminal library).

Tested here https://codepen.io/jcubic/pen/BwBYOZ

Build is unknown

And when you click on badge it open travis with not found page.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.