Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to specify concat array merge strategy #22

Closed
wants to merge 4 commits into from
Closed
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
9 changes: 8 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ methods

var merge = require('deepmerge')

merge(x, y)
merge(x, y, opts)
-----------

Merge two objects `x` and `y` deeply, returning a new merged object with the
Expand All @@ -45,6 +45,13 @@ The merge is immutable, so neither `x` nor `y` will be modified.

The merge will also merge arrays and array values.

### options

**arrays**

* `merge` (default) - Replace array values at index. e.g. `[1, 2] + [3] => [3, 2]`
* `concat` - Push value to the bottom of the array. e.g. `[1, 2] + [3] => [1, 2, 3]`

install
=======

Expand Down
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@
}
}(this, function () {

return function deepmerge(target, src) {
return function deepmerge(target, src, opts) {
var array = Array.isArray(src);
var dst = array && [] || {};
opts = opts || {};

if (array) {
target = target || [];
dst = dst.concat(target);

src.forEach(function(e, i) {
if (typeof dst[i] === 'undefined') {
dst[i] = e;
} else if (typeof e === 'object') {
dst[i] = deepmerge(target[i], e);
if (opts.arrays === 'concat') {
dst.push(e);
} else {
if (target.indexOf(e) === -1) {
dst.push(e);
if (typeof dst[i] === 'undefined') {
dst[i] = e;
} else if (typeof e === 'object') {
dst[i] = deepmerge(target[i], e, opts);
} else {
if (target.indexOf(e) === -1) {
dst.push(e);
}
}
}
});
Expand All @@ -40,7 +46,7 @@ return function deepmerge(target, src) {
if (!target[key]) {
dst[key] = src[key];
} else {
dst[key] = deepmerge(target[key], src[key]);
dst[key] = deepmerge(target[key], src[key], opts);
}
}
});
Expand Down
20 changes: 20 additions & 0 deletions test/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,23 @@ test('should work on arrays of nested objects', function(t) {
t.deepEqual(merge(target, src), expected)
t.end()
})

test('should push src to target if arrays opts is concat', function(t) {
var target = [
{ key1: { subkey: 'one' } }
]

var src = [
{ key1: { subkey: 'two' } },
{ key2: { subkey: 'three' } }
]

var expected = [
{ key1: { subkey: 'one' } },
{ key1: { subkey: 'two' } },
{ key2: {subkey: 'three' } }
]

t.deepEqual(merge(target, src, { arrays: 'concat' }), expected)
t.end()
})