Code Monkey home page Code Monkey logo

hierarchy-parser's Introduction

Hierarchy Parser

NPM version CI Size

What's it for?

Relational databases aren't very good at dealing with nested hierarchies.

Examples of hierarchies are:

  • Nested folders where each folder has many subfolders, those subfolders themselves have subfolders, and so on
  • Categories and sub-categories e.g. for a newspaper with sections for different sports, Sports category splits into Track Sports and Water Sports, Water Sports into Swimming and Diving, Diving into High Board, Middle Board and Low Board etc
  • Tree structures

How to use

Name Type Default Description
data Array Data to be parsed
options Object Parser options
options.identifier String "id" Children identifier
options.parentKey String "parentId" Parent identifier
options.initialParentId String Initial level in the hierarchy

Example

[
  {
    "id": 1,
    "name": "Home",
    "parentId": null
  },
  {
    "id": 2,
    "name": "Tech",
    "parentId": null
  },
  {
    "id": 3,
    "name": "Decor",
    "parentId": 1
  },
  {
    "id": 4,
    "name": "Bath",
    "parentId": 1
  },
  {
    "id": 5,
    "name": "Games",
    "parentId": 2
  },
  {
    "id": 6,
    "name": "Frames",
    "parentId": 3
  }
]

this data will become

[
  {
    "id": 1,
    "name": "Home",
    "children": [
      {
        "id": 3,
        "name": "Decor",
        "children": [
          {
            "id": 6,
            "name": "Frames"
          }
        ]
      },
      {
        "id": 4,
        "name": "Bath"
      }
    ]
  },
  {
    "id": 2,
    "name": "Tech",
    "children": [
      {
        "id": 5,
        "name": "Games"
      }
    ]
  }
]

see tests for more examples

hierarchy-parser's People

Contributors

juliocanares avatar rqbazan avatar yonycalsin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

hierarchy-parser's Issues

Add identifier

Sometimes we want to filter by a custom key.

use case:

const data = [
    {
        sku: 'KD910',
        parentId: null
    },
    {
        sku: 'M921LJ',
        parentId: 'KD910'
    }
]

children should be found by sku

Object is not extensible when attempt create children

When one object is setting with Object.preventExtensions() is not posible to see property children in object, you will have to do a deep copy of your array before to data parsed.

data.map((item) => ({
     ...item,
     children
})

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.