Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function prependToFormMobilePhoneNumber(
parsedNumber: PhoneNumber,
regionCode: string,
prefix: string
) {
if (phoneUtil.getNumberType(parsedNumber) === PhoneNumberType.MOBILE) {
return parsedNumber
}
let nationalNumber = phoneUtil.format(parsedNumber, PhoneNumberFormat.NATIONAL)
// Nationally formatted numbers sometimes contain leading 0
if (nationalNumber.charAt(0) === '0') {
nationalNumber = nationalNumber.slice(1)
}
// If the number already starts with prefix, don't prepend it again
if (nationalNumber.startsWith(prefix)) {
return null
}
const adjustedNumber = phoneUtil.parse(prefix + nationalNumber, regionCode)
return phoneUtil.getNumberType(adjustedNumber) === PhoneNumberType.MOBILE ? adjustedNumber : null
}
export const getDisplayPhoneNumber = (e164Number, country = "US") => {
const phoneUtil = PhoneNumberUtil.getInstance();
const parsed = phoneUtil.parse(e164Number, country);
return phoneUtil.format(parsed, PhoneNumberFormat.NATIONAL);
};
const number = phoneUtil.parse(n, countryCode),
isPossibleNumber = phoneUtil.isPossibleNumber(number),
res = {
input: n,
countryCode: countryCode,
isPossibleNumber: isPossibleNumber,
isPossibleNumberWithReason: phoneUtil.isPossibleNumberWithReason(number)
};
if(isPossibleNumber){
res.isPossibleNumber = true;
res.isNumberValid = phoneUtil.isValidNumber(number);
res.countryCode = countryCode;
res.formatted = phoneUtil.formatInOriginalFormat(number, countryCode);
res.national = phoneUtil.format(number, PNF.NATIONAL);
res.international = phoneUtil.format(number, PNF.INTERNATIONAL)
}
switch (phoneUtil.isPossibleNumberWithReason(number)){
case PNV.IS_POSSIBLE:
res.isPossibleNumberWithReason = 'IS_POSSIBLE';
break;
case PNV.INVALID_COUNTRY_CODE:
res.isPossibleNumberWithReason = 'INVALID_COUNTRY_CODE';
break;
case PNV.TOO_SHORT:
res.isPossibleNumberWithReason = 'TOO_SHORT';
break;