All Projects → iyegoroff → un-flatten-tree

iyegoroff / un-flatten-tree

Licence: MIT license
A small npm module for converting trees to lists and vice versa.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to un-flatten-tree

Scikit Garden
A garden for scikit-learn compatible trees
Stars: ✭ 230 (+1542.86%)
Mutual labels:  tree
data-structure-project
自己实现集合框架系列整理总结
Stars: ✭ 29 (+107.14%)
Mutual labels:  tree
JLBoost.jl
A 100%-Julia implementation of Gradient-Boosting Regression Tree algorithms
Stars: ✭ 65 (+364.29%)
Mutual labels:  tree
Libdict
C library of key-value data structures.
Stars: ✭ 234 (+1571.43%)
Mutual labels:  tree
codacy-scalameta
Codacy tool for Scalameta
Stars: ✭ 35 (+150%)
Mutual labels:  tree
Data-Structure-Algorithm-Programs
This Repo consists of Data structures and Algorithms
Stars: ✭ 464 (+3214.29%)
Mutual labels:  tree
Vuejs Tree
A highly customizable and blazing fast Vue tree component ⚡🌲
Stars: ✭ 211 (+1407.14%)
Mutual labels:  tree
nub
A rendering and interaction Processing library
Stars: ✭ 28 (+100%)
Mutual labels:  tree
RedisTree
Redis Tree (Ploytree) Structure Module
Stars: ✭ 64 (+357.14%)
Mutual labels:  tree
ng-treetable
A treetable module for angular 5
Stars: ✭ 32 (+128.57%)
Mutual labels:  tree
Computer Science In Javascript
Computer science reimplemented in JavaScript
Stars: ✭ 2,590 (+18400%)
Mutual labels:  tree
mst-persist
Persist and hydrate MobX-state-tree stores (in < 100 LoC)
Stars: ✭ 75 (+435.71%)
Mutual labels:  tree
vuejs-tree
A highly customizable and blazing fast Vue tree component ⚡🌲
Stars: ✭ 310 (+2114.29%)
Mutual labels:  tree
Dsladapter
🔥 Kotlin时代的Adapter, Dsl 的形式使用 RecyclerView.Adapter, 支持折叠展开, 树结构,悬停,情感图状态切换, 加载更多, 多类型Item,侧滑菜单等
Stars: ✭ 231 (+1550%)
Mutual labels:  tree
mongodb-tree-structure
Implementing Tree Structure in MongoDB
Stars: ✭ 14 (+0%)
Mutual labels:  tree
Element Tree Grid
tree grid extends element ui with vue
Stars: ✭ 223 (+1492.86%)
Mutual labels:  tree
react-vertical-tree
Simple & lightweight vertical tree like view.
Stars: ✭ 23 (+64.29%)
Mutual labels:  tree
avl array
High performance templated AVL tree using a fixed size array. Extensive test suite passing.
Stars: ✭ 33 (+135.71%)
Mutual labels:  tree
beehive
A flexible, modern, header-only implementation of behavior trees
Stars: ✭ 37 (+164.29%)
Mutual labels:  tree
jquery-tree
jQuery-tree is a jQuery plugin to make an HTML unorder list (ul) in a tree.
Stars: ✭ 29 (+107.14%)
Mutual labels:  tree

un-flatten-tree

npm version Build Status Coverage Status Dependency Status devDependency Status typings included npm

TestingBot Test Status

A small module for converting trees to lists and vice versa. Can be used in browser and Node.

Installation

$ npm i un-flatten-tree

Usage

flatten

Converts tree to list.

var uft = require('un-flatten-tree');

var tree = [
    {name: 'A', items: [
        {name: 'B'},
        {name: 'C'}
    ]},
    {name: 'D', items: [
        {name: 'E', items: []}
    ]}
];

var list = uft.flatten(
    tree,
    node => node.items, // obtain child nodes
    node => node.name   // create output node
);

list should be ['A', 'B', 'C', 'D', 'E']

unflatten

Converts list to tree.

var uft = require('un-flatten-tree');

var list = [
    {id: 1, pid: null},
    {id: 2, pid: null},
    {id: 3, pid: 2},
    {id: 4, pid: 3},
    {id: 5, pid: 4}
];

var tree = uft.unflatten(
    list,
    (node, parentNode) => node.pid === parentNode.id,  // check if node is a child of parentNode
    (node, parentNode) => parentNode.items.push(node), // add node to parentNode
    node => ({id: node.id, items: []})                 // create output node
);

tree should be

[
    {id: 1, items: []}, 
    {id: 2, items: [
        {id: 3, items: [
            {id: 4, items: [
                {id: 5, items: []}
            ]}
        ]}
    ]}
]

More complex examples of usage can be found in tests folder.

Typescript

This module also contains type declarations.

import * as uft from 'un-flatten-tree';

// or

import { unflatten, flatten } from 'un-flatten-tree';
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].