{"id":24539995,"url":"https://github.com/codeandcats/compare-lists","last_synced_at":"2025-08-18T20:35:34.669Z","repository":{"id":32846918,"uuid":"144303126","full_name":"codeandcats/compare-lists","owner":"codeandcats","description":"Super efficiently compares two sorted lists (arrays, strings, anything that is iterable actually).","archived":false,"fork":false,"pushed_at":"2023-01-04T21:37:26.000Z","size":1866,"stargazers_count":4,"open_issues_count":24,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-06T15:14:55.412Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/codeandcats.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2018-08-10T15:26:18.000Z","updated_at":"2023-05-28T15:28:33.000Z","dependencies_parsed_at":"2023-01-14T22:25:17.751Z","dependency_job_id":null,"html_url":"https://github.com/codeandcats/compare-lists","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeandcats%2Fcompare-lists","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeandcats%2Fcompare-lists/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeandcats%2Fcompare-lists/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeandcats%2Fcompare-lists/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codeandcats","download_url":"https://codeload.github.com/codeandcats/compare-lists/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248593759,"owners_count":21130312,"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":"2025-01-22T17:17:39.317Z","updated_at":"2025-04-15T06:28:17.256Z","avatar_url":"https://github.com/codeandcats.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## compare-lists\n\nSuper efficiently compares two sorted lists (arrays, strings, anything that is iterable actually).\n\n\u003ccenter\u003e\n\t\u003cimg src=\"https://raw.githubusercontent.com/codeandcats/compare-lists/master/img/visualisation.png\" /\u003e\n\u003c/center\u003e\n\n[![npm version](https://badge.fury.io/js/compare-lists.svg)](https://badge.fury.io/js/compare-lists)\n[![Build Status](https://travis-ci.org/codeandcats/compare-lists.svg?branch=master)](https://travis-ci.org/codeandcats/compare-lists)\n[![Coverage Status](https://coveralls.io/repos/github/codeandcats/compare-lists/badge.svg?branch=master)](https://coveralls.io/github/codeandcats/compare-lists?branch=master)\n\n### Install\n\n```sh\nnpm install compare-lists --save\n```\n\n### Usage\n\n```typescript\nimport { compareLists } from \"compare-lists\";\n```\n\nSay you have two sorted lists of filenames and you need to know which files exist in the left list, which exist in the right list, and which exist in both lists.\n\n```typescript\nconst leftList = [\n  \"documents/apples.txt\",\n  \"documents/funny-cats.mp4\",\n  \"documents/funny-dogs.avi\",\n  \"trash/linux.iso\",\n  \"trash/zebras.doc\"\n];\n\nconst rightList = [\n  \"documents/apples.txt\",\n  \"documents/funny-cats.mp4\",\n  \"documents/taxes.doc\",\n  \"trash/linux.iso\",\n  \"trash/zebras.doc\"\n];\n```\n\nJust call `compareLists` passing both the lists, a function to compare items in each list and handlers for any events that you're interested in.\n\n```typescript\ncompareLists({\n  left: leftList,\n  right: rightList,\n  compare: (left, right) =\u003e left.localeCompare(right),\n\n  onMatch: value =\u003e console.log(`\"${value}\" is found in both lists`),\n  onMissingInLeft: right =\u003e\n    console.log(`\"${right}\" is missing in the left list`),\n  onMissingInRight: left =\u003e\n    console.log(`\"${left}\" is missing in the right list`)\n});\n```\n\nThe output will be:\n\n```\n\"documents/apples.txt\" is found in both lists\n\"documents/funny-cats.mp4\" is found in both lists\n\"documents/funny-dogs.avi\" is missing in the right list\n\"documents/taxes.doc\" is missing in the left list\n\"trash/linux.iso\" is found in both lists\n\"trash/zebras.doc\" is found in both lists\n```\n\nThat's the basics!\n\n### More...\n\n#### Simple API\n\nAlternatively, you can ask for a \"report\" instead of handling events. Just keep in mind this will use more memory than simply handling events.\n\n```typescript\nconst report = compareLists({\n  left: leftList,\n  right: rightList,\n  compare: (left, right) =\u003e left.localeCompare(right),\n\n  returnReport: true\n});\n\nconsole.log(JSON.stringify(report, null, \"  \"));\n```\n\n```json\n{\n  \"missingInLeft\": [\"documents/taxes.doc\"],\n  \"missingInRight\": [\"documents/funny-dogs.avi\"],\n  \"matches\": [\n    [\"documents/apples.txt\", \"documents/apples.txt\"],\n    [\"documents/funny-cats.mp4\", \"documents/funny-cats.mp4\"],\n    [\"trash/linux.iso\", \"trash/linux.iso\"],\n    [\"trash/zebras.doc\", \"trash/zebras.doc\"]\n  ]\n}\n```\n\n#### The two lists can be different types\n\nThe left and right lists do not need to be the same type as each other. Just remember the items still need to by sorted by the field you are comparing on.\n\n```typescript\nconst leftList = [{ name: \"Morty Smith\" }, { name: \"Rick Sanchez\" }];\nconst rightList = [\"Morty Smith\", \"Mr. Poopy Butthole\"];\n\nconst report = compareLists({\n  left: leftList,\n  right: rightList,\n  compare: (left, right) =\u003e left.name.localeCompare(right),\n  returnReport: true\n});\n\nconsole.log(JSON.stringify(report, null, \"  \"));\n```\n\nWill output:\n\n```json\n{\n  \"missingInLeft\": [\"Mr. Poopy Butthole\"],\n  \"missingInRight\": [{ \"name\": \"Rick Sanchez\" }],\n  \"matches\": [[{ \"name\": \"Morty Smith\" }, \"Morty Smith\"]]\n}\n```\n\n#### Comparing lists other than arrays\n\nThe library works with any objects that implement the iterable protocol, including arrays, strings, maps, etc.\n\nHere is an example of comparing characters in two strings:\n\n```typescript\nconst left = \"abcdef\";\nconst right = \"abcxyz\";\n\nconst report = compareLists({\n  left,\n  right,\n  compare: (left, right) =\u003e left.localeCompare(right)\n});\n\nconsole.log(JSON.stringify(report, null, \"  \"));\n```\n\nWill output:\n\n```json\n{\n  \"missingInLeft\": [\"x\", \"y\", \"z\"],\n  \"missingInRight\": [\"d\", \"e\", \"f\"],\n  \"matches\": [\n    [\"a\", \"a\"],\n    [\"b\", \"b\"],\n    [\"c\", \"c\"]\n  ]\n}\n```\n\n#### Comparing two iterators\n\nThere is also a handy `compareIterators` function if you need it.\nDon't forget the values need to be in ascending order!\n\n```typescript\nimport { compareIterators } from \"compare-lists\";\n\nconst leftIterator = function*() {\n  yield \"a\";\n  yield \"b\";\n  yield \"c\";\n};\n\nconst rightIterator = function*() {\n  yield \"a\";\n  yield \"c\";\n  yield \"d\";\n};\n\nconst report = compareIterators({\n  left: leftIterator(),\n  right: rightIterator(),\n  compare: (left, right) =\u003e left.localeCompare(right),\n  returnReport: true\n});\n\nconsole.log(JSON.stringify(report, null, \"  \"));\n```\n\nWill output:\n\n```json\n{\n  \"missingInLeft\": [\"d\"],\n  \"missingInRight\": [\"b\"],\n  \"matches\": [\n    [\"a\", \"a\"],\n    [\"c\", \"c\"]\n  ]\n}\n```\n\n## Contributing\n\nGot an issue or a feature request? [Log it](https://github.com/codeandcats/compare-lists/issues).\n\n[Pull-requests](https://github.com/codeandcats/compare-lists/pulls) are also welcome. 😸\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeandcats%2Fcompare-lists","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeandcats%2Fcompare-lists","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeandcats%2Fcompare-lists/lists"}