{"id":13652551,"url":"https://github.com/paldepind/dffptch","last_synced_at":"2025-06-15T19:04:41.493Z","repository":{"id":24206503,"uuid":"27598070","full_name":"paldepind/dffptch","owner":"paldepind","description":"A micro library for diffing and patching JSON objects using a compact diff format","archived":false,"fork":false,"pushed_at":"2017-01-11T04:09:08.000Z","size":276,"stargazers_count":171,"open_issues_count":2,"forks_count":3,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-05-02T23:04:19.370Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paldepind.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-05T15:44:30.000Z","updated_at":"2025-02-11T15:47:50.000Z","dependencies_parsed_at":"2022-07-12T07:00:18.243Z","dependency_job_id":null,"html_url":"https://github.com/paldepind/dffptch","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/paldepind/dffptch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paldepind%2Fdffptch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paldepind%2Fdffptch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paldepind%2Fdffptch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paldepind%2Fdffptch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paldepind","download_url":"https://codeload.github.com/paldepind/dffptch/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paldepind%2Fdffptch/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260035128,"owners_count":22949226,"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-08-02T02:01:00.369Z","updated_at":"2025-06-15T19:04:41.456Z","avatar_url":"https://github.com/paldepind.png","language":"JavaScript","funding_links":[],"categories":["Differencing"],"sub_categories":[],"readme":"Δ dffptch.js\n==========\nA micro library for diffing and patching JSON objects using a compact diff format.\n\nWhy\n---\nIf your JavaScript client is sending a lot of updates to your backend – as it might\nin a collaborative app, a real-time game or a continously saving app – \ntransfering the entire changed JSON wastes a lot of bandwidth. dffptch.js\nmakes sending only the changes in a compact format very easy.\n\nExample\n-------\n```javascript\nvar rabbit = {\n  name: 'Thumper',\n  color: 'grey',\n  age: 2,\n  bestFriend: 'bambi',\n  foodNotes: {\n    grassHay: 'his primary food'\n  } \n};\n// We make some changes to our rabbit\nvar updatedRabbit = {\n  name: 'Thumper', // Still the same name\n  color: 'grey and white', // He is white as well\n  age: 3, // He just turned three!\n  // we delete `bestFriend` – Thumper has many friends and he likes them equally\n  foodNotes: {\n    grassHay: 'his primary food', // Grass hay is still solid food for a rabbit\n    carrots: 'he likes them a lot' // He also likes carrots\n  } \n};\nvar delta = diff(rabbit, updatedRabbit);\n// Delta is now a compact diff representing 1 deletion, 2 modifications and 1\n// nested added property. The diff format might look odd, but is actually very\n// simple as explained below.\nassert.deepEqual(delta,\n                 {\"d\": [\"1\"],\n                  \"m\": {\"0\": 3, \"2\": \"grey and white\"},\n                  \"r\": {\"3\": {\"a\": {\"carrots\": \"he likes them a lot\"}}}\n                 });\n```\n\nFeatures\n--------\n* __What you expect__ – Handles all types of changes. Added, modified and\n  deleted properties. Nested objects and arrays.\n* __Compact one way diffs__ – No needless verbosity. Property names\n  are shortened to single characters while still remaining unambiguous.\n* __Performant__ – Sane choices of algorithms. For instance, when generating\n  the delta the keys of the old and new object are sorted, and then all changes\n  are found in a single pass over both objects at the same time.\n* __Very small__ – Too many huge libraries claim to be lightweight! This one is\n  not among them. By having a tight focus on its targeted use case and a\n  carefull implementation dffptch.js is barely 600B minified. Gzipped it's only\n  420B.\n* __Readable source code__ – Well commented and less than 50 sloc. UglifyJS2\n  takes care of almost all golfing.\n* __Availability__: Available both as a CommonJS module, a AMD module and a\n  plain old global export.\n* __Tested__: The test suite covers every feature.\n\nCompared to other diff \u0026 patch libraries\n----------------------------------------\n* It is significantly smaller. Ten times smaller than some alternatives.\n* In common cases dffptch.js generates smaller diffs because it only patches one way and thus can shorten property name.\n* It doesn't handle complex array changes as well as others. See the section on limitations below.\n\nInstall\n-------\n```\nbower install dffptch\n```\nor\n```\nnpm install dffptch\n```\n\nHow the diff format works\n-------------------------\nThe diff format is an object with up to four properties. `a` is an\nobject representing added properties. Each key is the name of a property and\neach value is the value assigned to the property. `m` is a similar object but\nfor modified properties and with shortened keys. `d` is an array with\ndeleted properties as elements. `r` contains all changes to nested objects\nand arrays, it recursively contains the four properties as well for the\nnested object.\n\nAn example\n```javascript\n{\n  a: {foo: 'bar'}, // One added property\n  m: {'3': 'hello'}, // One modified property\n  d: ['5'], // One deleted property\n  r: {'3': { ... }} Changes to one nested object\n}\n```\nIn `m`, `d` and `r` the property names are shortened to single characters.  The\nalgorithm works like this: The keys in the original object are sorted, giving\neach key a unique number.  The number is converted to a character using\nJavaScripts `String.fromCharCode` with an offset so the first key is assigned\nto the char '1' (this avoids the characters '/' and '\\' that require escaping\nin JSON.\n\nSo for this object\n```javascript\n{\n  foo: 'bar',\n  sample: 'object',\n  an: 'example'\n}\n```\nwe'd get the sorted keys `['an', 'foo', 'sample']` and thus `an` whould be shortened\nto `'1'`, `foo` to `'2'` and `sample` to `3`. There are _a lot_ of unicode characters\nso this approach is safe no matter how many properties your objects have.\n\nBrowser support\n---------------\ndffptch.js is environment independent (neither Node nor a browser is required).\nIt does however use the two ECMAScript 5 functions\n[`Object.keys`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) and\n[`Array.prototype.map`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map).\nIf you require support for \u003c IE9 you should polyfill those functions\n(splendid polyfills are included in the MDN links above).\n\nLimitations\n-----------\nThe differences generated by dffptch.js can only take you from _a to b_.\nNot from _b to a_. This is by design and is necessary for the compact format.\n\ndffptch.js handles arrays as it handles objects.  Order is not taken into\naccount. If you're changing elements or append to an array this is not an\nissue. However, if you're reordering or inserting elements the diffs will be\nsuboptimal. Finding the shortest edit distance in an ordered and possibly\nnested collection whould complicate dffptch.js significantly with little\nbenefit. Simply flattening your data before feeding it to dffptch.js avoids the\nproblem.\n\nLicense\n-------\ndffptch.js is made by Simon Friis Vindum. But copyright declarations wastes bandwidth.\nthus dffptch.js is public domain or [WTFPL](http://en.wikipedia.org/wiki/WTFPL) or\n[CC0](http://en.wikipedia.org/wiki/Creative_Commons_license#Zero_.2F_Public_domain).\nDo what you want but please [follow me on Twitter](https://twitter.com/paldepind)\nor give a GitHub star if you feel like it.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaldepind%2Fdffptch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaldepind%2Fdffptch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaldepind%2Fdffptch/lists"}