Socket
Book a DemoInstallSign in
Socket

object-treeify

Package Overview
Dependencies
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-treeify

Stringify Object as tree structure

Source
npmnpm
Version
1.1.20
Version published
Weekly downloads
3.3M
1.64%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

object

FAQs

Package last updated on 14 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts