UNPKG

@synotech/utils

Version:

a collection of utilities for internal use

21 lines (19 loc) 637 B
import { parsePhoneNumber } from 'libphonenumber-js'; /** * This function formats a phone number string to international format * @module phoneNumberFormatString * @param {string} numberString - a phone number string * @return {object} {String} a formatted phone number string * @example * * phoneNumberFormatString('+276925411223') // returns +27 692 541 1223 * */ export const phoneNumberFormatString = (numberString: string) => { const phoneNumber = parsePhoneNumber(numberString); if (phoneNumber && typeof numberString === 'string') { return phoneNumber?.formatInternational(); } else { return numberString; } };