{"id":23483470,"url":"https://github.com/khtdr/treeify-paths","last_synced_at":"2025-06-30T20:36:05.439Z","repository":{"id":57379673,"uuid":"81502291","full_name":"khtdr/treeify-paths","owner":"khtdr","description":"turns a list of file names into a directory tree-like structure","archived":false,"fork":false,"pushed_at":"2023-08-08T18:14:54.000Z","size":73,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-16T08:11:33.245Z","etag":null,"topics":["filenames","nodejs","tree-structure","utilities"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/khtdr.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"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":"2017-02-09T22:43:07.000Z","updated_at":"2023-07-18T09:07:44.000Z","dependencies_parsed_at":"2024-06-21T17:52:55.085Z","dependency_job_id":null,"html_url":"https://github.com/khtdr/treeify-paths","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/khtdr/treeify-paths","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khtdr%2Ftreeify-paths","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khtdr%2Ftreeify-paths/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khtdr%2Ftreeify-paths/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khtdr%2Ftreeify-paths/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/khtdr","download_url":"https://codeload.github.com/khtdr/treeify-paths/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khtdr%2Ftreeify-paths/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260498449,"owners_count":23018265,"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":["filenames","nodejs","tree-structure","utilities"],"created_at":"2024-12-24T21:12:04.622Z","updated_at":"2025-06-23T00:33:27.965Z","avatar_url":"https://github.com/khtdr.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# treeify-paths\n\n[![NPM version](https://img.shields.io/npm/v/treeify-paths.svg)](https://www.npmjs.com/package/treeify-paths)\n[![npm](https://img.shields.io/npm/dm/treeify-paths.svg)]()\n\nProvide a **list of file names**:\n\n- blog/all.html\n- blog/2036/overflows.html\n\nAnd recieve a **directory-like tree**:\n\n- blog\n  - all.html\n  - 2036\n    - overflows.html\n\n### Use Cases\n\nUseful when converting a list of file names into a nested UL/LI tree. Nice for site maps, etc.\n\n- [Live example](https://runkit.com/khtdr/treeify-paths)\n- [Download example](https://runkit.com/downloads/khtdr/treeify-paths/1.0.0.zip)\n\n## Installation:\n\nInstall it with NPM:\n\n```bash\nnpm install --save treeify-paths\n```\n\n```javascript\nimport treeifyPaths from \"treeify-paths\";\n```\n\nIf you are not using NPM, install the library by downloading the [source file](https://raw.githubusercontent.com/khtdr/treeify-paths/master/treeify-paths.js) and including it in your project:\n\n```bash\ncurl -o treeify-paths.js \"https://raw.githubusercontent.com/khtdr/treeify-paths/master/treeify-paths.js\"\n```\n\n```javascript\nlet treeify_paths = require(\"./treeify-paths\").default;\n```\n\n## Usage:\n\nThis module provides a function `treeifyPaths` that takes a list of file names and returns a directory-like tree.\n\n### the following script:\n\n```javascript\nimport treeifyPaths from 'treeify-paths';\nconsole.log(JSON.stringify(treeifyPaths([\n  'about.html',\n  'careers',\n  'careers/job-1.html',\n  'careers/job-2.html',\n  'to/some/page.aspx',\n]), null, 3);\n```\n\n### produces the following output:\n\n```json\n{\n  \"path\": \"\",\n  \"name\": \"\",\n  \"children\": [\n    {\n      \"path\": \"about.html\",\n      \"name\": \"about.html\",\n      \"children\": []\n    },\n    {\n      \"path\": \"careers\",\n      \"name\": \"careers\",\n      \"children\": [\n        {\n          \"path\": \"careers/job-1.html\",\n          \"name\": \"job-1.html\",\n          \"children\": []\n        },\n        {\n          \"path\": \"careers/job-2.html\",\n          \"name\": \"job-2.html\",\n          \"children\": []\n        }\n      ]\n    },\n    {\n      \"path\": \"to\",\n      \"name\": \"\",\n      \"children\": [\n        {\n          \"path\": \"to/some\",\n          \"name\": \"\",\n          \"children\": [\n            {\n              \"path\": \"to/some/page.aspx\",\n              \"name\": \"page.aspx\",\n              \"children\": []\n            }\n          ]\n        }\n      ]\n    }\n  ]\n}\n```\n\n### Controlling the behavior\n\nYou may pass in a list of `path`/`context` pairs. The context is available in the result.\n\n```javascript\nimport treeifyPaths from 'treeify-paths';\nconsole.log(JSON.stringify(treeifyPaths([\n  ['about.html', {admin:false}],\n  ['careers', {admin:false}],\n  ['careers/job-1.html', {admin:false}],\n  ['careers/job-2.html', {admin:false}],\n  ['to/some/page.aspx', {admin:true}],\n]), null, 3);\n```\n\nThe context is available on the result:\n\n```json\n{\n  \"path\": \"about.html\",\n  \"name\": \"about.html\",\n  \"children\": [],\n  \"ctx\": { \"admin\": false }\n}\n```\n\nBy default:\n\n- sorting is `case-sensitive`\n- directories and files are treated the same\n\nThere are options for overridding this:\n\n```javascript\nimport treeifyPaths from \"treeify-paths\";\ntreeifyPaths([], {\n  caseInsensitive: true, // default false\n  directoriesFirst: true, // default false\n  directoriesLast: true, // default false\n});\n```\n\n## Testing\n\nThe mocha [tests have many examples](./tests.js)\n\n```bash\n\u003e npm run test\n\n  treeifyPaths([...arguments])\n    arguments: none\n      ✔ should return an empty object\n    arguments: empty list\n      ✔ should return an empty object\n    arguments: list with a single file\n      ✔ should return a single file\n      ✔ should ignore leading slashes\n      ✔ should return with nested children\n    arguments: multiple file names\n      ✔ should return with nested children\n      ✔ should ignore perimeter slashes and empty or redundant entries\n      ✔ should alphabetize\n      ✔ should alphabetize case-sensitve\n      ✔ should not respect directories\n      ✔ should ignore duplicates\n      ✔ should be able to target directories\n\n  12 passing (7ms)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhtdr%2Ftreeify-paths","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhtdr%2Ftreeify-paths","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhtdr%2Ftreeify-paths/lists"}