From 2bc6bdde4c7293312cde11bcc4cea23f9a7caa8d Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Tue, 9 Apr 2024 13:50:38 +0530 Subject: [PATCH] fix: modify phone number module core engine for more accurate results [ATLAS-194] (#123) * [fix]: remove redundant function and modify detectCountryAndDialCodeFromPhone * [fix]: fix failing UTs due to previous commit * [fix]: fix failing e2e --- .../detectCountryAndDialCodeFromPhone.test.ts | 2 +- .../getDialCodeFromCountryCode.test.ts | 39 ------------------- .../__tests__/isValidPhoneNumber.spec.ts | 2 +- .../__tests__/isValidPhoneNumber.test.ts | 2 +- .../src/modules/phoneNumber/utils.ts | 34 +--------------- 5 files changed, 4 insertions(+), 75 deletions(-) delete mode 100644 packages/i18nify-js/src/modules/phoneNumber/__tests__/getDialCodeFromCountryCode.test.ts diff --git a/packages/i18nify-js/src/modules/phoneNumber/__tests__/detectCountryAndDialCodeFromPhone.test.ts b/packages/i18nify-js/src/modules/phoneNumber/__tests__/detectCountryAndDialCodeFromPhone.test.ts index ce4edb27..8528855c 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/__tests__/detectCountryAndDialCodeFromPhone.test.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/__tests__/detectCountryAndDialCodeFromPhone.test.ts @@ -6,7 +6,7 @@ describe('detectCountryAndDialCodeFromPhone', () => { countryCode: 'IN', dialCode: '+91', }); - expect(detectCountryAndDialCodeFromPhone('60123456789')).toEqual({ + expect(detectCountryAndDialCodeFromPhone('+60123456789')).toEqual({ countryCode: 'MY', dialCode: '+60', }); diff --git a/packages/i18nify-js/src/modules/phoneNumber/__tests__/getDialCodeFromCountryCode.test.ts b/packages/i18nify-js/src/modules/phoneNumber/__tests__/getDialCodeFromCountryCode.test.ts deleted file mode 100644 index 66a69149..00000000 --- a/packages/i18nify-js/src/modules/phoneNumber/__tests__/getDialCodeFromCountryCode.test.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { CountryCodeType } from '../../types'; -import { DIAL_CODE_MAPPER } from '../data/dialCodeMapper'; -import { getDialCodeFromCountryCode } from '../utils'; - -describe('getDialCodeFromCountryCode', () => { - it('should return the correct dial code for a valid country code', () => { - expect(getDialCodeFromCountryCode('US')).toBe('1'); - expect(getDialCodeFromCountryCode('GB')).toBe('44'); - expect(getDialCodeFromCountryCode('DE')).toBe('49'); - }); - - it('should return an empty string for an invalid country code', () => { - expect(getDialCodeFromCountryCode('XYZ' as CountryCodeType)).toBe(''); - }); - - it('should be case-insensitive', () => { - expect(getDialCodeFromCountryCode('us' as CountryCodeType)).toBe('1'); - expect(getDialCodeFromCountryCode('gb' as CountryCodeType)).toBe('44'); - }); - - it('should return the correct dial code for countries with shared codes', () => { - expect(getDialCodeFromCountryCode('CA')).toBe('1'); // Canada shares dial code 1 with the US and others - expect(getDialCodeFromCountryCode('RU')).toBe('7'); // Russia shares dial code 7 with Kazakhstan - }); - - it('should return an empty string for empty or whitespace-only input', () => { - expect(getDialCodeFromCountryCode('' as CountryCodeType)).toBe(''); - expect(getDialCodeFromCountryCode(' ' as CountryCodeType)).toBe(''); - }); - - it('should return a valid dial code for every country code in DIAL_CODE_MAPPER', () => { - Object.values(DIAL_CODE_MAPPER) - .flat() - .forEach((countryCode) => { - const dialCode = getDialCodeFromCountryCode(countryCode); - expect(dialCode).toMatch(/^\d+$/); - }); - }); -}); diff --git a/packages/i18nify-js/src/modules/phoneNumber/__tests__/isValidPhoneNumber.spec.ts b/packages/i18nify-js/src/modules/phoneNumber/__tests__/isValidPhoneNumber.spec.ts index 55e5c065..eda1e4aa 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/__tests__/isValidPhoneNumber.spec.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/__tests__/isValidPhoneNumber.spec.ts @@ -54,7 +54,7 @@ test.describe('isValidPhoneNumber', () => { `await isValidPhoneNumber('${phoneNumber}', '${countryCode}')`, ); - await assertScriptText(page, 'true'); + await assertScriptText(page, 'false'); }); test('should handle a missing phoneNumber', async ({ page }) => { diff --git a/packages/i18nify-js/src/modules/phoneNumber/__tests__/isValidPhoneNumber.test.ts b/packages/i18nify-js/src/modules/phoneNumber/__tests__/isValidPhoneNumber.test.ts index af2c0633..f15f48f5 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/__tests__/isValidPhoneNumber.test.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/__tests__/isValidPhoneNumber.test.ts @@ -35,7 +35,7 @@ describe('isValidPhoneNumber', () => { const phoneNumber = '1234567890'; const countryCode = 'XYZ'; const isValid = isValidPhoneNumber(phoneNumber, countryCode as any); - expect(isValid).toBe(true); + expect(isValid).toBe(false); }); it('should handle a missing phoneNumber', () => { diff --git a/packages/i18nify-js/src/modules/phoneNumber/utils.ts b/packages/i18nify-js/src/modules/phoneNumber/utils.ts index f9c39bf1..a2c3a5a1 100644 --- a/packages/i18nify-js/src/modules/phoneNumber/utils.ts +++ b/packages/i18nify-js/src/modules/phoneNumber/utils.ts @@ -9,8 +9,7 @@ import { PHONE_REGEX_MAPPER } from './data/phoneRegexMapper'; * and matches the leading digits with known dial codes mapped to countries. * - For matched dial codes, it further filters based on country-specific regex patterns * to validate the phone number format for those countries. - * - If the phone number doesn't start with '+', it directly matches the number - * against regular expressions associated with various countries to identify the code. + * - If the phone number doesn't start with '+', it returns empty strings as dialCode and countryCode * * @param phoneNumber The input phone number (string or number). * @returns The detected countryCode & dialCode or an empty strings in both if not found. @@ -55,19 +54,6 @@ export const detectCountryAndDialCodeFromPhone = ( dialCode: '', } ); - } else { - // If phone number doesn't start with '+', directly match against country regexes - for (const countryCode in PHONE_REGEX_MAPPER) { - const regex = PHONE_REGEX_MAPPER[countryCode as CountryCodeType]; - if (regex.test(phoneNumber.toString())) { - return { - countryCode: countryCode as CountryCodeType, - dialCode: getDialCodeFromCountryCode(countryCode as CountryCodeType) - ? `+${getDialCodeFromCountryCode(countryCode as CountryCodeType)}` - : '', - }; - } - } } // Return empty string if no country code is detected @@ -81,21 +67,3 @@ export const cleanPhoneNumber = (phoneNumber: string) => { const cleanedPhoneNumber = phoneNumber.replace(regex, ''); return phoneNumber[0] === '+' ? `+${cleanedPhoneNumber}` : cleanedPhoneNumber; }; - -/** - * Returns the dial code mapped for the country code passed from DIAL_CODE_MAPPER - */ -export const getDialCodeFromCountryCode = ( - countryCode: CountryCodeType, -): string => { - for (const dialCode in DIAL_CODE_MAPPER) { - if ( - DIAL_CODE_MAPPER[dialCode].includes( - countryCode.toUpperCase() as CountryCodeType, - ) - ) { - return dialCode; - } - } - return ''; -};