Permalink
Browse files

add .merge(a, b)

  • Loading branch information...
1 parent ac21441 commit c1568ee940fad51038fe1b7fc1dbb5a8a98bbeb6 @tj tj committed Sep 19, 2012
Showing with 33 additions and 0 deletions.
  1. +5 −0 Readme.md
  2. +14 −0 index.js
  3. +14 −0 test/object.js
View
@@ -28,6 +28,11 @@ is.date(obj);
Check if `obj` is empty.
+### .merge(a, b)
+
+ Merge object `b` into `a`, returns `a`.
+ Precedence is given to `b`.
+
## License
MIT
View
@@ -41,6 +41,20 @@ exports.values = function(obj){
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`.
*
View
@@ -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(){
it('should check if the object is empty', function(){
object.isEmpty({}).should.be.true;

0 comments on commit c1568ee

Please sign in to comment.