Wondering what’s next for npm?Check out our public roadmap! Β»

    object-treeify

    2.0.0Β β€’Β PublicΒ β€’Β Published

    object-treeify

    Build Status Test Coverage Dependabot Status Dependencies NPM Downloads Semantic-Release Gardener

    Stringify Object as tree structure

    {
        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
        }
    }
    

    Project was inspired by treeify and works almost identical. However the algorithm is much shorter and faster, works without recursion and is very memory efficient. Furthermore the output can be sorted using a custom comparator function.

    Install

    $ npm install --save object-treeify
    

    Usage

    const treeify = require('object-treeify');
    
    treeify({
      oranges: {
        mandarin: {
          clementine: null,
          tangerine: 'so cheap and juicy!'
        }
      },
      apples: {
        gala: null,
        'pink lady': null
      }
    }, {/* options */});
    
    // =>
    // β”œβ”€ oranges
    // β”‚  └─ mandarin
    // β”‚     β”œβ”€ clementine
    // β”‚     └─ tangerine: so cheap and juicy!
    // └─ apples
    //    β”œβ”€ gala
    //    └─ pink lady

    Features

    • Allows for custom sorting
    • Very fast and memory efficient implementation
    • Input traversed exactly once
    • Dependency free and small in size
    • Tests to verify correctness

    Options

    joined

    Type: boolean
    Default: true

    By default a single string is returned. Can be set to false to instead return an array containing lines.

    spacerNoNeighbour

    Type: string
    Default:

    Prefix for depth level when no further neighbour is present.

    spacerNeighbour

    Type: string
    Default: β”‚

    Prefix for depth level when a further neighbour is present.

    keyNoNeighbour

    Type: string
    Default: └─

    Prefix for key when no further neighbour is present.

    keyNeighbour

    Type: string
    Default: β”œβ”€

    Prefix for key when a further neighbour is present.

    sortFn

    Type: function
    Default: null

    Function that defines the key sort order. Defaults to ordering of Object.keys(...), which is typically insertion order.

    Examples

    More examples can be found in the tests.

    Install

    npm i object-treeify

    DownloadsWeekly Downloads

    482,997

    Version

    2.0.0

    License

    MIT

    Unpacked Size

    9.67 kB

    Total Files

    4

    Last publish

    Collaborators

    • avatar