From ccbb9700dc6f8823ef50bbaf67d2807a8be7a0d6 Mon Sep 17 00:00:00 2001 From: Marcus Westin Date: Tue, 7 May 2013 19:51:00 -0700 Subject: [PATCH] add addProps and setProps for adding/setting properties of an object, respectively --- addProps.js | 12 ++++++++++++ setProps.js | 10 ++++++++++ 2 files changed, 22 insertions(+) create mode 100644 addProps.js create mode 100644 setProps.js diff --git a/addProps.js b/addProps.js new file mode 100644 index 0000000..78c95ea --- /dev/null +++ b/addProps.js @@ -0,0 +1,12 @@ +var each = require('./each') +var copy = require('./copy') + +module.exports = function addProps(target, addPropsIfNotExist) { + var result = copy(target) + each(addPropsIfNotExist, function(val, key) { + if (result[key] == undefined) { + result[key] = addPropsIfNotExist[key] + } + }) + return result +} diff --git a/setProps.js b/setProps.js new file mode 100644 index 0000000..1673403 --- /dev/null +++ b/setProps.js @@ -0,0 +1,10 @@ +var each = require('./each') +var copy = require('./copy') + +module.exports = function setProps(target, overwriteProps) { + var result = copy(target) + each(overwriteProps, function(val, key) { + result[key] = overwriteProps[key] + }) + return result +}