Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const getFormattedPhoneNumber = (cell, country = "US") => {
const phoneUtil = PhoneNumberUtil.getInstance();
// we return an empty string vs null when the phone number is inValid
// because when the cell is null, batch inserts into campaign contacts fail
// then when contacts have cell.length < 12 (+1), it's deleted before assignments are created
try {
const inputNumber = phoneUtil.parse(cell, country);
const isValid = phoneUtil.isValidNumber(inputNumber);
if (isValid) {
return phoneUtil.format(inputNumber, PhoneNumberFormat.E164);
}
return "";
} catch (e) {
log.error(e);
return "";
}
};
return formattedDate ? formattedDate : null;
case DataType.EMAIL:
if (this.isValidEmail(value)) {
return `${value}`.trim().toLowerCase();
} else {
return null;
}
case DataType.PHONE:
let phone: string = `${value}`.trim(); // trim
const phoneNumber: PhoneNumber = this.phoneUtil.parseAndKeepRawInput(phone, "US");
// check if valid, otherwise return null
if (!this.phoneUtil.isValidNumberForRegion(phoneNumber, "US")) {
logger.debug(`Phone ${phone} was invalid`);
return null;
} else {
phone = this.phoneUtil.format(phoneNumber, PhoneNumberFormat.E164);
logger.debug(`Returning formatted phone ${phone}`);
return phone;
}
case DataType.TIMESTAMP:
const formattedTimestamp: number = moment.utc(value).valueOf(); // UNIX with mills
return formattedTimestamp && !isNaN(formattedTimestamp) ? formattedTimestamp : null;
default:
logger.debug(`No compatible type so just returning raw value`); // should be impossible with Enum
return value;
}
}
function formatPhoneNumber(phoneNumber) {
const phoneNumberE164 = phoneNumberUtilInstance.format(phoneNumber, PhoneNumberFormat.E164);
return phoneNumberE164;
}
export function isE164NumberStrict(phoneNumber: string) {
const parsedPhoneNumber = phoneUtil.parse(phoneNumber)
if (!phoneUtil.isValidNumber(parsedPhoneNumber)) {
return false
}
return phoneUtil.format(parsedPhoneNumber, PhoneNumberFormat.E164) === phoneNumber
}
function formatPhoneNumber(phoneNumber: PhoneNumber) {
const phoneNumberE164: string = phoneNumberUtilInstance.format(phoneNumber, PhoneNumberFormat.E164);
return phoneNumberE164;
}
phone(v) {
const number = phoneUtil.parseAndKeepRawInput(v, v.substr(0, 1) === '+' ? null : 'FR');
if (!phoneUtil.isValidNumber(number)) {
return v;
}
return phoneUtil.format(number, PhoneNumberFormat.E164);
},
iban(v) {
return clean(areaCode + national, 'AR');
}
else
throw "Unknown area code for " + targetPhone;
}
else if (national[0] != '9') {
return clean('+549' + national, 'AR');
}
}
}
catch (error) {
throw "Can't parse number " + targetPhone + ": " + error;
}
if (tel) {
return phoneUtil.format(tel, PNF.E164);
}
else {
throw "Unknown error."
}
};