{"id":27958895,"url":"https://github.com/janmalch/comparing","last_synced_at":"2025-05-07T18:26:49.802Z","repository":{"id":35094426,"uuid":"205244503","full_name":"JanMalch/comparing","owner":"JanMalch","description":"Easily create descriptive comparators.","archived":false,"fork":false,"pushed_at":"2024-09-27T12:45:32.000Z","size":2832,"stargazers_count":4,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-27T04:48:28.243Z","etag":null,"topics":["comparators","comparing","javascript","library","typescript"],"latest_commit_sha":null,"homepage":"https://janmalch.github.io/comparing/","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/JanMalch.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2019-08-29T20:22:57.000Z","updated_at":"2024-06-16T10:17:48.000Z","dependencies_parsed_at":"2023-01-15T13:45:22.310Z","dependency_job_id":"c389e933-1d7b-49c3-91fa-72a1af993368","html_url":"https://github.com/JanMalch/comparing","commit_stats":{"total_commits":104,"total_committers":5,"mean_commits":20.8,"dds":"0.41346153846153844","last_synced_commit":"a19e29aa58eea858fd046ecb90496347c329dc45"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JanMalch%2Fcomparing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JanMalch%2Fcomparing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JanMalch%2Fcomparing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JanMalch%2Fcomparing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JanMalch","download_url":"https://codeload.github.com/JanMalch/comparing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252933065,"owners_count":21827441,"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":["comparators","comparing","javascript","library","typescript"],"created_at":"2025-05-07T18:26:47.131Z","updated_at":"2025-05-07T18:26:49.768Z","avatar_url":"https://github.com/JanMalch.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# comparing \u003ca href=\"https://www.github.com/JanMalch/comparing\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/JanMalch/comparing/master/.github/assets/logo.png\" width=\"90\" height=\"90\" align=\"right\"\u003e\u003c/a\u003e\n\n[![npm](https://img.shields.io/npm/v/comparing)][npm-url]\n[![documentation](https://img.shields.io/badge/docs-available-success)][docs-url]\n[![minified + gzip](https://badgen.net/bundlephobia/minzip/comparing)][bundlephobia-url]\n[![Build](https://github.com/JanMalch/comparing/workflows/Build/badge.svg)][build-url]\n[![codecov](https://codecov.io/gh/JanMalch/comparing/branch/master/graph/badge.svg)][codecov-url]\n\n\u003ci\u003eEasily create descriptive comparators.\u003c/i\u003e\n\n## Features\n\n- predefined Comparators for common use-cases\n- lightweight\n- easily sort arrays of objects via [`compareBy`](https://janmalch.github.io/comparing/modules.html#compareBy)\n- [compose](https://janmalch.github.io/comparing/modules.html#composeComparators) or [reverse](https://janmalch.github.io/comparing/modules.html#reverseComparator) predefined and custom Comparators\n- define custom [orders for enums](https://janmalch.github.io/comparing/modules.html#comparatorForOrder) or other arbitrary value sets\n- create comparators that [support type unions](https://janmalch.github.io/comparing/modules.html#comparatorWithPredicate)\n\nMake sure to check out the [documentation][docs-url].\n\n## Installation\n\n```bash\nnpm i comparing\n```\n\nImport all comparators, factories and types `from 'comparing'`.\n\n## Examples\n\nAll comparators are fully tested, so you can find more examples in the [unit tests][test-files-url].\nA lot of comparators also have examples in their [documentation][docs-url].\n\n### Basic usage\n\n```typescript\nimport { naturalOrder } from 'comparing';\n\nconst actual = [4, 1, 3, 5].sort(naturalOrder);\nexpect(actual).toEqual([1, 3, 4, 5]);\n```\n\n```typescript\nconst comparator: Comparator\u003cstring | null\u003e = composeComparators([nullishFirst, localeCompare]);\nconst actual = ['A', 'a', 'b', null, 'B'].sort(comparator);\nexpect(actual).toEqual([null, 'a', 'A', 'b', 'B']);\n\nconst reversedComparator = reverseComparator(comparator);\nconst actualReversed = ['A', 'a', 'b', null, 'B'].sort(reversedComparator);\nexpect(actual).toEqual(['B', 'b', 'A', 'a', null]);\n```\n\n### Comparing objects\n\nThe following example shows how to sort an array of objects, which have an optional `order` property that should take precedence when sorting.\nIf equal or not present, then the objects should be sorted by their `name` property.\n\n```typescript\nconst nameComparator: Comparator\u003c{ name: string }\u003e = compareBy((x) =\u003e x.name, ignoreCase);\nconst orderComparator = compareBy((x) =\u003e x.order, composeComparators([nullishLast, naturalOrder]));\nconst myObjectComparator = composeComparators([orderComparator, nameComparator]);\n\nconst actual = [\n  { name: 'B', order: 1 },\n  { name: 'F' },\n  { name: 'C', order: 3 },\n  { name: 'A', order: 1 },\n  { name: 'D' },\n  { name: 'E', order: 2 },\n].sort(myObjectComparator);\n\nexpect(actual).toEqual([\n  { name: 'A', order: 1 },\n  { name: 'B', order: 1 },\n  { name: 'E', order: 2 },\n  { name: 'C', order: 3 },\n  { name: 'D' },\n  { name: 'F' },\n]);\n```\n\n### Usage with other libraries\n\nThe library has no dependencies, thus only creates and\nworks with the JavaScript objects and functions you already know.\n\n**Compare with dot path**\n\n```typescript\nimport { compareBy } from 'comparing';\nimport get from 'lodash/fp/get';\n\nconst abcComparator = compareBy(get('a.b.c'));\n\nconst data = [\n  { a: { b: { c: 1 } } },\n  { a: { b: { c: 0 } } },\n  { a: { b: { c: 2 } } },\n].sort(abcComparator);\n\nexpect(data).toEqual([\n  { a: { b: { c: 0 } } },\n  { a: { b: { c: 1 } } },\n  { a: { b: { c: 2 } } },\n]);\n```\n\n**Sort dependencies**\n\n```typescript\nimport {\n  compareBy,\n  comparatorForOrder,\n  composeComparators,\n  Comparator,\n} from 'comparing';\nimport toposort from 'toposort';\n\nconst graph = [\n  // must first put on the shirt before the jacket, and so on ...\n  ['put on your shirt', 'put on your jacket'],\n  ['put on your shorts', 'put on your jacket'],\n  ['put on your shorts', 'put on your shoes'],\n];\n\nconst taskComparator = comparatorForOrder(toposort(graph));\nconst comparator: Comparator\u003c{ day: number; task: string }\u003e = composeComparators([\n  compareBy((x) =\u003e x.day), // naturalOrder implicitly\n  compareBy((x) =\u003e x.task, taskComparator),\n]);\n\nconst todoList = [\n  { day: 0, task: 'put on your jacket' },\n  { day: 0, task: 'put on your shirt' },\n  { day: 1, task: 'put on your shoes' },\n  { day: 2, task: 'put on your shirt' },\n  { day: 1, task: 'put on your jacket' },\n  { day: 1, task: 'put on your shorts' },\n].sort(comparator);\n\nexpect(todoList).toEqual([\n  { day: 0, task: 'put on your shirt' },\n  { day: 0, task: 'put on your jacket' },\n  { day: 1, task: 'put on your shorts' },\n  { day: 1, task: 'put on your jacket' },\n  { day: 1, task: 'put on your shoes' },\n  { day: 2, task: 'put on your shirt' },\n]);\n```\n\n[docs-url]: https://janmalch.github.io/comparing/\n[npm-url]: https://www.npmjs.com/package/comparing\n[build-url]: https://github.com/JanMalch/comparing/actions?query=workflow%3ABuild\n[codecov-url]: https://codecov.io/gh/JanMalch/comparing\n[bundlephobia-url]: https://bundlephobia.com/result?p=comparing\n[test-files-url]: https://github.com/JanMalch/comparing/tree/master/test\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanmalch%2Fcomparing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjanmalch%2Fcomparing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanmalch%2Fcomparing/lists"}