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.all tests and implementation #50

Merged
merged 5 commits into from
Nov 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
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ 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')
}

// 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)
})
}

return deepmerge

}));
67 changes: 67 additions & 0 deletions test/merge-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var merge = require('../')
var test = require('tap').test

test('throw error if first argument is not an array', function(t) {
t.throws(merge.all.bind(null, { example: true }, { another: '2' }), Error)
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)
t.end()
})

test('execute correctly if options object were not passed', function(t) {
var arrayToMerge = [{ example: true }, { another: '123' }]
t.doesNotThrow(merge.all.bind(null, arrayToMerge))
t.end()
})

test('execute correctly if options object were passed', function(t) {
var arrayToMerge = [{ example: true }, { another: '123' }]
t.doesNotThrow(merge.all.bind(null, arrayToMerge, { clone:true }))
t.end()
})

test('invoke merge on every item in array should result with all props', function(t) {
var firstObject = { first: true }
var secondObject = { second: false }
var thirdObject = { third: 123 }
var fourthObject = { fourth: 'some string' }

var mergedObject = merge.all([firstObject, secondObject, thirdObject, fourthObject])

t.ok(mergedObject.first === true)
t.ok(mergedObject.second === false)
t.ok(mergedObject.third === 123)
t.ok(mergedObject.fourth === 'some string')
t.end()
})

test('invoke merge on every item in array with clone should clone all elements', function(t) {
var firstObject = { a: { d: 123 } }
var secondObject = { b: { e: true } }
var thirdObject = { c: { f: 'string' } }

var mergedWithClone = merge.all([firstObject, secondObject, thirdObject], { clone: true })

t.notEqual(mergedWithClone.a, firstObject.a)
t.notEqual(mergedWithClone.b, secondObject.b)
t.notEqual(mergedWithClone.c, thirdObject.c)

t.end()
})

test('invoke merge on every item in array without clone should not clone all elements', function(t) {
var firstObject = { a: { d: 123 } }
var secondObject = { b: { e: true } }
var thirdObject = { c: { f: 'string' } }

var mergedWithoutClone = merge.all([firstObject, secondObject, thirdObject])

t.equal(mergedWithoutClone.a, firstObject.a)
t.equal(mergedWithoutClone.b, secondObject.b)
t.equal(mergedWithoutClone.c, thirdObject.c)

t.end()
})