From 0b886467ad0fea252b5d0c33bc8595d2ec4c78e7 Mon Sep 17 00:00:00 2001 From: Kirtesh Dusane Date: Wed, 8 Nov 2023 18:37:26 +0530 Subject: [PATCH] X2-7706 feat(helpers): adds `formatPhoneNumber` helper function --- src/components/Utilities/Phone.jsx | 51 +++++++----------------------- src/helpers/phone.js | 45 ++++++++++++++++++++++++++ src/index.js | 1 + 3 files changed, 58 insertions(+), 39 deletions(-) create mode 100644 src/helpers/phone.js diff --git a/src/components/Utilities/Phone.jsx b/src/components/Utilities/Phone.jsx index ecba5b415..755b3f916 100644 --- a/src/components/Utilities/Phone.jsx +++ b/src/components/Utilities/Phone.jsx @@ -1,48 +1,21 @@ -import React from "react"; -import phoneLib from "google-libphonenumber"; -import PropTypes from "prop-types"; import clsx from "clsx"; - -const PNF = phoneLib.PhoneNumberFormat; -const phoneUtil = phoneLib.PhoneNumberUtil.getInstance(); +import PropTypes from "prop-types"; +import React from "react"; +import { formatPhoneNumber, getRegionCode } from "../../helpers/phone"; export const Phone = ({ countryCode = "US", className, children }) => { const number = children; - - try { - let phoneObject = phoneUtil.parseAndKeepRawInput(number, countryCode); - - const regionCode = phoneUtil.getRegionCodeForNumber(phoneObject); - if (regionCode && regionCode !== countryCode) { - // If the region code is different than what was passed in, reparse according to that format - phoneObject = phoneUtil.parseAndKeepRawInput(number, regionCode); - } - - // Parse number for display in the region's format - let formattedNumber; - if (regionCode) { - const format = regionCode === countryCode ? PNF.NATIONAL : PNF.INTERNATIONAL; - formattedNumber = phoneUtil.format(phoneObject, format); - } else { - // If we didn't detect a region, don't guess and return the original thing - formattedNumber = number; - } - - return ( - - {formattedNumber} - - ); - } catch {} + const formattedNumber = formatPhoneNumber(number, countryCode); + const regionCode = getRegionCode(number, countryCode); return ( - - {number} + + {formattedNumber} ); }; diff --git a/src/helpers/phone.js b/src/helpers/phone.js new file mode 100644 index 000000000..1c1ce173c --- /dev/null +++ b/src/helpers/phone.js @@ -0,0 +1,45 @@ +import phoneLib from "google-libphonenumber"; + +const PNF = phoneLib.PhoneNumberFormat; +const phoneUtil = phoneLib.PhoneNumberUtil.getInstance(); + +export const getRegionCode = (number, countryCode = "US") => { + let phoneObject = phoneUtil.parseAndKeepRawInput(number, countryCode); + + return phoneUtil.getRegionCodeForNumber(phoneObject); +}; + +/** + * Formats the Phone Number for provided country code + * + * @param number {string} + * @param countryCode {string} + * + * @return {string} + */ +export const formatPhoneNumber = (number, countryCode = "US") => { + try { + let phoneObject = phoneUtil.parseAndKeepRawInput(number, countryCode); + + const regionCode = phoneUtil.getRegionCodeForNumber(phoneObject); + if (regionCode && regionCode !== countryCode) { + // If the region code is different than what was passed in, reparse according to that format + phoneObject = phoneUtil.parseAndKeepRawInput(number, regionCode); + } + + // Parse number for display in the region's format + let formattedNumber; + + if (regionCode) { + const format = regionCode === countryCode ? PNF.NATIONAL : PNF.INTERNATIONAL; + formattedNumber = phoneUtil.format(phoneObject, format); + } else { + // If we didn't detect a region, don't guess and return the original thing + formattedNumber = number; + } + + return formattedNumber; + } catch { + return number; + } +}; diff --git a/src/index.js b/src/index.js index c6c31bb68..5b8c718a5 100644 --- a/src/index.js +++ b/src/index.js @@ -56,6 +56,7 @@ export { XolaLogoSimple } from "./images/XolaLogoSimple"; export { almostZero, numberFormat, roundNumber } from "./helpers/numbers"; export { getSymbol, isZeroDecimal } from "./helpers/currency"; export { formatDate, formatTime, dateFromObjectId } from "./helpers/date"; +export { formatPhoneNumber } from "./helpers/phone"; export { isOSX } from "./helpers/browser"; export { flash } from "./helpers/flash";