Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tree-flatter

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tree-flatter

Transforms a nested tree structure into a flat list, each item has it's children reference and parent reference.

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
57
increased by72.73%
Maintainers
1
Weekly downloads
 
Created
Source

tree-flatter

Transforms a nested tree structure into a flat list, each item has it's children reference and parent reference.

version Node.js Dependency Status devDependency Status Build Status

Installation

$ npm install tree-flatter

Usage

Default
import treeFlatter from 'tree-flatter';

const tree = [
    {
        name: 'item1',
        children: [
            {
                name: 'item2',
                children: [
                    {name: 'item3'}
                ]
            },
            { name: 'item4' }
        ]
    }
];

const options = {
    initNode: node => node, // <= default, consider node => _.clone(node) to avoid mutating the tree
};

const list = treeFlatter(tree, 'children', options);ÏÏÏÏÏ

Results in:

[
    {id: 1, name: 'item1', children: [2, 4]},
    {id: 2, name: 'item2', children: [3], parent: 1},
    {id: 3, name: 'item3', parent: 2},
    {id: 4, name: 'item4', parent: 1}
]
Specifies item idKey and children itemKey
import treeFlatter from 'tree-flatter';

const tree = [
    {
        name: 'item1',
        objectID: 'aaaaaaaaaaaaaaa',
        layers: [
            {
                name: 'item2',
                objectID: 'aaaaaaaaaaaaaaa2',
                layers: [
                    {
                      name: 'item3',
                      objectID: 'aaaaaaaaaaaaaaa3',
                    }
                ]
            },
            {
              name: 'item4',
              objectID: 'aaaaaaaaaaaaaaa4',
            }
        ]
    }
];

const list = treeFlatter(tree, , {
    idKey: 'objectID',
    itemsKey: 'layers',
);

Results in:

[
      { objectID: 'aaaaaaaaaaaaaaa', name: 'item1', layers: ['aaaaaaaaaaaaaaa2', 'aaaaaaaaaaaaaaa4'] },
      { objectID: 'aaaaaaaaaaaaaaa2', name: 'item2', layers: ['aaaaaaaaaaaaaaa3'], parent: 'aaaaaaaaaaaaaaa' },
      { objectID: 'aaaaaaaaaaaaaaa4', name: 'item4', parent: 'aaaaaaaaaaaaaaa' },
      { objectID: 'aaaaaaaaaaaaaaa3', name: 'item3', parent: 'aaaaaaaaaaaaaaa2' },
]

API

/**
 * Flatten tree-object
 *
 * @param {Array|Object} tree - Object tree
 * @param {Object} options - Config options
 * @param {Function=} [options.initNode=(node=>node)] - Initialize node
 * @param {String=} [options.itemsKey='children'] - Specifies item's children itemsKey
 * @param {String=} [options.idKey='id'] - Specifies item's idKey
 * @param {Number=} [options.uniqueIdStart=1] - Unique id start
 * @param {Function=} [options.generateUniqueId=(() => settings.uniqueIdStart++)] - Unique id generator
 *
 * @return {Object[]} Flatten collection
 */

Unit test

$ npm t

License

MIT

FAQs

Package last updated on 06 Jun 2017

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc