Skip to content

Commit

Permalink
[chore]: back-merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
tarun-khanna committed Apr 9, 2024
2 parents d0260d8 + 2bc6bdd commit 184a776
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('detectCountryAndDialCodeFromPhone', () => {
countryCode: 'IN',
dialCode: '+91',
});
expect(detectCountryAndDialCodeFromPhone('60123456789')).toEqual({
expect(detectCountryAndDialCodeFromPhone('+60123456789')).toEqual({
countryCode: 'MY',
dialCode: '+60',
});
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
38 changes: 1 addition & 37 deletions packages/i18nify-js/src/modules/phoneNumber/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import PHONE_REGEX_MAPPER from './data/phoneRegexMapper.json';
* 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.
Expand Down Expand Up @@ -63,19 +62,6 @@ export const detectCountryAndDialCodeFromPhone = (
dialCode: '',
}
);
} else {
// If phone number doesn't start with '+', directly match against country regexes
for (const countryCode in regexMapper) {
const regex = new RegExp(regexMapper[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
Expand All @@ -89,25 +75,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 => {
const dialCodeMap = DIAL_CODE_MAPPER.dial_code_to_country as Record<
string,
CountryCodeType[]
>;
for (const dialCode in dialCodeMap) {
if (
dialCodeMap[dialCode].includes(
countryCode.toUpperCase() as CountryCodeType,
)
) {
return dialCode;
}
}
return '';
};

0 comments on commit 184a776

Please sign in to comment.