Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

copy() now takes additional props to set on the result; copy() no lon…

…ger takes a deep boolean; deepCopy() does deep copies; setProps no longer copies the target
  • Loading branch information...
commit 99f2eedbd2a9e734f7d138b96b3ffe2f6be4a032 1 parent 11a0f55
@marcuswestin authored
Showing with 20 additions and 23 deletions.
  1. +0 −12 addProps.js
  2. +6 −7 copy.js
  3. +11 −0 deepCopy.js
  4. +3 −4 setProps.js
View
12 addProps.js
@@ -1,12 +0,0 @@
-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
-}
View
13 copy.js
@@ -1,10 +1,9 @@
-var each = require('./each'),
- isArray = require('./isArray')
+var isList = require('./isList')
+var setProps = require('./setProps')
-module.exports = function copy(obj, deep) {
- var result = isArray(obj) ? new Array(obj.length) : {}
- each(obj, function(val, key) {
- result[key] = (deep && typeof val == 'object') ? copy(val, deep) : val
- })
+module.exports = function copy(original, additionalProps) {
+ var result = isList(original) ? [] : {}
+ setProps(result, original)
+ if (additionalProps) { setProps(result, additionalProps) }
return result
}
View
11 deepCopy.js
@@ -0,0 +1,11 @@
+var each = require('./each')
+var isList = require('./isList')
+var isObject = require('./isObject')
+
+module.exports = function deepCopy(obj) {
+ var result = isList(obj) ? [] : {}
+ each(obj, function(val, key) {
+ result[key] = ((isList(val) || isObject(val)) ? deepCopy(val) : val)
+ })
+ return result
+}
View
7 setProps.js
@@ -1,10 +1,9 @@
var each = require('./each')
-var copy = require('./copy')
module.exports = function setProps(target, overwriteProps) {
- var result = copy(target)
+ if (!target) { return null }
each(overwriteProps, function(val, key) {
- result[key] = overwriteProps[key]
+ target[key] = overwriteProps[key]
})
- return result
+ return target
}
Please sign in to comment.
Something went wrong with that request. Please try again.