From 04d4a45137e3ff666885a0303f29425993a5a41b Mon Sep 17 00:00:00 2001 From: allgandalf Date: Sat, 19 Jul 2025 01:15:01 +0530 Subject: [PATCH 1/2] add new function to take country code as input --- src/libs/LocalePhoneNumber.ts | 43 +++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/libs/LocalePhoneNumber.ts b/src/libs/LocalePhoneNumber.ts index 3e3e4c4e0def4..e93d2189d90df 100644 --- a/src/libs/LocalePhoneNumber.ts +++ b/src/libs/LocalePhoneNumber.ts @@ -4,10 +4,10 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import {parsePhoneNumber} from './PhoneNumber'; -let countryCodeByIP: number; +let countryCodeByIPOnyx: number; Onyx.connect({ key: ONYXKEYS.COUNTRY_CODE, - callback: (val) => (countryCodeByIP = val ?? 1), + callback: (val) => (countryCodeByIPOnyx = val ?? 1), }); /** @@ -39,6 +39,44 @@ function formatPhoneNumber(number: string): string { const regionCode = parsedPhoneNumber.countryCode; + if (regionCode === countryCodeByIPOnyx) { + return parsedPhoneNumber.number.national; + } + + return parsedPhoneNumber.number.international; +} + + +/** + * This is a TEMPORARY function to be used until we have migrated away from using Onyx.Connect + * Returns a locally converted phone number for numbers from the same region + * and an internationally converted phone number with the country code for numbers from other regions + */ +function formatPhoneNumberWithCountryCode(number: string, countryCodeByIP: number): string { + if (!number) { + return ''; + } + + // eslint-disable-next-line no-param-reassign + number = number.replace(/ /g, '\u00A0'); + + // do not parse the string, if it doesn't contain the SMS domain and it's not a phone number + if (number.indexOf(CONST.SMS.DOMAIN) === -1 && !CONST.REGEX.DIGITS_AND_PLUS.test(number)) { + return number; + } + const numberWithoutSMSDomain = Str.removeSMSDomain(number); + const parsedPhoneNumber = parsePhoneNumber(numberWithoutSMSDomain); + + // return the string untouched if it's not a phone number + if (!parsedPhoneNumber.valid) { + if (parsedPhoneNumber.number?.international) { + return parsedPhoneNumber.number.international; + } + return numberWithoutSMSDomain; + } + + const regionCode = parsedPhoneNumber.countryCode; + if (regionCode === countryCodeByIP) { return parsedPhoneNumber.number.national; } @@ -49,4 +87,5 @@ function formatPhoneNumber(number: string): string { export { // eslint-disable-next-line import/prefer-default-export formatPhoneNumber, + formatPhoneNumberWithCountryCode, }; From 28162f299a08adc6a98cb188f1b25fc27e5b91a5 Mon Sep 17 00:00:00 2001 From: allgandalf Date: Sat, 19 Jul 2025 01:16:53 +0530 Subject: [PATCH 2/2] fix prettier --- src/libs/LocalePhoneNumber.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/LocalePhoneNumber.ts b/src/libs/LocalePhoneNumber.ts index e93d2189d90df..c7750e031f489 100644 --- a/src/libs/LocalePhoneNumber.ts +++ b/src/libs/LocalePhoneNumber.ts @@ -46,7 +46,6 @@ function formatPhoneNumber(number: string): string { return parsedPhoneNumber.number.international; } - /** * This is a TEMPORARY function to be used until we have migrated away from using Onyx.Connect * Returns a locally converted phone number for numbers from the same region