{"id":18910046,"url":"https://github.com/cheton/flattree","last_synced_at":"2025-04-15T06:31:05.065Z","repository":{"id":6036994,"uuid":"54539975","full_name":"cheton/flattree","owner":"cheton","description":"Convert hierarchical tree structure to flat structure.","archived":false,"fork":false,"pushed_at":"2021-11-21T14:17:48.000Z","size":74,"stargazers_count":31,"open_issues_count":0,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-30T00:00:13.056Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cheton.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":null,"open_collective":"cheton","ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2016-03-23T07:35:39.000Z","updated_at":"2024-10-24T19:24:47.000Z","dependencies_parsed_at":"2022-08-06T19:01:06.266Z","dependency_job_id":null,"html_url":"https://github.com/cheton/flattree","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheton%2Fflattree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheton%2Fflattree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheton%2Fflattree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheton%2Fflattree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cheton","download_url":"https://codeload.github.com/cheton/flattree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248289960,"owners_count":21078923,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T09:39:09.639Z","updated_at":"2025-04-15T06:31:05.046Z","avatar_url":"https://github.com/cheton.png","language":"JavaScript","readme":"# flattree [![CI](https://github.com/cheton/flattree/actions/workflows/node.js.yml/badge.svg?branch=master)](https://github.com/cheton/flattree/actions/workflows/node.js.yml) [![codecov](https://codecov.io/gh/cheton/flattree/branch/master/graph/badge.svg?token=O3B7N9OQ9L)](https://codecov.io/gh/cheton/flattree)\n\n[![NPM](https://nodei.co/npm/flattree.png?downloads=true\u0026stars=true)](https://www.npmjs.com/package/flattree)\n\nConvert hierarchical tree structure to flat structure.\nWith a flat structure, it allows you to scroll a large tree easily with virtualization. \n\nCheck out [infinite-tree](https://github.com/cheton/infinite-tree) to see how it integrated with [FlatTree](https://github.com/cheton/flattree).\n\n## Installation\n\n```bash\nnpm install --save flattree\n```\n## Examples\n\nGiven a hierarchical tree structure like [this](https://github.com/cheton/flattree/blob/master/test/fixtures/tree.json), you can build a tree in any form. For example:\n\n### Flat List View\nFile: [examples/test1.js](https://github.com/cheton/flattree/blob/master/examples/tree1.js)\n```\n\u003croot\u003e: path=\".0\", parent=\"\", children=2, total=11, depth=0, prefix=\"0\", open=1, lastChild=1\nAlpha: path=\".0.0\", parent=\".0\", children=0, total=0, depth=1, prefix=\"00\", open=0, lastChild=0\nBravo: path=\".0.1\", parent=\".0\", children=3, total=9, depth=1, prefix=\"00\", open=1, lastChild=1\nCharlie: path=\".0.1.0\", parent=\".0.1\", children=2, total=4, depth=2, prefix=\"000\", open=1, lastChild=0\nDelta: path=\".0.1.0.0\", parent=\".0.1.0\", children=2, total=2, depth=3, prefix=\"0001\", open=1, lastChild=0\nEcho: path=\".0.1.0.0.0\", parent=\".0.1.0.0\", children=0, total=0, depth=4, prefix=\"00011\", open=0, lastChild=0\nFoxtrot: path=\".0.1.0.0.1\", parent=\".0.1.0.0\", children=0, total=0, depth=4, prefix=\"00011\", open=0, lastChild=1\nGolf: path=\".0.1.0.1\", parent=\".0.1.0\", children=0, total=0, depth=3, prefix=\"0001\", open=0, lastChild=1\nHotel: path=\".0.1.1\", parent=\".0.1\", children=1, total=2, depth=2, prefix=\"000\", open=1, lastChild=0\nIndia: path=\".0.1.1.0\", parent=\".0.1.1\", children=1, total=1, depth=3, prefix=\"0001\", open=1, lastChild=1\nJuliet: path=\".0.1.1.0.0\", parent=\".0.1.1.0\", children=0, total=0, depth=4, prefix=\"00010\", open=0, lastChild=1\nKilo: path=\".0.1.2\", parent=\".0.1\", children=0, total=0, depth=2, prefix=\"000\", open=0, lastChild=1\n```\n\n### Nested Hierarchy\nFile: [examples/test2.js](https://github.com/cheton/flattree/blob/master/examples/tree2.js)\n```\n\u003croot\u003e (.0)\n  ├── Alpha (.0.0)\n  └─┬ Bravo (.0.1)\n    ├─┬ Charlie (.0.1.0)\n    | ├─┬ Delta (.0.1.0.0)\n    | | ├── Echo (.0.1.0.0.0)\n    | | └── Foxtrot (.0.1.0.0.1)\n    | └── Golf (.0.1.0.1)\n    ├─┬ Hotel (.0.1.1)\n    | └─┬ India (.0.1.1.0)\n    |   └── Juliet (.0.1.1.0.0)\n    └── Kilo (.0.1.2)\n```\n\n### Single Root Node\nFile: [examples/test3.js](https://github.com/cheton/flattree/blob/master/examples/tree3.js)\n```\n- \u003croot\u003e (.0)\n    Alpha (.0.0)\n  - Bravo (.0.1)\n    - Charlie (.0.1.0)\n      + Delta (.0.1.0.0)\n        Golf (.0.1.0.1)\n    - Hotel (.0.1.1)\n      - India (.0.1.1.0)\n          Juliet (.0.1.1.0.0)\n      Kilo (.0.1.2)\n```\n\n### Multiple Root Nodes\nFile: [examples/test4.js](https://github.com/cheton/flattree/blob/master/examples/tree4.js)\n```\n  Alpha (.0)\n- Bravo (.1)\n  - Charlie (.1.0)\n    + Delta (.1.0.0)\n      Golf (.1.0.1)\n  - Hotel (.1.1)\n    - India (.1.1.0)\n        Juliet (.1.1.0.0)\n    Kilo (.1.2)\n```\n\n## Usage\n```js\nvar flatten = require('flattree').flatten;\n\nvar tree = { // tree can either be object or array\n    id: 'fruit',\n    label: 'Fruit',\n    children: [\n        { id: 'apple', label: 'Apple' },\n        { id: 'banana', label: 'Banana', children: [{ id: 'cherry', label: 'Cherry' }] }\n    ]\n};\n\nflatten(tree, {\n    openNodes: ['fruit', 'banana'],\n    openAllNodes: false, // Defaults to false\n    throwOnEerror: false // Defaults to false\n});\n// → [Node { id: 'fruit', ...}, Node { id: 'apple', ...}, Node { id: 'banana', ...}, Node { id: 'cherry', ...}]\n```\n\nThis demostrates how to open a node and rebuild the tree:\n```js\nvar _ = require('lodash');\nvar flatten = require('flattree').flatten;\n\n// Create the list\nvar nodes = flatten(require('./test/fixtures/tree.json'));\n// → [Node { id: 'fruit', ...}]\n\n// Find the first node with an id attribute that equals to 'fruit'\nvar index = _.findIndex(nodes, { 'id': 'fruit' });\nvar node = nodes[index];\n\nvar siblingNodes = flatten(node.children, { openNodes: ['fruit'] });\n\n// Insert an array inside another array\nnodes.splice.apply(nodes, [index + 1, 0].concat(siblingNodes));\n\nconsole.log(nodes);\n// → [Node { id: 'fruit', ...}, Node { id: 'apple', ...}, Node { id: 'banana', ...}]\n```\n\nThis demostrates how to close a node and rebuild the tree:\n```js\nvar _ = require('lodash');\nvar flatten = require('flattree').flatten;\n\n// Create the list\nvar nodes = flatten(require('./test/fixtures/tree.json'), { openAllNodes: true });\n// → [Node { id: 'fruit', ...}, Node { id: 'apple', ...}, Node { id: 'banana', ...}, Node { id: 'cherry', ...}]\n\n// Find the first node with an id attribute that equals to 'banana'\nvar index = _.findIndex(nodes, { 'id': 'banana' });\nvar node = nodes[index];\nvar deleteCount = node.state.total;\n\n// Traversing up through ancestors to subtract node.state.total\nvar p = node;\nwhile (p) {\n    p.state.total = (p.state.total - deleteCount);\n    p = p.parent;\n}\n\n// Remove elements from an array\nnodes.splice(index + 1, deleteCount);\n\nconsole.log(nodes);\n// → [Node { id: 'fruit', ...}, Node { id: 'apple', ...}, Node { id: 'banana', ...}]\n```\n\n## API\nhttps://github.com/cheton/flattree/wiki/API\n\n## License\n\nCopyright (c) 2016 Cheton Wu\n\nLicensed under the [MIT License](LICENSE).\n","funding_links":["https://opencollective.com/cheton"],"categories":["Utilities"],"sub_categories":["React Components"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheton%2Fflattree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcheton%2Fflattree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheton%2Fflattree/lists"}