Skip to content

Commit

Permalink
add .merge(a, b)
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Sep 19, 2012
1 parent ac21441 commit c1568ee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ is.date(obj);


Check if `obj` is empty. Check if `obj` is empty.


### .merge(a, b)

Merge object `b` into `a`, returns `a`.
Precedence is given to `b`.

## License ## License


MIT MIT
14 changes: 14 additions & 0 deletions index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ exports.values = function(obj){
return vals; return vals;
}; };


/**
* Merge `b` into `a`.
*
* @param {Object} a
* @param {Object} b
* @return {Object} a
* @api public
*/

exports.merge = function(a, b){
for (var key in b) a[key] = b[key];
return a;
};

/** /**
* Return length of `obj`. * Return length of `obj`.
* *
Expand Down
14 changes: 14 additions & 0 deletions test/object.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ describe('.length(obj)', function(){
}) })
}) })


describe('.merge(a, b)', function(){
it('should merge two objects', function(){
var a = { foo: 'bar' };
var b = { bar: 'baz' };
object.merge(a, b).should.eql({ foo: 'bar', bar: 'baz' });
})

it('should give precedence to b', function(){
var a = { foo: 'bar' };
var b = { foo: 'baz' };
object.merge(a, b).should.eql({ foo: 'baz' });
})
})

describe('.isEmpty()', function(){ describe('.isEmpty()', function(){
it('should check if the object is empty', function(){ it('should check if the object is empty', function(){
object.isEmpty({}).should.be.true; object.isEmpty({}).should.be.true;
Expand Down

0 comments on commit c1568ee

Please sign in to comment.