Skip to content

Instantly share code, notes, and snippets.

@Twoody
Last active October 16, 2019 23:36
Show Gist options
  • Save Twoody/6fd0b9e2f37d20cb60c8bf85d64f6a4f to your computer and use it in GitHub Desktop.
Save Twoody/6fd0b9e2f37d20cb60c8bf85d64f6a4f to your computer and use it in GitHub Desktop.
A phone scrubber for quick formatting
// Helper methods for dealing with phone numbers and phone related content (i.e. "tel:")
export default
{
/**
* Display a phone number as a given format
* @param {String, Int} pnum - A Phone number
* @param {Int} displayCase - The desired formmatting pattern
* @return {String} - If good phone number, properly displayed phone number; Else, empty string;
*/
display(__pnum, displayCase=1)
{
const phoneRE = /^(\d{3})(\d{3})(\d{4})$/
let pnum = this.scrub(__pnum)
let ret = ""
if (pnum === "")
{
// There was an error with the phone number
if (this.isDebugging())
{
console.log('ERROR: bad phone number: `' + __pnum + '`')
}
return ret
}
switch (displayCase)
{
case 1:
ret = pnum.replace(phoneRE, "$1.$2.$3")
break
case 2:
ret = pnum.replace(phoneRE, "($1)$2 $3")
break
case 3:
ret = pnum.replace(phoneRE, "($1)$2-$3")
break
case 4:
ret = pnum.replace(phoneRE, "$1-$2-$3")
break
case 5:
ret = pnum.replace(phoneRE, "$1 $2 $3")
break
case 6:
ret = pnum.replace(phoneRE, "$1$2$3")
break
default:
ret = pnum.replace(phoneRE, "$1.$2.$3")
break
}
return ret
},// end display()
/**
* Uniform process for displaying the href of a phone number
* @param {String, Int} pnum - A Phone number
* @param {Int} displayCase - The desired formmatting pattern
* @return {String} - If good phone number, properly displayed phone number; Else, empty string;
*/
getHref(pnum)
{
return "tel:" + this.scrub(pnum)
},
/**
* Quick function to turn on/off reporting to console;
* @return {Boolean} - If we are currently debugging this file or not;
*/
isDebugging()
{
return false
},
/**
* Strip a phone number to just its digits;
* @param {String, Int, Array} pnum -- A Phone number or list of phone numbers
* @return {String, Array} -- If good phone number, exactly ten character string; Else, empty string;
* If array, return list of stripped phone numbers with no duplicates;
*/
scrub(pnum)
{
// If array, clean every element and remove duplicates
if (Array.isArray(pnum))
{
let pnums = [];
for (let i = 0; i < pnum.length; i++)
{
pnums.push(this.scrub(pnum[i]));
}// end i-for
// Removing duplicates:
pnums = pnums.reduce(function(a,b)
{
if (a.indexOf(b) < 0 ) a.push(b);
return a;
}, []);
return pnums;
}
if (Number.isInteger(pnum))
{
pnum = pnum.toString()
}
// Get just the digits of our number
let ret = pnum.match(/\d+/g).map(Number).join("");
if (ret.length < 10)
{
if (this.isDebugging())
{
console.log('\tERROR: phone numbers less than ten digits not supported')
console.log('\t\tCurrent Length: ' + ret.length)
}
ret = ""
}
else if (ret.length > 10)
{
if (this.isDebugging())
{
// Might have a country code attached...
console.log('\tWARNING: pnum contains more than 10 digits; Just taking last ten digits;')
console.log('\t\tpnum: ' + pnum)
}
// Might have a country code attached...
// Just take the last ten digits;
ret = ret.slice(-10)
}
return ret
}// end scrub()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment