{"id":16621738,"url":"https://github.com/peterthehan/group-similar","last_synced_at":"2025-10-29T21:31:47.487Z","repository":{"id":42583417,"uuid":"410237529","full_name":"peterthehan/group-similar","owner":"peterthehan","description":"Group similar items together.","archived":false,"fork":false,"pushed_at":"2023-10-19T11:17:38.000Z","size":1406,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-02T05:04:41.832Z","etag":null,"topics":["algorithm","cluster","compare","comparison","disjoint-set","edit-distance","fuzzy","group","group-similar","grouping","levenshtein","match","matching","merge-find","similar","similarity","string","union-find"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/group-similar","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/peterthehan.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":["peterthehan"],"patreon":"peterthehan","ko_fi":"peterthehan","custom":["https://paypal.me/peterthehan","https://venmo.com/peterthehan"]}},"created_at":"2021-09-25T10:04:31.000Z","updated_at":"2024-08-19T05:12:18.000Z","dependencies_parsed_at":"2023-02-08T06:15:41.596Z","dependency_job_id":null,"html_url":"https://github.com/peterthehan/group-similar","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peterthehan%2Fgroup-similar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peterthehan%2Fgroup-similar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peterthehan%2Fgroup-similar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peterthehan%2Fgroup-similar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peterthehan","download_url":"https://codeload.github.com/peterthehan/group-similar/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238900122,"owners_count":19549453,"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":["algorithm","cluster","compare","comparison","disjoint-set","edit-distance","fuzzy","group","group-similar","grouping","levenshtein","match","matching","merge-find","similar","similarity","string","union-find"],"created_at":"2024-10-12T02:48:17.183Z","updated_at":"2025-10-29T21:31:47.082Z","avatar_url":"https://github.com/peterthehan.png","language":"TypeScript","funding_links":["https://github.com/sponsors/peterthehan","https://patreon.com/peterthehan","https://ko-fi.com/peterthehan","https://paypal.me/peterthehan","https://venmo.com/peterthehan"],"categories":[],"sub_categories":[],"readme":"# Group Similar\n\n[![Discord](https://discord.com/api/guilds/258167954913361930/embed.png)](https://discord.gg/WjEFnzC) [![Twitter Follow](https://img.shields.io/twitter/follow/peterthehan.svg?style=social)](https://twitter.com/peterthehan)\n\nGroup similar items together.\n\nRuntime complexity is `O(N^2 * (M + α(N)))`, where `N` is the number of elements in `items`, `M` is the runtime complexity of the `similarityFunction`, and `α(N)` is the [inverse Ackermann function](https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Time_complexity) (amortized constant time for all practical purposes).\n\nSpace complexity is `O(N)`.\n\n## Getting started\n\n```\nnpm i group-similar\n```\n\n## Examples\n\n\u003cdetails open\u003e\n\n\u003csummary\u003eGroup similar strings\u003c/summary\u003e\n\n```ts\nimport { groupSimilar } from \"group-similar\";\nimport { distance } from \"fastest-levenshtein\";\n\nfunction levenshteinSimilarityFunction(a: string, b: string): number {\n  return a.length === 0 \u0026\u0026 b.length === 0\n    ? 1\n    : 1 - distance(a, b) / Math.max(a.length, b.length);\n}\n\ngroupSimilar({\n  items: [\"cat\", \"bat\", \"kitten\", \"dog\", \"sitting\"],\n  mapper: (i) =\u003e i,\n  similarityFunction: levenshteinSimilarityFunction,\n  similarityThreshold: 0.5,\n});\n\n// [ [ 'cat', 'bat' ], [ 'kitten', 'sitting' ], [ 'dog' ] ]\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\n\u003csummary\u003eGroup similar numbers\u003c/summary\u003e\n\n```ts\nimport { groupSimilar } from \"group-similar\";\n\nfunction evenOddSimilarityFunction(a: number, b: number): number {\n  return Number(a % 2 === b % 2);\n}\n\ngroupSimilar({\n  items: [1, 5, 10, 0, 2, 123],\n  mapper: (i) =\u003e i,\n  similarityFunction: evenOddSimilarityFunction,\n  similarityThreshold: 1,\n});\n\n// [ [ 1, 5, 123 ], [ 10, 0, 2 ] ]\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\n\u003csummary\u003eGroup similar objects\u003c/summary\u003e\n\n```ts\nimport { groupSimilar } from \"group-similar\";\nimport { distance } from \"fastest-levenshtein\";\n\nfunction nestedMapper(object: { a: { b: { value: string } } }): string {\n  return object.a.b.value;\n}\n\nfunction levenshteinSimilarityFunction(a: string, b: string): number {\n  return a.length === 0 \u0026\u0026 b.length === 0\n    ? 1\n    : 1 - distance(a, b) / Math.max(a.length, b.length);\n}\n\ngroupSimilar({\n  items: [\n    { a: { b: { value: \"sitting\" } } },\n    { a: { b: { value: \"dog\" } } },\n    { a: { b: { value: \"kitten\" } } },\n    { a: { b: { value: \"bat\" } } },\n    { a: { b: { value: \"cat\" } } },\n  ],\n  mapper: nestedMapper,\n  similarityFunction: levenshteinSimilarityFunction,\n  similarityThreshold: 0.5,\n});\n\n// [\n//   [{ a: { b: { value: \"sitting\" } } }, { a: { b: { value: \"kitten\" } } }],\n//   [{ a: { b: { value: \"dog\" } } }],\n//   [{ a: { b: { value: \"bat\" } } }, { a: { b: { value: \"cat\" } } }],\n// ]\n```\n\n\u003c/details\u003e\n\n## Syntax\n\n```ts\ngroupSimilar(options);\ngroupSimilar({ items, mapper, similarityFunction, similarityThreshold });\n```\n\n### Parameters\n\n| Parameter           | Type     | Required | Default | Description                       |\n| ------------------- | -------- | -------- | ------- | --------------------------------- |\n| [options](#options) | `Object` | Yes      | _none_  | Arguments to pass to the function |\n\n#### Options\n\n| Property            | Type                     | Required | Default | Description                                                                                         |\n| ------------------- | ------------------------ | -------- | ------- | --------------------------------------------------------------------------------------------------- |\n| items               | `T[]`                    | Yes      | _none_  | Array of items to group                                                                             |\n| mapper              | `(t: T) =\u003e K`            | Yes      | _none_  | Function to apply to each element in items prior to measuring similarity                            |\n| similarityFunction  | `(a: K, b: K) =\u003e number` | Yes      | _none_  | Function to measure similarity between mapped items                                                 |\n| similarityThreshold | `number`                 | Yes      | _none_  | Threshold at which items whose similarity value is greater than or equal to it are grouped together |\n\n### Return value\n\nThe **return value** is a new nested array of type `T[][]` containing elements of `items` grouped by similarity. If there are no elements in `items`, an empty array will be returned.\n\n## Benchmark\n\nBenchmark test results where `N` is the number of items being grouped, higher `ops/sec` is better.\n\n| Library                                                        | N=16  | N=32  | N=64 | N=128 | N=256 | N=512 | N=1024 | N=2048 |\n| -------------------------------------------------------------- | ----- | ----- | ---- | ----- | ----- | ----- | ------ | ------ |\n| [group-similar](https://www.npmjs.com/package/group-similar)   | 86867 | 17538 | 6067 | 1594  | 444   | 171   | 75     | 27     |\n| [set-clustering](https://www.npmjs.com/package/set-clustering) | 28506 | 6258  | 1831 | 455   | 121   | 30    | 6      | 1      |\n\nBenchmark configuration details can be found [here](./test/benchmark.ts).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeterthehan%2Fgroup-similar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeterthehan%2Fgroup-similar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeterthehan%2Fgroup-similar/lists"}