Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: modify phone number module core engine for more accurate results [ATLAS-194] #123

Merged
merged 4 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
34 changes: 1 addition & 33 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';
* 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 @@ -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
Expand All @@ -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 '';
};
Loading