Skip to content

Commit

Permalink
Fix displayName checks and replace FN if empty on validate
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <[email protected]>
  • Loading branch information
skjnldsv committed Jan 25, 2019
1 parent d982fcf commit 1f86484
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/models/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,10 @@ export default class Contact {
const org = this.vCard.getFirstPropertyValue('org')

// if ordered by last or first name we need the N property
if (orderKey && n) {
// ! by checking the property we check for null AND empty string
// ! that means we can then check for empty array and be safe not to have
// ! 'xxxx'.join('') !== ''
if (orderKey && n && n.join('') !== '') {
switch (orderKey) {
case 'firstName':
// Stevenson;John;Philip,Paul;Dr.;Jr.,M.D.,A.C.P.
Expand All @@ -299,20 +302,19 @@ export default class Contact {
}
}
// otherwise the FN is enough
if (this.vCard.hasProperty('fn')) {
if (fn) {
return fn
}
// BUT if no FN property use the N anyway
if (n) {
if (n && n.join('') !== '') {
// Stevenson;John;Philip,Paul;Dr.;Jr.,M.D.,A.C.P.
// -> John Stevenson
return n.slice(0, 2).reverse().join(' ')
}
// LAST chance, use the org ir that's the only thing we have
if (org) {
// Stevenson;John;Philip,Paul;Dr.;Jr.,M.D.,A.C.P.
// -> John Stevenson
return org
if (org && org.join('') !== '') {
// org is supposed to be an array but is also used as plain string
return Array.isArray(org) ? org[0] : org
}
return ''

Expand Down
3 changes: 2 additions & 1 deletion src/services/checks/missingFN.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
export default {
name: 'missing FN',
run: contact => {
return !contact.vCard.hasProperty('fn')
return !contact.vCard.hasProperty('fn') // No FN
|| contact.vCard.getFirstPropertyValue('fn') === '' // Empty FN
},
fix: contact => {
if (contact.vCard.hasProperty('n')) {
Expand Down
4 changes: 2 additions & 2 deletions src/services/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export default function(contact) {
// A fix is needed, running ⏳
if (!check.fix(contact)) {
// FAILURE 🙅
console.warn('The following contact needed a correction that failed', check.name, contact)
console.warn('The following contact needed a correction that failed:', check.name, contact)
} else {
// SUCCESS 💪
console.info('The following contact has been repaired', check.name, contact)
console.info('The following contact has been repaired:', check.name, contact)
}
}
})
Expand Down

0 comments on commit 1f86484

Please sign in to comment.