Skip to content

Commit

Permalink
Allow calling merge.all with only one element
Browse files Browse the repository at this point in the history
Fixes #71
  • Loading branch information
TehShrike committed Oct 4, 2017
1 parent 7559f45 commit bdc290b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ function deepmerge(target, source, optionsArgument) {
}

deepmerge.all = function deepmergeAll(array, optionsArgument) {
if (!Array.isArray(array) || array.length < 2) {
throw new Error('first argument should be an array with at least two elements')
if (!Array.isArray(array) || array.length < 1) {
throw new Error('first argument should be an array with at least one element')
}

// we are sure there are at least 2 values, so it is safe to have no initial value
return array.reduce(function(prev, next) {
return deepmerge(prev, next, optionsArgument)
})
}, {})
}

module.exports = deepmerge
11 changes: 9 additions & 2 deletions test/merge-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ test('throw error if first argument is not an array', function(t) {
t.end()
})

test('throw error if first argument is an array with least than two elements', function(t) {
t.throws(merge.all.bind(null, [{ example: true }]), Error)
test('throw error if first argument is an array with no elements', function(t) {
t.throws(merge.all.bind(null, []), Error)
t.end()
})

test('Work just fine if first argument is an array with least than two elements', function(t) {
var actual = merge.all([{ example: true }])
var expected = { example: true }
t.deepEqual(actual, expected)
t.end()
})

Expand Down

0 comments on commit bdc290b

Please sign in to comment.