{"id":22030959,"url":"https://github.com/ts-stack/comments-to-tree","last_synced_at":"2026-05-06T05:36:15.267Z","repository":{"id":48030228,"uuid":"112673099","full_name":"ts-stack/comments-to-tree","owner":"ts-stack","description":"A micro TypeScript / JavaScript utility that converts a one-dimensional array with comments into a comments tree.","archived":false,"fork":false,"pushed_at":"2024-10-15T20:31:28.000Z","size":600,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T11:30:24.094Z","etag":null,"topics":["comments-tree","nested-comments"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/ts-stack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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-11-30T23:44:04.000Z","updated_at":"2024-10-15T20:32:32.000Z","dependencies_parsed_at":"2025-03-23T11:38:10.938Z","dependency_job_id":null,"html_url":"https://github.com/ts-stack/comments-to-tree","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/ts-stack/comments-to-tree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fcomments-to-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fcomments-to-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fcomments-to-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fcomments-to-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ts-stack","download_url":"https://codeload.github.com/ts-stack/comments-to-tree/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fcomments-to-tree/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32680886,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-06T02:33:58.958Z","status":"ssl_error","status_checked_at":"2026-05-06T02:33:39.611Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["comments-tree","nested-comments"],"created_at":"2024-11-30T08:12:41.068Z","updated_at":"2026-05-06T05:36:15.230Z","avatar_url":"https://github.com/ts-stack.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# What is this for?\n\nA micro utility that converts a one-dimensional array with comments into a comments tree.\n\n## API\n\n```ts\n// We need the comments come from a database with such properties, at least.\ninterface DefaultCommentFromDb {\n  commentId: number | string;\n  parentId?: number | string;\n}\n\n// This utility will convert comments from the database to comments with this interface.\ninterface DefaultComment {\n  commentId: number | string;\n  children: this[];\n  parentId?: number | string;\n  parent?: this;\n}\n```\n\nThis utility has one public method:\n\n```ts\nstatic getTree\u003cT extends DefaultCommentFromDb\u003e(\n  allCommentsFromDb: T[],\n  actionRoot: ActionArray = 'unshift',\n  actionChild: ActionArray = 'unshift'\n)\n```\n\nPlease do not be afraid =). It's easy to use this method.\n\n`CommentsToTree.getTree()` - converts a one-dimensional array of comments into a comments tree. The array must be sorted in reverse order - at its beginning there are the most recent comments.\n\n`allCommentsFromDb` - one-dimensional array of comments from the database (the array sorted in reverse order).\n\n`actionRoot` - the action you need to apply to insert a root comment to comments tree. By default `unshift`.\n\n`actionChild` - the action you need to apply to insert a child comment to comments tree. By default `unshift`.\n\n## Usage\n\nCopy `main.ts` from this repository. You need just implement `DefaultCommentFromDb` in your comment class:\n\n```ts\nimport { DefaultCommentFromDb } from './main';\n\nclass MyCommentFromDb implements DefaultCommentFromDb {\n  commentId: number;\n  parentId?: number;\n  someOtherPropertyFromDb?: string;\n}\n\nconst allCommentsFromDb: MyCommentFromDb[] = [\n  { commentId: 5, parentId: 2, someOtherPropertyFromDb: 'comment5' },\n  { commentId: 4, someOtherPropertyFromDb: 'root comment4' },\n  { commentId: 3, parentId: 1, someOtherPropertyFromDb: 'comment3' },\n  { commentId: 2, parentId: 1, someOtherPropertyFromDb: 'comment2' },\n  { commentId: 1, someOtherPropertyFromDb: 'root comment1' },\n];\n\nconst commentsTree = CommentsToTree.getTree\u003cMyCommentFromDb\u003e(allCommentsFromDb);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fts-stack%2Fcomments-to-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fts-stack%2Fcomments-to-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fts-stack%2Fcomments-to-tree/lists"}