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

Update the merge strategy #844

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 13 additions & 4 deletions src/helpers/mergeContact.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export function mergeContact(contact, attributes) {
const strategies = {
email: mergeArrayOfObject('address'),
phone: mergeArrayOfObject('number'),
givenName: keepLonger,
familyName: keepLonger
name: completeObject
}

const fromStrategies = strategies => (objValue, srcValue, key) =>
Expand All @@ -26,8 +25,18 @@ function mergeArrayOfObject(key) {
}
}

function keepLonger(objValue = '', srcValue = '') {
return objValue.length > srcValue.length ? objValue : srcValue
function completeObject(objValue = '', srcValue = '') {
if (!objValue) {
return srcValue
}

Object.keys(srcValue).forEach(key => {
if (!objValue[key]) {
objValue[key] = srcValue[key]
}
})

return objValue
}

/**
Expand Down
31 changes: 27 additions & 4 deletions src/helpers/mergeContact.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('Merge contacts', () => {
],
address: [{ formattedAddress: 'Westeros', primary: true, type: 'Work' }]
}

expect(mergeContact(fakeContact, importedContact)).toEqual(expectedContact)
})

Expand Down Expand Up @@ -52,23 +53,45 @@ describe('Merge contacts', () => {
expect(mergeContact(fakeContact, importedContact)).toEqual(expectedContact)
})

test('should merge name only if it is longer', () => {
test('should merge properties', () => {
const fakeContact = {
email: [{ address: 'starck@localhost' }],
name: { familyName: 'Starck', givenName: 'N' },
name: { familyName: 'Starck' },
phone: [{ number: '0987654321', primary: true, type: 'Work' }],
address: [{ formattedAddress: 'Westeros', primary: true, type: 'Work' }]
}
const importedContact = {
name: { familyName: 'S.', givenName: 'Ned' },
name: { familyName: 'Starck', givenName: 'Ned' },
phone: [{ number: '0123456789' }],
email: [{ address: 'ned@localhost' }]
}
const expectedContact = {
email: [{ address: 'starck@localhost' }, { address: 'ned@localhost' }],
name: { familyName: 'Starck', givenName: 'Ned' },
phone: [{ number: '0987654321', primary: true, type: 'Work' }],
phone: [
{ number: '0987654321', primary: true, type: 'Work' },
{ number: '0123456789' }
],
address: [{ formattedAddress: 'Westeros', primary: true, type: 'Work' }]
}

expect(mergeContact(fakeContact, importedContact)).toEqual(expectedContact)
})

test('should keeps the present fields', () => {
const fakeContact = {
name: { familyName: 'Starck', givenName: 'Ned' },
email: [{ address: 'ned@localhost' }]
}
const importedContact = {
name: { familyName: 'Ned Starck' },
address: [{ formattedAddress: 'Westeros' }]
}
const expectedContact = {
name: { familyName: 'Starck', givenName: 'Ned' },
email: [{ address: 'ned@localhost' }],
address: [{ formattedAddress: 'Westeros' }]
}
const mergedContact = mergeContact(fakeContact, importedContact)
expect(mergedContact).toEqual(expectedContact)
})
Expand Down