GithubHelp home page GithubHelp logo

aleff13 / un-flatten-tree Goto Github PK

View Code? Open in Web Editor NEW

This project forked from iyegoroff/un-flatten-tree

0.0 0.0 0.0 97 KB

A small npm module for converting trees to lists and vice versa.

License: MIT License

TypeScript 100.00%

un-flatten-tree's Introduction

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';

un-flatten-tree's People

Contributors

greenkeeper[bot] avatar iyegoroff avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.