Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/blackflux/object-treeify

Stringify Object as tree structure
https://github.com/blackflux/object-treeify

Last synced: 2 months ago
JSON representation

Stringify Object as tree structure

Awesome Lists containing this project

README

        

# object-treeify

[![Build Status](https://circleci.com/gh/blackflux/object-treeify.png?style=shield)](https://circleci.com/gh/blackflux/object-treeify)
[![NPM](https://img.shields.io/npm/v/object-treeify.svg)](https://www.npmjs.com/package/object-treeify)
[![Downloads](https://img.shields.io/npm/dt/object-treeify.svg)](https://www.npmjs.com/package/object-treeify)

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](https://github.com/notatestuser/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

```js
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.