Here is a function that formats a phone number and adds a country code prefix if it is not included in it.
So, as an in input value it could be, for example, '23581010' or '4923581010' and in output it would become via the formatNumber function '(02) 358 10 10'.
Is the positioning of the let assignment ok in this code ? If something else feels not ok can you tell me ?
import { formatNumber } from 'libphonenumber-js';
export const formatGermanPhoneNumber = phoneNumber => {
if (!phoneNumber) {
return '';
}
const phoneNumberString = `${phoneNumber}`;
let prefix = '';
if (!phoneNumberString.startsWith('49')) {
prefix = '49';
}
return formatNumber(`+${prefix}${phoneNumberString}`, 'National');
};