{"id":17736504,"url":"https://github.com/zibanpirate/l2t","last_synced_at":"2025-04-01T16:30:51.598Z","repository":{"id":98806559,"uuid":"287514812","full_name":"ZibanPirate/l2t","owner":"ZibanPirate","description":"Elegantly Convert List into Tree","archived":false,"fork":false,"pushed_at":"2020-10-03T10:03:51.000Z","size":137,"stargazers_count":12,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-05T19:34:50.654Z","etag":null,"topics":["commonjs","es6","javascript","library","list","npm","package","tree","types","typescript","yarn"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/l2t","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/ZibanPirate.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-14T11:13:47.000Z","updated_at":"2023-02-05T11:28:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"c15cde23-bdeb-436a-bee4-83ee05742455","html_url":"https://github.com/ZibanPirate/l2t","commit_stats":{"total_commits":40,"total_committers":4,"mean_commits":10.0,"dds":0.475,"last_synced_commit":"fb72dd6174bb92947a769e0e23e8773e5d20bd8d"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZibanPirate%2Fl2t","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZibanPirate%2Fl2t/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZibanPirate%2Fl2t/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZibanPirate%2Fl2t/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ZibanPirate","download_url":"https://codeload.github.com/ZibanPirate/l2t/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246620355,"owners_count":20806756,"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":["commonjs","es6","javascript","library","list","npm","package","tree","types","typescript","yarn"],"created_at":"2024-10-26T00:23:08.812Z","updated_at":"2025-04-01T16:30:51.297Z","avatar_url":"https://github.com/ZibanPirate.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# l2t - Convert List into Tree\n\nElegantly Convert List into Tree\n\n[![](https://img.shields.io/npm/v/l2t)](https://www.npmjs.com/package/l2t)\n[![](https://img.shields.io/npm/dm/l2t)](https://www.npmjs.com/package/l2t)\n\n[![Tests](https://github.com/ZibanPirate/l2t/workflows/Tests/badge.svg?branch=master)](https://github.com/ZibanPirate/l2t/actions?query=workflow%3ATests)\n[![codecov](https://codecov.io/gh/ZibanPirate/l2t/branch/master/graph/badge.svg)](https://codecov.io/gh/ZibanPirate/l2t)\n\n## Table of Content\n\n- [l2t - Convert List into Tree](#l2t---convert-list-into-tree)\n  - [Table of Content](#table-of-content)\n  - [Get Started](#get-started)\n    - [Install the package](#install-the-package)\n    - [Use with Javascript](#use-with-javascript)\n    - [Use with Typescript (rich typing support)](#use-with-typescript-rich-typing-support)\n  - [Contributing](#contributing)\n    - [Perquisites](#perquisites)\n    - [Clone the repo](#clone-the-repo)\n    - [Install dependencies](#install-dependencies)\n    - [Run test suite](#run-test-suite)\n  - [License](#license)\n\n## Get Started\n\n### Install the package\n\n- from NPM\n\n```sh\nnpm install l2t\n```\n\n- or from Yarn\n\n```sh\nyarn add l2t\n```\n\n### Use with Javascript\n\n```js\nimport listToTree from \"lt2\"; // with ES6 syntax:\n// const { listToTree } = require(\"lt2\"); // or with CommonJS syntax:\n\n// a simple list of 6 items, three of which are children\nconst simpleList = [\n  { id: \"1\", label: \"1\" },\n  { id: \"2\", label: \"2\", parentId: \"1\" },\n  { id: \"3\", label: \"3\", parentId: \"1\" },\n  { id: \"4\", label: \"4\" },\n  { id: \"5\", label: \"5\" },\n  { id: \"6\", label: \"6\", parentId: \"5\" },\n];\n\n// convert the simple list into a tree\nconst tree = listToTree(\n  // the list\n  simpleList,\n  // a function returns id of the item\n  // or a string name of the id field e.g. 'id'\n  (item) =\u003e item.id,\n  // a function returns id of the item's parent\n  // or a string name of the parent id field e.g. 'parentId'\n  (item) =\u003e item.parentId,\n  // key for storing children items if there are any\n  \"children\",\n  // a mapper function to map the item to whatever object you like\n  (item) =\u003e {\n    return {\n      label: `item-number-${item.label}`, // let's modify the label and prepend \"item-number-\"\n      index: item.id, // let's change id to index\n    };\n  },\n);\n\n// output the result\nconsole.log(JSON.stringify(tree, null, 2));\n```\n\nthe result:\n\n```json\n[\n  {\n    \"label\": \"item-number-1\",\n    \"index\": \"1\",\n    \"children\": [\n      {\n        \"label\": \"item-number-2\",\n        \"index\": \"2\",\n        \"children\": []\n      },\n      {\n        \"label\": \"item-number-3\",\n        \"index\": \"3\",\n        \"children\": []\n      }\n    ]\n  },\n  {\n    \"label\": \"item-number-4\",\n    \"index\": \"4\",\n    \"children\": []\n  },\n  {\n    \"label\": \"item-number-5\",\n    \"index\": \"5\",\n    \"children\": [\n      {\n        \"label\": \"item-number-6\",\n        \"index\": \"6\",\n        \"children\": []\n      }\n    ]\n  }\n]\n```\n\n### Use with Typescript (rich typing support)\n\n```ts\nimport { listToTree } from \"lt2\";\n// import listToTree from \"lt2\"; // or with default import:\n\n// List Item interface\ninterface SimpleItem {\n  id: string;\n  label: string;\n  parentId?: string;\n}\n// Tree Node interface\ninterface SimpleNode {\n  index: string;\n  label: string;\n  children: SimpleNode[];\n}\n\n// a simple list of 6 items, three of which are children\nconst simpleList: SimpleItem[] = [\n  { id: \"1\", label: \"1\" },\n  { id: \"2\", label: \"2\", parentId: \"1\" },\n  { id: \"3\", label: \"3\", parentId: \"1\" },\n  { id: \"4\", label: \"4\" },\n  { id: \"5\", label: \"5\" },\n  { id: \"6\", label: \"6\", parentId: \"5\" },\n];\n\n// convert the simple list into a tree\nconst tree = listToTree\u003cSimpleItem, SimpleNode\u003e(\n  simpleList,\n  // a function returns id of the item\n  // or a string name of the id field e.g. 'id'\n  (item) =\u003e item.id,\n  // a function returns id of the item's parent\n  // or a string name of the parent id field e.g. 'parentId'\n  (item) =\u003e item.parentId,\n  // key for storing children items if there are any, note that when using typescript,\n  // this key has to be one of the keys in the node interface (SimpleNode in this case)\n  \"children\",\n  // a mapper function to map the item to whatever object you like\n  (item) =\u003e {\n    return {\n      label: `item-number-${item.label}`, // let's modify the label and prepend \"item-number-\"\n      index: item.id, // let's change id to index\n    };\n  },\n);\n```\n\nthis will give you the same result as the Javascript version.\n\n## Contributing\n\nTo get started see [the contributing guidelines](https://github.com/ZibanPirate/l2t/blob/master/.github/CONTRIBUTING.md).\n\n**Unit test** :\nUnit test are written in [Jest](https://jestjs.io/). Please add/edit unit test(s) for every new feature or bug fix. `yarn test` to run the test suite.\n\n### Perquisites\n\nMake sure you have:\n\n- [git](https://git-scm.com/)\n- [nodejs](https://nodejs.org/) 10 or higher\n- [yarn](https://yarnpkg.com/)\n\n### Clone the repo\n\n```sh\ngit clone https://github.com/ZibanPirate/l2t.git\n```\n\n### Install dependencies\n\n```sh\nyarn\n```\n\n### Run test suite\n\n- Run once\n\n```sh\nyarn test\n```\n\n- Run in watch mode\n\n```sh\nyarn test --watch\n```\n\n- Run in watch mode with coverage report\n\n```sh\nyarn test --watch --coverage\n```\n\n## License\n\nCopyright (c) 2020 ZibanPirate (twitter: [@ZibanPirate](https://twitter.com/zibanpirate)) Licensed under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzibanpirate%2Fl2t","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzibanpirate%2Fl2t","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzibanpirate%2Fl2t/lists"}