Skip to content

Commit

Permalink
Support for extending arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
BenHall committed Nov 28, 2011
1 parent 4ce11b2 commit 83133be
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/objects.js
Expand Up @@ -41,6 +41,8 @@ $(document).ready(function() {
equals(_.keys(result).join(''), 'b', 'extend does not copy undefined values');
same(_.extend({x:'x', a:{b:'c'}}, { a: { d: 'e' } }).a, {b:'c', d:'e'}, 'can extend an object recursively when source property is a hash');
same(_.extend({x:'x'}, { a: { b: 'c' } }).a, {b:'c'}, 'can extend an object recursively when destination does not exist');
same(_.extend({x:'x', a:[1,2,3]}, { a:[4] }).a, [1,2,3,4], 'can merge arrays');
same(_.extend({x:'x', a:{b:'c'}}, { a:[4] }).a, [4], 'overrides destination with source if types are different');
});

test("objects: defaults", function() {
Expand Down
3 changes: 3 additions & 0 deletions underscore.js
Expand Up @@ -649,6 +649,9 @@
if(obj[prop] != undefined && _.isObject(source[prop]) && !_.isArray(source[prop])) {
_.extend(obj[prop], source[prop]);
}
else if(_.isArray(obj[prop]) && _.isArray(source[prop])) {
obj[prop] = _.union(obj[prop], source[prop]);
}
else {
obj[prop] = source[prop];
}
Expand Down

0 comments on commit 83133be

Please sign in to comment.