Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@paldepind
Forked from 140bytes/LICENSE.txt
Last active March 8, 2018 11:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paldepind/e895067a68153290bba0 to your computer and use it in GitHub Desktop.
Save paldepind/e895067a68153290bba0 to your computer and use it in GitHub Desktop.

patch – 140byt.es

A patch function in 67 bytes. It modifies a given object according to a diff as generated by this diff function.

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(a, // Object to patch
b, // The patch
c
){
for (c in b.m) // For each change in patch
a[c] = b.m[c]; // Apply the change to obj
for (c in b.d) // For each deletion in patch
delete a[c]; // Delete property in obj
}
function(a,b,c){for(c in b.m)a[c]=b.m[c];for(c in b.d)delete a[c]}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2014 Simon Friis Vindum <simon@vindum.io>
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": "patch",
"description": "Patches object with given diff.",
"keywords": [
"diff",
"patch"
]
}
<!DOCTYPE html>
<title>Patch</title>
<script>
var patch = function(a, // Object to patch
b, // The patch
c
){
for (c in b.m) // For each change in patch
a[c] = b.m[c]; // Apply the change to obj
for (c in b.d) // For each deletion in patch
delete a[c]; // Delete property in obj
}
var rabbit = {name: 'Thumper', age: 1};
var rabbitPatch = {m: {color: 'grey'}, d: {age: 1}};
console.log('Expected');
console.log({name: 'Thumper', color: 'grey'});
console.log('Actual');
patch(rabbit, rabbitPatch);
console.log(rabbit);
</script>
@jimmywarting
Copy link

(a,b)=>{Object.assign(a,b.m);for(b in b.d)delete a[b]}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment