{"id":15369089,"url":"https://github.com/developit/object-diff-patch","last_synced_at":"2025-04-14T00:42:42.076Z","repository":{"id":47630970,"uuid":"398313121","full_name":"developit/object-diff-patch","owner":"developit","description":null,"archived":false,"fork":false,"pushed_at":"2021-08-20T16:39:25.000Z","size":10,"stargazers_count":108,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-10T11:16:09.245Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/developit.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}},"created_at":"2021-08-20T15:01:50.000Z","updated_at":"2025-02-07T03:29:57.000Z","dependencies_parsed_at":"2022-09-10T12:12:13.694Z","dependency_job_id":null,"html_url":"https://github.com/developit/object-diff-patch","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/developit%2Fobject-diff-patch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fobject-diff-patch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fobject-diff-patch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fobject-diff-patch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developit","download_url":"https://codeload.github.com/developit/object-diff-patch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248804718,"owners_count":21164127,"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":"2024-10-01T13:34:05.914Z","updated_at":"2025-04-14T00:42:42.057Z","avatar_url":"https://github.com/developit.png","language":"JavaScript","readme":"# `object-diff-patch` [![npm](https://img.shields.io/npm/v/object-diff-patch.svg)](https://www.npmjs.org/package/object-diff-patch)\n\nCalculates the difference between two objects/arrays/primitives.\nThe resulting patch object is a subset of the source object's structure.\n\nPatches can be applied to objects to morph them into the result.\n\nYou can use this library to synchronize objects between threads without transfering the whole object every sync.\n\n## Example\n\n```js\nimport { diff, apply } from 'object-diff-patch';\n\n// our initial record:\nlet person = {\n\tname: 'Robert',\n\taliases: ['Rob', 'Bob']\n};\n\n// a new version of the record:\nlet newPerson = {\n\tfirstName: 'Robert',\n\tlastName: 'Loblaw',\n\taliases: ['Rob', 'Bob', 'Bobby']\n};\n\n// Calculate the differences between the first and second version:\nlet patch = diff(person, newPerson);\n// {\n//   name: undefined,\n//   firstName: 'Robert',\n//   lastName: 'Loblaw',\n//   aliases: { 2: 'Bobby' }\n// }\n\n// Apply the patch to the first version:\nperson = apply(person, patch);\n\n// Now they're the same:\nJSON.stringify(person) === JSON.stringify(newPerson); // true\n```\n\n## Threaded Example\n\nThe following example shows how to use `object-diff-patch` to\nsend deltas/patches back from a Worker thread instead of whole new objects.\n\n**index.js:**\n\n```js\nimport { wrap } from 'comlink';\nimport { apply } from 'object-diff-patch/apply';\n\nconst worker = wrap(new Worker('./worker.js', { type: 'module' }));\n\nconst CACHE = new Map();\n\nexport async function getThing(name) {\n\t// grab the previous object from our cache:\n\tlet obj = CACHE.get(name);\n\n\t// get only the patch/delta object from the worker:\n\tconst patch = await worker.getThing(name);\n\n\t// morph the previous object into the new one:\n\tobj = apply(obj, patch);\n\n\t// cache the new result (so we can repeat this again)\n\tCACHE.set(name, obj);\n\treturn obj;\n}\n```\n\n**worker.js:**\n\n```js\nimport { expose } from 'comlink';\nimport { diff } from 'object-diff-patch/diff';\n\nconst CACHE = new Map();\n\nasync function getThing(name) {\n\tconst res = await fetch(`/things/${name}`);\n\tconst thing = await res.json();\n\n\tconst old = CACHE.get(name); // note: can be undefined!\n\n\t// generate the patch object (the delta from the previous cached result):\n\tconst patch = diff(thing, old);\n\n\t// store for next call (so we can repeat this again)\n\tCACHE.set(name, thing);\n\n\t// pass only the patch/delta to the main thread\n\treturn patch;\n}\n\nexpose({ getThing });\n```\n\n## License\n\nApache-2.0\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fobject-diff-patch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevelopit%2Fobject-diff-patch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fobject-diff-patch/lists"}