Skip to content

Commit

Permalink
Fixes some special-object-in-array cases
Browse files Browse the repository at this point in the history
- Extra test for #18
- Some bugs similar to #23 and #31 where regexes and dates wouldn't be copied correctly across arrays
  • Loading branch information
TehShrike committed Sep 27, 2016
1 parent 8bc841e commit ef1c6ba
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
function isMergeableObject(val) {
var nonNullObject = val && typeof val === 'object'


return nonNullObject
&& Object.prototype.toString.call(val) !== '[object RegExp]'
&& Object.prototype.toString.call(val) !== '[object Date]'
Expand All @@ -27,7 +26,7 @@ return function deepmerge(target, src) {
src.forEach(function(e, i) {
if (typeof dst[i] === 'undefined') {
dst[i] = e;
} else if (typeof e === 'object' && e !== null) {
} else if (isMergeableObject(e)) {
dst[i] = deepmerge(target[i], e);
} else {
if (target.indexOf(e) === -1) {
Expand Down
38 changes: 38 additions & 0 deletions test/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,41 @@ test('should work on array with null in it', function(t) {
t.deepEqual(merge(target, src), expected)
t.end()
})

test('null should be equal to null in an array', function(t) {
var target = [null, 'dude']
var source = [null, 'lol']

var expected = [null, 'dude', 'lol']
var actual = merge(target, source)

t.deepEqual(actual, expected)
t.end()
})

test('dates in an array should be compared correctly', function(t) {
var monday = new Date('2016-09-27T01:08:12.761Z')

var target = [monday, 'dude']
var source = [monday, 'lol']

var expected = [monday, 'dude', 'lol']
var actual = merge(target, source)

t.deepEqual(actual, expected)
t.end()
})

test('dates should copy correctly in an array', function(t) {
var monday = new Date('2016-09-27T01:08:12.761Z')
var tuesday = new Date('2016-09-28T01:18:12.761Z')

var target = [monday, 'dude']
var source = [tuesday, 'lol']

var expected = [monday, 'dude', tuesday, 'lol']
var actual = merge(target, source)

t.deepEqual(actual, expected)
t.end()
})

0 comments on commit ef1c6ba

Please sign in to comment.