phone-parser
Version:
Parse the phone input into an output format of your choice. Good for formatting a user input.
31 lines (28 loc) • 806 B
JavaScript
function phoneParser(input, format) {
// Trim away everything that's not a digit.
input = input.replace(/\D/g, '');
// Split to array for easier popping.
input = input.split('');
format = format.split('');
var output = '';
while (format.length) {
currentFormatSymbol = format.pop();
if (currentFormatSymbol === 'x') {
var digitToInsert = input.pop();
if (digitToInsert === undefined) break;
output = digitToInsert + output;
} else {
output = currentFormatSymbol + output;
}
}
if (input.length !== 0 || format.length !== 0) {
throw new Error('Phone format cannot be parsed.');
}
return output;
}
// Direct browser support.
if (typeof module === 'undefined') {
window.phoneParser = phoneParser;
} else {
module.exports = phoneParser;
}