summaryrefslogtreecommitdiffstats
path: root/src/models
diff options
context:
space:
mode:
authorJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2019-01-25 09:48:19 +0100
committerJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2019-01-25 09:48:19 +0100
commitd982fcf753353236cbaa65a8715cbb4097d2e49f (patch)
treed56e3433ec5badd5a013335ca10290314f314306 /src/models
parent9b3d2929add7c1ab045b9bcf1d286fe55b7248f2 (diff)
Properly format displayName and validate fields
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
Diffstat (limited to 'src/models')
-rw-r--r--src/models/contact.js50
1 files changed, 38 insertions, 12 deletions
diff --git a/src/models/contact.js b/src/models/contact.js
index 2f877d2e..1240d3d2 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,49 @@ 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() {
+ 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
+ if (orderKey && n) {
+ 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(', ')
+ }
+ }
+ // otherwise the FN is enough
if (this.vCard.hasProperty('fn')) {
- return this.vCard.getFirstPropertyValue('fn')
+ return fn
}
- if (this.vCard.hasProperty('n')) {
- // reverse and join
- return this.vCard.getFirstPropertyValue('n')
- .filter(function(part) {
- return part
- })
- .join(' ')
+ // BUT if no FN property use the N anyway
+ if (n) {
+ // 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
}
- return null
+ return ''
+
}
/**