tree-flatter
Transforms a nested tree structure into a flat list, each item has it's children reference and parent reference.
Installation
$ npm install tree-flatter
Usage
Default
; const tree = name: 'item1' children: name: 'item2' children: name: 'item3' name: 'item4' ; const options = node // <= default, consider node => _.clone(node) to avoid mutating the tree; const list = ;ÏÏÏÏÏ
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
; const tree = name: 'item1' objectID: 'aaaaaaaaaaaaaaa' layers: name: 'item2' objectID: 'aaaaaaaaaaaaaaa2' layers: name: 'item3' objectID: 'aaaaaaaaaaaaaaa3' name: 'item4' objectID: 'aaaaaaaaaaaaaaa4' ; const list =
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 * @param * @param * @param * @param * @param * @param * * @return */
Unit test
$ npm t
License
MIT