From ed16c0a0744c811c0e6182cf9d96941cc397b1eb Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Fri, 9 Aug 2013 17:18:12 +0200 Subject: Clean phone numbers, don’t leave them formatted. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- converter.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'converter.js') diff --git a/converter.js b/converter.js index 1c8ae81..f90fe59 100644 --- a/converter.js +++ b/converter.js @@ -32,6 +32,8 @@ function translateObjectToContact(inRec) { ValueError.prototype = new Error(); ValueError.prototype.constructor = ValueError; + const cleanPhoneNoRE = /[^0-9+]*/g; + /** * Find the proper record (or create new one) in the multi-value * attribute @@ -117,6 +119,14 @@ function translateObjectToContact(inRec) { } } + /** + * Firefox OS doesn’t recognize well formatted phone numbers, and + * works best with the plain number-only ones. + */ + function cleanPhoneNumber(inNo) { + return inNo.replace(cleanPhoneNoRE, ""); + } + for (var key in inRec) { if (["birthyear", "birthmonth", "birthday"].indexOf(key) !== -1) { // We have alternatively either whole date in birthyear field, @@ -159,14 +169,14 @@ function translateObjectToContact(inRec) { } else if (key === "facsimiletelephonenumber") { curRec = findSubElement("tel", "fax"); - curRec.value = inRec[key]; + curRec.value = cleanPhoneNumber(inRec[key]); } else if (key === "givenName") { contact.givenName = [inRec[key]]; } else if (key === "homePhone") { curRec = findSubElement("tel", "home"); - curRec.value = inRec[key]; + curRec.value = cleanPhoneNumber(inRec[key]); } else if (key === "l") { curRec = findSubElement("adr", "work"); @@ -182,7 +192,7 @@ function translateObjectToContact(inRec) { } else if (key === "mobile") { curRec = findSubElement("tel", "mobile"); - curRec.value = inRec[key]; + curRec.value = cleanPhoneNumber(inRec[key]); } else if (key === "mozillaHomeCountryName") { curRec = findSubElement("adr", "home"); @@ -275,7 +285,7 @@ function translateObjectToContact(inRec) { } else if (key === "telephoneNumber") { curRec = findSubElement("tel", "work"); - curRec.value = inRec[key]; + curRec.value = cleanPhoneNumber(inRec[key]); } else if (key === "title") { contact.jobTitle = [inRec[key]]; -- cgit