2.1.1 • Published 3 years ago

@riqra/hierarchy-parser v2.1.1

Weekly downloads
107
License
MIT
Repository
github
Last release
3 years ago

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

NameTypeDefaultDescription
dataArrayData to be parsed
optionsObjectParser options
options.identifierString"id"Children identifier
options.parentKeyString"parentId"Parent identifier
options.initialParentIdStringInitial 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

2.1.1

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.2.2

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago