{"id":23501872,"url":"https://github.com/frederikgoovaerts/diff-em","last_synced_at":"2025-04-15T20:22:02.229Z","repository":{"id":40458061,"uuid":"507151494","full_name":"FrederikGoovaerts/diff-em","owner":"FrederikGoovaerts","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-04T11:42:51.000Z","size":242,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-25T02:46:50.499Z","etag":null,"topics":["added","deleted","diff","json","object","properties","typescript","updated"],"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/FrederikGoovaerts.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":"2022-06-24T21:44:36.000Z","updated_at":"2025-02-04T11:42:54.000Z","dependencies_parsed_at":"2024-12-25T07:47:03.212Z","dependency_job_id":"87897785-faea-4e41-a782-c1dc1e3c5730","html_url":"https://github.com/FrederikGoovaerts/diff-em","commit_stats":{"total_commits":33,"total_committers":1,"mean_commits":33.0,"dds":0.0,"last_synced_commit":"e1393ac1557afeab306b5bbfd93b208348752140"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FrederikGoovaerts%2Fdiff-em","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FrederikGoovaerts%2Fdiff-em/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FrederikGoovaerts%2Fdiff-em/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FrederikGoovaerts%2Fdiff-em/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FrederikGoovaerts","download_url":"https://codeload.github.com/FrederikGoovaerts/diff-em/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249145960,"owners_count":21220066,"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":["added","deleted","diff","json","object","properties","typescript","updated"],"created_at":"2024-12-25T07:42:39.521Z","updated_at":"2025-04-15T20:22:02.208Z","avatar_url":"https://github.com/FrederikGoovaerts.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Diff 'em\n\n[![npm](https://img.shields.io/npm/v/diff-em)](https://www.npmjs.com/package/diff-em)\n[![Build Status](https://github.com/FrederikGoovaerts/diff-em/actions/workflows/ci.yaml/badge.svg)](https://github.com/FrederikGoovaerts/diff-em/actions/workflows/ci.yaml)\n[![codecov](https://codecov.io/gh/FrederikGoovaerts/diff-em/branch/main/graph/badge.svg?token=LIP8MHA7HG)](https://codecov.io/gh/FrederikGoovaerts/diff-em)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)\n\nA collection of utilities to calculate differences between JavaScript objects. Written in TypeScript, zero dependencies and compatible with most recent Node.js and browser environments.\n\n## Installation\n\n```\nnpm install diff-em\n```\n\n## Usage\n\n### `diffObject`\n\nCalculates the differences between two supplied JavaScript objects. The first parameter is regarded as the original object, while the second is the new state. `diffObject` will return an object with added, updated and deleted properties. Added and updated properties will have the value of the new state object, while deleted properties will return `undefined` for their keys.\n\nArrays will be regarded as objects with numeric keys in the resulting difference object.\n\n`diffObject` has three derivates, which provide a more granular diff. `addedDiffObject`, `updatedDiffObject` and `deletedDiffObject` will only return a resulting object with added properties, updated properties and deleted properties respectively.\n\n```ts\nimport { diffObject } from 'diff-em';\n\ndiffObject({ a: 1, b: { c: 2, d: 3 } }, { a: 2, b: { d: 3 }, e: 4 });\n// result: { a: 2, b: { c: undefined }, e: 4 }\n\ndiffObject({ a: ['b', 'c'] }, { a: ['b', 'd', 'e'] });\n// result: { a: { 1: 'd', 2: 'e' }}\n```\n\n```ts\nimport { addedDiffObject, updatedDiffObject, deletedDiffObject } from 'diff-em';\n\naddedDiffObject({ a: 1, b: { c: 2 } }, { a: 2, b: { c: 2, d: 3 }, e: 4 });\n// result: { b: { d: 3 }, e: 4 }\n\nupdatedDiffObject({ a: 1, b: { c: 2, d: 3 } }, { a: 2, b: { d: 3 }, e: 4 });\n// result: { a: 2 }\n\ndeletedDiffObject({ a: 1, b: { c: 2, d: 3 } }, { a: 2, b: { d: 3 }, e: 4 });\n// result: { b: { c: undefined } }\n```\n\n### `diffPatch`\n\nCalculates the differences between two supplied JavaScript objects and returns an array with [JSON Patch (RFC 6902)](https://datatracker.ietf.org/doc/html/rfc6902/) operation objects.\n\nLike `diffObject`, arrays will be regarded as objects with numeric keys in the resulting paths.\n\n```ts\nimport { diffPatch } from 'diff-em';\n\ndiffPatch({ a: 1, b: { c: 2, d: 3 } }, { a: 2, b: { d: 3 }, e: 4 });\n// result:\n// [\n//  {\"op\": \"replace\", \"path\": \"/a\", \"value\": 2},\n//  {\"op\": \"remove\", \"path\": \"/b/c\"},\n//  {\"op\": \"add\", \"path\": \"/e\", \"value\": 4}\n// ]\n\ndiffPatch({ a: ['b', 'c'] }, { a: ['b', 'd', 'e'] });\n// result:\n// [\n//  {\"op\": \"replace\", \"path\": \"/a/1\", \"value\": \"d\"},\n//  {\"op\": \"add\", \"path\": \"/a/2\", \"value\": \"e\"}\n// ]\n```\n\n### `diffPath`\n\nCalculates the differences between two supplied JavaScript objects and returns an array with strings in [JSONPath](https://goessner.net/articles/JsonPath/) format. The entries of the array may indicate an added, updated or deleted property between the two objects. For a more granular diff, use `addedDiffPath`, `updatedDiffPath` and `deletedDiffPath`. These will only return paths for added properties, updated properties and deleted properties respectively.\n\nLike `diffObject`, arrays will be regarded as objects with numeric keys in the resulting paths.\n\n```ts\nimport { diffPath } from 'diff-em';\n\ndiffPath({ a: 1, b: { c: 2, d: 3 } }, { a: 2, b: { d: 3 }, e: 4 });\n// result: ['$.a', '$.b.c', '$.e']\n\ndiffPath({ a: ['b', 'c'] }, { a: ['b', 'd', 'e'] });\n// result: ['$.a.1', '$.a.2']\n```\n\n```ts\nimport { addedDiffPath, updatedDiffPath, deletedDiffPath } from 'diff-em';\n\naddedDiffPath({ a: 1, b: { c: 2 } }, { a: 2, b: { c: 2, d: 3 }, e: 4 });\n// result: ['$.b.d', '$.e']\n\nupdatedDiffPath({ a: 1, b: { c: 2, d: 3 } }, { a: 2, b: { d: 3 }, e: 4 });\n// result: ['$.a']\n\ndeletedDiffPath({ a: 1, b: { c: 2, d: 3 } }, { a: 2, b: { d: 3 }, e: 4 });\n// result: ['$.b.c']\n```\n\n## License\n\nDiff 'em is released under the [MIT license](LICENSE).\n\n## Acknowledgement\n\n- Thanks to [Matt Phillips](https://github.com/mattphillips) for the original code and idea this repository was based on.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrederikgoovaerts%2Fdiff-em","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrederikgoovaerts%2Fdiff-em","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrederikgoovaerts%2Fdiff-em/lists"}