Skip to content

Commit

Permalink
Fixed merge operator when both source and target are strings
Browse files Browse the repository at this point in the history
closes #25
  • Loading branch information
elboletaire committed Nov 5, 2018
1 parent 1b13c32 commit 8c57f1f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/worker/execution-plugins/merger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ describe('merger', () => {
}
},
payload3: undefined,
payload4: {
nestedValues: {
one: 'whatever',
},
},
valueNotToBeMerged: {
iMust: 'be on result'
}
Expand Down Expand Up @@ -84,4 +89,23 @@ describe('merger', () => {
done()
})
})

it('should merge strings even with empty objects from next merges', (done) => {
const options = {
sourceFields: [
'payload.nestedValues.one',
'payload4.nestedValues.one',
],
targetField: 'newBody.deep'
}

const objTransformer = new MergerPlugin(msg, {options}, '')

objTransformer.execute((err, mergedObj) => {
expect(err).to.be.null
expect(mergedObj.newBody.deep).to.be.an('string')
expect(mergedObj.newBody.deep).to.equal('whatever')
done()
})
})
})
4 changes: 4 additions & 0 deletions src/worker/execution-plugins/merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export default class MergerPlugin {
})

const mergedResult = slicedObjects.reduce((prevObj: any, currObj: any) => {
if (typeof prevObj === 'string' && typeof currObj === 'string') {
return currObj
}

return merge(prevObj, currObj)
}, [])

Expand Down

0 comments on commit 8c57f1f

Please sign in to comment.