4.0.1 • Published 8 months ago

object-treeify v4.0.1

Weekly downloads
581,770
License
MIT
Repository
github
Last release
8 months ago

object-treeify

Build Status NPM Downloads

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
  • Handles circular references

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.

separator

Type: string Default: :

Used to separate node key from node value.

renderFn

Type: function Default: (node) => (['boolean', 'string', 'number'].includes(typeof node) ? node : undefined)

Can be used to overwrite the node rendering logic. Node is rendered if result is not equal undefined.

sortFn

Type: function Default: null

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

breakCircularWith

Type: string or null Default: (circular ref.)

When string, circular references are broken with that string, at a minor performance cost.

Examples

More examples can be found in the tests.

4.0.1

8 months ago

4.0.0

8 months ago

3.2.1

2 years ago

3.2.0

2 years ago

3.0.0

2 years ago

2.1.1

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

2.0.1

3 years ago

1.1.33

3 years ago

1.1.32

3 years ago

2.0.0

3 years ago

1.1.31

3 years ago

1.1.30

3 years ago

1.1.29

3 years ago

1.1.28

4 years ago

1.1.27

4 years ago

1.1.26

4 years ago

1.1.25

4 years ago

1.1.24

4 years ago

1.1.23

4 years ago

1.1.22

4 years ago

1.1.21

4 years ago

1.1.20

5 years ago

1.1.19

5 years ago

1.1.18

5 years ago

1.1.17

5 years ago

1.1.16

5 years ago

1.1.15

5 years ago

1.1.14

5 years ago

1.1.13

5 years ago

1.1.12

5 years ago

1.1.11

5 years ago

1.1.10

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago