@riqra/hierarchy-parser
TypeScript icon, indicating that this package has built-in type declarations

2.1.1 • Public • Published

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

Package Sidebar

Install

npm i @riqra/hierarchy-parser

Weekly Downloads

71

Version

2.1.1

License

MIT

Unpacked Size

19.2 kB

Total Files

12

Last publish

Collaborators

  • riqra-team
  • juliocanares