Skip to content

Commit

Permalink
renaming merge to update - only updates existing values on the left o…
Browse files Browse the repository at this point in the history
…bject
  • Loading branch information
selfcontained committed Jul 25, 2013
1 parent f24c93d commit 5f56779
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var deap = module.exports = lib.extend;
deap(deap, {
clone: lib.clone,
extend: lib.extend,
merge: lib.merge,
update: lib.update,
extendShallow: lib.extendShallow,
mergeShallow: lib.mergeShallow
updateShallow: lib.updateShallow
});
10 changes: 5 additions & 5 deletions lib/deap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module.exports = {
clone: clone,
extend: deepExtend,
extendShallow: extend,
merge: deepMerge,
mergeShallow: merge
update: deepUpdate,
updateShallow: update
};

function clone(val) {
Expand Down Expand Up @@ -45,7 +45,7 @@ function deepExtend(a, b /*, [b2..n] */) {
return a;
}

function merge(a, b /*, [b2..n] */) {
function update(a, b /*, [b2..n] */) {
slice.call(arguments, 1).forEach(function(b) {
Object.keys(b).forEach(function(p) {
if(a.hasOwnProperty(p)) a[p] = b[p];
Expand All @@ -54,7 +54,7 @@ function merge(a, b /*, [b2..n] */) {
return a;
}

function deepMerge(a, b /*, [b2..n] */) {
function deepUpdate(a, b /*, [b2..n] */) {
slice.call(arguments, 1).forEach(function(b) {
var ap, bp, ta, tb;
Object.keys(b).forEach(function(p) {
Expand All @@ -64,7 +64,7 @@ function deepMerge(a, b /*, [b2..n] */) {
ta = typeOf(ap);
tb = typeOf(bp);
if(tb === 'object' && ta === 'object')
deepMerge(ap, bp);
deepUpdate(ap, bp);
else if(tb === 'array' && ta === 'array') {
ap.length = 0;
ap.push.apply(ap, bp.map(clone));
Expand Down
2 changes: 1 addition & 1 deletion shallow.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ var deap = module.exports = lib.extendShallow;
deap(deap, {
clone: lib.clone,
extend: lib.extendShallow,
merge: lib.mergeShallow
update: lib.updateShallow
});
12 changes: 6 additions & 6 deletions test/deap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ describe('deap', function() {
assert.deepEqual(deap.extendShallow, lib.extendShallow);
});

it('should have merge defined', function() {
assert.isFunction(deap.merge);
assert.deepEqual(deap.merge, lib.merge);
it('should have update defined', function() {
assert.isFunction(deap.update);
assert.deepEqual(deap.update, lib.update);
});

it('should have mergeShallow defined', function() {
assert.isFunction(deap.mergeShallow);
assert.deepEqual(deap.mergeShallow, lib.mergeShallow);
it('should have updateShallow defined', function() {
assert.isFunction(deap.updateShallow);
assert.deepEqual(deap.updateShallow, lib.updateShallow);
});


Expand Down
4 changes: 2 additions & 2 deletions test/shallow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ describe('shallow', function() {
assert.isFunction(shallow);

assert.isFunction(shallow.extend);
assert.isFunction(shallow.merge);
assert.isFunction(shallow.update);
assert.isFunction(shallow.clone);
});

it('should be shallow functions', function() {
assert.equal(shallow, lib.extendShallow);
assert.equal(shallow.extend, lib.extendShallow);
assert.equal(shallow.merge, lib.mergeShallow);
assert.equal(shallow.update, lib.updateShallow);
assert.equal(shallow.clone, lib.clone);
});

Expand Down
40 changes: 20 additions & 20 deletions test/merge.test.js → test/update.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var lib = require('../lib/deap'),
assert = require('chai').assert;

describe('shallow merge', function() {
var shallowMerge = lib.mergeShallow;
describe('shallow update', function() {
var shallowUpdate = lib.updateShallow;

it('should not merge anything into an empty object', function() {
var result = shallowMerge({}, { foo: 'bar' });
it('should not update anything into an empty object', function() {
var result = shallowUpdate({}, { foo: 'bar' });

assert.deepEqual(result, {});
});
Expand All @@ -14,7 +14,7 @@ describe('shallow merge', function() {
var a = { burp: 'adurp' },
b = { burp: 'zing', grr: 'arghh' };

var result = shallowMerge(a, b);
var result = shallowUpdate(a, b);

assert.strictEqual(result, a);
});
Expand All @@ -23,7 +23,7 @@ describe('shallow merge', function() {
var a = { burp: 'adurp' },
b = { burp: 'zing', grr: 'arghh' };

var result = shallowMerge(a, b);
var result = shallowUpdate(a, b);

assert.deepEqual(result, a);
assert.equal(a.burp, b.burp);
Expand All @@ -32,34 +32,34 @@ describe('shallow merge', function() {

});

describe('deep merge', function() {
var deepMerge = lib.merge;
describe('deep update', function() {
var deepUpdate = lib.update;

it('should return a reference to the first argument', function() {
var a = { burp: 'adurp' },
b = { burp: 'zing', grr: 'arghh' };

var result = deepMerge(a, b);
var result = deepUpdate(a, b);

assert.strictEqual(result, a);
});

it('should merge a nested object one level deep', function() {
it('should update a nested object one level deep', function() {
var a = { foo: 'bar', deep: { foo: 'bar', baz: 'buzz' }},
b = { deep: { foo: 'beep' } };

var result = deepMerge(a, b);
var result = deepUpdate(a, b);

assert.equal(result.foo, a.foo);
assert.equal(result.deep.foo, b.deep.foo);
assert.equal(result.deep.baz, a.deep.baz);
});

it('should merge a nested object two levels deep', function() {
it('should update a nested object two levels deep', function() {
var a = { foo: 'bar', deep: { hi: 'hello', deeper: { foo: 'bar', baz: 'buzz' }}},
b = { deep: { deeper: { foo: 'beep' } } };

var result = deepMerge(a, b);
var result = deepUpdate(a, b);

assert.equal(result.foo, a.foo);
assert.isObject(result.deep);
Expand All @@ -69,12 +69,12 @@ describe('deep merge', function() {
assert.equal(result.deep.deeper.baz, a.deep.deeper.baz);
});

it('should merge properties from multiple objects', function() {
it('should update properties from multiple objects', function() {
var a = { foo: ['one'], boo: 'far', poo: 'tar' },
b = { foo: ['two', 'three'], zoo: 'car' },
c = { boo: 'star', two: 'czar' };

var result = deepMerge(a, b, c);
var result = deepUpdate(a, b, c);

assert.deepEqual(result, {
foo: b.foo,
Expand All @@ -83,11 +83,11 @@ describe('deep merge', function() {
});
});

it('should not merge properties that are not on the first argument', function() {
it('should not update properties that are not on the first argument', function() {
var a = { foo: 'bar', deep: { deeper: { foo: 'bar' } } },
b = { boo: 'far', deep: { hi: 'hello', deeper: { foo: 'beep', baz: 'buzz' } } };

var result = deepMerge(a, b);
var result = deepUpdate(a, b);

assert.isUndefined(result.boo);
assert.isObject(result.deep);
Expand All @@ -103,7 +103,7 @@ describe('deep merge', function() {
newFoo = { burp: nested },
b = { foo: newFoo };

var result = deepMerge(a, b);
var result = deepUpdate(a, b);

assert.deepEqual(a.foo.burp, b.foo.burp);
assert.notStrictEqual(a.foo.burp, nested);
Expand All @@ -114,7 +114,7 @@ describe('deep merge', function() {
b = { nested: [{ boo: 'far' }] },
deep = a.nested;

var result = deepMerge(a, b);
var result = deepUpdate(a, b);

assert.deepEqual(result.nested, b.nested);
assert.notStrictEqual(result.nested, b.nested);
Expand All @@ -127,7 +127,7 @@ describe('deep merge', function() {
b = { nested: [{ boo: 'far' }] },
deeper = a.nested[0];

var result = deepMerge(a, b);
var result = deepUpdate(a, b);

assert.deepEqual(result.nested, b.nested);
assert.notStrictEqual(result.nested[0], deeper);
Expand Down

0 comments on commit 5f56779

Please sign in to comment.