diff options
author | John Molakvoæ <skjnldsv@users.noreply.github.com> | 2019-01-25 12:48:13 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-25 12:48:13 +0100 |
commit | 37434efc8ab1bc615c64d267c5a7b4296164a9d2 (patch) | |
tree | 44592ae63ef88482c284e4d2a8731b315bf6ced8 /src/models | |
parent | 9b3d2929add7c1ab045b9bcf1d286fe55b7248f2 (diff) | |
parent | 1f864843455282d4e4a02a46ad1560ed7abe27cf (diff) |
Merge pull request #880 from nextcloud/displayName-fix
Properly format displayName and fields validatation
Diffstat (limited to 'src/models')
-rw-r--r-- | src/models/contact.js | 54 |
1 files changed, 41 insertions, 13 deletions
diff --git a/src/models/contact.js b/src/models/contact.js index 2f877d2e..f12cef50 100644 --- a/src/models/contact.js +++ b/src/models/contact.js @@ -23,6 +23,8 @@ import uuid from 'uuid' import ICAL from 'ical.js' +import store from '../store' + export default class Contact { /** @@ -52,7 +54,7 @@ export default class Contact { // if no uid set, create one if (!this.vCard.hasProperty('uid')) { - console.debug('This contact did not have a proper uid. Setting a new one for ', this) + console.info('This contact did not have a proper uid. Setting a new one for ', this) this.vCard.addPropertyWithValue('uid', uuid()) } } @@ -271,25 +273,51 @@ export default class Contact { } /** - * Return the display name + * Formatted display name based on the order key * * @readonly * @memberof Contact - * @returns {string} the displayName */ get displayName() { - if (this.vCard.hasProperty('fn')) { - return this.vCard.getFirstPropertyValue('fn') + const orderKey = store.getters.getOrderKey + const n = this.vCard.getFirstPropertyValue('n') + const fn = this.vCard.getFirstPropertyValue('fn') + const org = this.vCard.getFirstPropertyValue('org') + + // if ordered by last or first name we need the N property + // ! 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. + // -> John Stevenson + return n.slice(0, 2).reverse().join(' ') + + case 'lastName': + // Stevenson;John;Philip,Paul;Dr.;Jr.,M.D.,A.C.P. + // -> Stevenson, John + return n.slice(0, 2).join(', ') + } } - if (this.vCard.hasProperty('n')) { - // reverse and join - return this.vCard.getFirstPropertyValue('n') - .filter(function(part) { - return part - }) - .join(' ') + // otherwise the FN is enough + if (fn) { + return fn } - return null + // BUT if no FN property use the N anyway + 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 && org.join('') !== '') { + // org is supposed to be an array but is also used as plain string + return Array.isArray(org) ? org[0] : org + } + return '' + } /** |