项目作者: riqra

项目描述 :
Nested hierarchies
高级语言: JavaScript
项目地址: git://github.com/riqra/hierarchy-parser.git
创建时间: 2017-12-20T16:51:21Z
项目社区:https://github.com/riqra/hierarchy-parser

开源协议:

下载





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

  1. [
  2. {
  3. "id": 1,
  4. "name": "Home",
  5. "parentId": null
  6. },
  7. {
  8. "id": 2,
  9. "name": "Tech",
  10. "parentId": null
  11. },
  12. {
  13. "id": 3,
  14. "name": "Decor",
  15. "parentId": 1
  16. },
  17. {
  18. "id": 4,
  19. "name": "Bath",
  20. "parentId": 1
  21. },
  22. {
  23. "id": 5,
  24. "name": "Games",
  25. "parentId": 2
  26. },
  27. {
  28. "id": 6,
  29. "name": "Frames",
  30. "parentId": 3
  31. }
  32. ]

this data will become

  1. [
  2. {
  3. "id": 1,
  4. "name": "Home",
  5. "children": [
  6. {
  7. "id": 3,
  8. "name": "Decor",
  9. "children": [
  10. {
  11. "id": 6,
  12. "name": "Frames"
  13. }
  14. ]
  15. },
  16. {
  17. "id": 4,
  18. "name": "Bath"
  19. }
  20. ]
  21. },
  22. {
  23. "id": 2,
  24. "name": "Tech",
  25. "children": [
  26. {
  27. "id": 5,
  28. "name": "Games"
  29. }
  30. ]
  31. }
  32. ]

see tests for more examples