Skip to content

Instantly share code, notes, and snippets.

@Thaina
Created August 5, 2016 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thaina/9e4d8d6643b3dee06903350ffc5d9714 to your computer and use it in GitHub Desktop.
Save Thaina/9e4d8d6643b3dee06903350ffc5d9714 to your computer and use it in GitHub Desktop.
function (doc,req) {
if(!req.id)
return [null,{ message : "Support only PUT with id" }];
if(!doc)
doc = { "_id":req.id };
var data = JSON.parse(req.body);
delete data._id;
delete data.id;
function MergeObject(origin,obj) {
var arr = Object.keys(obj);
arr.forEach(function(key) {
if(obj[key] instanceof Object && !Array.isArray(obj[key]))
{
if(!origin[key])
origin[key] = {};
obj[key] = MergeObject(origin[key],obj[key]);
if(Object.keys(obj[key]).length < 1)
delete obj[key];
}
else if(obj[key] === null)
{
if(origin[key])
delete origin[key];
else delete obj[key];
}
else if(origin[key] === obj[key])
delete obj[key];
else origin[key] = obj[key];
});
return obj;
}
data = MergeObject(doc,data);
return [doc,JSON.stringify(data)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment