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
}
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
}