Skip to content

Instantly share code, notes, and snippets.

@paldepind
Forked from 140bytes/LICENSE.txt
Last active February 1, 2018 13:47
Show Gist options
  • Save paldepind/e01103ffb993032067c7 to your computer and use it in GitHub Desktop.
Save paldepind/e01103ffb993032067c7 to your computer and use it in GitHub Desktop.

diff – 140byt.es

A diff function in 110 bytes. It supports changes, additions and deletions. Nested objects however are unsupported.

The returned diffs can be patched in using this patch function in < 140 kb;

For a full featured diff and patch library in 600 bytes check out dffptch.js.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function x(a, // Original object
b, // New object
c, // Changed properties
d, //
e // Deleted properties
){
c = {}, e = {};
for(d in b) // get each key in changed obj
a[d] !== b[d] && // if the vals aren't equal
(c[d] = b[d]); // add the val to changes
for(d in a) // get each key in original obj
d in b || // if key isn't in changed obj
(e[d] = 1); // add val to deleted obj
return {m: c, d: e}
}
function(a,b,c,d,e){c={},e={};for(d in b)a[d]!==b[d]&&(c[d]=b[d]);for(d in a)d in b||(e[d]=1);return{m:c,d:e}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2014 Simon Friis Vindum <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "diff",
"description": "Generates the difference between two objects.",
"keywords": [
"diff",
"patch"
]
}
<!DOCTYPE html>
<title>Diff</title>
<script>
var diff = function(a,b,c,d,e){c={},e={};for(d in b)a[d]!==b[d]&&(c[d]=b[d]);for(d in a)d in b||(e[d]=1);return{m:c,d:e}}
var rabbit = {name: 'Thumper', age: 1}
var newRabbit = {name: 'Thumper', color: 'grey'}
console.log('Expected');
console.log({m: {color: 'grey'}, d: {age: 1}});
console.log('Actual');
console.log(diff(rabbit, newRabbit));
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment