Skip to content
Permalink
Browse files Browse the repository at this point in the history
don't inadvertantly modify Object.prototype
  • Loading branch information
pjshumphreys committed Mar 10, 2021
1 parent cae8100 commit 5b383c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions index.js
Expand Up @@ -102,6 +102,17 @@ const objectMerge = (original, patch, removeMode) => {
}
//merge objects
else {
//Don't implicitly mutate Object.prototype but subsitute an empty object
if(original[name] === Object.prototype) {
delete original[name];

Object.defineProperty(original, name, {
enumerable: true,
writable: true,
value: {}
});
}

original[name] = objectMerge(original[name], patch[name], removeMode);
}
}
Expand Down
5 changes: 5 additions & 0 deletions test2.js
@@ -0,0 +1,5 @@
patchMerge = require('./index.js');
var obj = {}
console.log("Before : " + obj.isAdmin);
console.log(patchMerge(obj, JSON.parse('{ "__proto__": { "isAdmin": true }}')));
console.log("After : " + obj.isAdmin);

0 comments on commit 5b383c5

Please sign in to comment.