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

Merge arrays onto objects #46

Merged
merged 3 commits into from
Oct 12, 2016
Merged
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
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ function deepmerge(target, source, optionsArgument) {
var arrayMerge = options.arrayMerge || defaultArrayMerge

if (array) {
target = target || [];
return arrayMerge(target, source, optionsArgument)
return Array.isArray(target) ? arrayMerge(target, source, optionsArgument) : source.slice()
} else {
return mergeObject(target, source, optionsArgument)
}
Expand Down
51 changes: 51 additions & 0 deletions test/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,57 @@ test('should replace object with simple key in target', function (t) {
t.end()
})

test('should replace objects with arrays', function(t) {
var target = [
{ key1: { subkey: 'one' }}
]

var src = [
{ key1: [ "subkey" ]}
]

var expected = [
{ key1: [ "subkey" ]}
]

t.deepEqual(merge(target, src), expected)
t.end()
})

test('should replace dates with arrays', function(t) {
var target = [
{ key1: new Date()}
]

var src = [
{ key1: [ "subkey" ]}
]

var expected = [
{ key1: [ "subkey" ]}
]

t.deepEqual(merge(target, src), expected)
t.end()
})

test('should replace null with arrays', function(t) {
var target = {
key1: null
}

var src = {
key1: [ "subkey" ]
}

var expected = {
key1: [ "subkey" ]
}

t.deepEqual(merge(target, src), expected)
t.end()
})

test('should work on simple array', function (t) {
var src = ['one', 'three']
var target = ['one', 'two']
Expand Down