-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(extend): optionally deep-extend when last parameter is true
#10519
Changes from 3 commits
5e927ee
f683061
550f4ac
b3f74d0
090e9b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -333,27 +333,43 @@ function setHashKey(obj, h) { | |
* Extends the destination object `dst` by copying own enumerable properties from the `src` object(s) | ||
* to `dst`. You can specify multiple `src` objects. If you want to preserve original objects, you can do so | ||
* by passing an empty object as the target: `var object = angular.extend({}, object1, object2)`. | ||
* Note: Keep in mind that `angular.extend` does not support recursive merge (deep copy). | ||
* | ||
* @param {Object} dst Destination object. | ||
* @param {...Object} src Source object(s). | ||
* @param {boolean=} deep if the last parameter is set to `true`, objects are recursively merged | ||
* (deep copy). Defaults to `false`. | ||
* @returns {Object} Reference to `dst`. | ||
*/ | ||
function extend(dst) { | ||
var h = dst.$$hashKey; | ||
var argsLength = arguments.length; | ||
var isDeep = false; | ||
if (argsLength >= 3) { | ||
var maybeIsDeep = arguments[argsLength - 1]; | ||
// Secret code to use deep extend without adding hash keys to destination object properties! | ||
if (maybeIsDeep === true || maybeIsDeep === 0xFACECAFE) isDeep = maybeIsDeep; | ||
} | ||
|
||
if (isDeep) --argsLength; | ||
|
||
for (var i = 1, ii = arguments.length; i < ii; i++) { | ||
for (var i = 1; i < argsLength; i++) { | ||
var obj = arguments[i]; | ||
if (obj) { | ||
var keys = Object.keys(obj); | ||
for (var j = 0, jj = keys.length; j < jj; j++) { | ||
var key = keys[j]; | ||
dst[key] = obj[key]; | ||
if (!isObject(obj) && !isFunction(obj)) continue; | ||
var keys = Object.keys(obj); | ||
for (var j = 0, jj = keys.length; j < jj; j++) { | ||
var key = keys[j]; | ||
var src = obj[key]; | ||
|
||
if (isDeep && isObject(src)) { | ||
if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {}; | ||
extend(dst[key], src, 0xFACECAFE); | ||
} else { | ||
dst[key] = src; | ||
} | ||
} | ||
} | ||
|
||
setHashKey(dst, h); | ||
if (isDeep !== 0xFACECAFE) setHashKey(dst, h); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably still attach a hashkey if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, I think that makes sense, will get rid of FACECAFE =) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ich bin bis 05.01.2015 abwesend. Ich werde vertreten durch Herrn T. Landgraf und Herrn D. Neander. Hinweis: Dies ist eine automatische Antwort auf Ihre Nachricht "Re: Diese ist die einzige Benachrichtigung, die Sie empfangen werden, während |
||
return dst; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about object cycles? At least, I think we should put something in the docs about not trying to deep extend an object that has cyclic references.