Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Updated angular extend to ignore undefined properties #8387

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ function setHashKey(obj, h) {
*
* @description
* Extends the destination object `dst` by copying all of the properties from the `src` object(s)
* to `dst`. You can specify multiple `src` objects.
* to `dst`. You can specify multiple `src` objects. Properties that are `undefined` are not copied.
*
* @param {Object} dst Destination object.
* @param {...Object} src Source object(s).
Expand All @@ -337,7 +337,9 @@ function extend(dst) {
forEach(arguments, function(obj) {
if (obj !== dst) {
forEach(obj, function(value, key) {
dst[key] = value;
if (isDefined(value)) {
dst[key] = value;
}
});
}
});
Expand Down
16 changes: 16 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,22 @@ describe('angular', function() {
expect(clone.$$some).toBeUndefined();
expect(clone.$$).toBeUndefined();
});

it('should omit properties with undefined values', function() {
var src,dst;
src = {key:undefined};
dst = {key:'Hello World'};
dst = extend(dst,src);
expect(dst.key).not.toBeUndefined();
});

it('should add properties with null values', function() {
var src,dst;
src = {key:null};
dst = {key:'Hello World'};
dst = extend(dst,src);
expect(dst.key).toBeNull();
});

it('should copy "$"-prefixed properties from copy', function() {
var original = {$some: true};
Expand Down