-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat]: add new api for masked contact number
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
packages/i18nify-js/src/modules/phoneNumber/__tests__/getMaskedPhoneNumber.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { CountryCodeType } from '../../types'; | ||
import { getMaskedPhoneNumber } from '../index'; | ||
|
||
describe('phoneNumber - getMaskedPhoneNumber', () => { | ||
// Test data array for multiple countries | ||
const testCasesWithDialCode = [ | ||
{ countryCode: 'US', expected: '+1 xxx-xxx-xxxx' }, | ||
{ countryCode: 'GB', expected: '+44 xxxx xxx xxx' }, | ||
{ countryCode: 'DE', expected: '+49 xxx xxxxxxxx' }, | ||
{ countryCode: 'IN', expected: '+91 xxxx xxxxxx' }, | ||
{ countryCode: 'JP', expected: '+81 xx xxxx xxxx' }, | ||
]; | ||
|
||
// Tests for valid inputs including dial code | ||
testCasesWithDialCode.forEach(({ countryCode, expected }) => { | ||
it(`should return the correct phone number format including dial code for ${countryCode}`, () => { | ||
const result = getMaskedPhoneNumber(countryCode as CountryCodeType, true); | ||
expect(result).toBe(expected); | ||
}); | ||
}); | ||
|
||
// Tests for valid inputs without dial code | ||
testCasesWithDialCode.forEach(({ countryCode, expected }) => { | ||
it(`should return the correct phone number format without dial code for ${countryCode}`, () => { | ||
const result = getMaskedPhoneNumber( | ||
countryCode as CountryCodeType, | ||
false, | ||
); | ||
// Remove the dial code and leading space from the expected string | ||
const expectedWithoutDialCode = expected.substring( | ||
expected.indexOf(' ') + 1, | ||
); | ||
expect(result).toBe(expectedWithoutDialCode); | ||
}); | ||
}); | ||
|
||
// Test for invalid country code | ||
it('should throw an error for an invalid country code', () => { | ||
expect(() => { | ||
// @ts-expect-error null is not a valid country code | ||
getMaskedPhoneNumber('XYZ', true); | ||
}).toThrow('Parameter "countryCode" is invalid: XYZ'); | ||
}); | ||
|
||
// Test for missing country code | ||
it('should throw an error when country code is undefined', () => { | ||
expect(() => { | ||
// @ts-expect-error null is not a valid country code | ||
getMaskedPhoneNumber(undefined, true); | ||
}).toThrow('Parameter "countryCode" is invalid: undefined'); | ||
}); | ||
|
||
// Test for null country code | ||
it('should throw an error when country code is null', () => { | ||
expect(() => { | ||
// @ts-expect-error null is not a valid country code | ||
getMaskedPhoneNumber(null, true); | ||
}).toThrow('Parameter "countryCode" is invalid: null'); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
packages/i18nify-js/src/modules/phoneNumber/getMaskedPhoneNumber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import getDialCodeByCountryCode from './getDialCodeByCountryCode'; | ||
import { withErrorBoundary } from '../../common/errorBoundary'; | ||
import PHONE_FORMATTER_MAPPER from './data/phoneFormatterMapper.json'; | ||
import { CountryCodeType } from '../types'; | ||
|
||
/** | ||
* Returns a masked phone number based on the country code. | ||
* It uses predefined mappings to format phone numbers according to the country standards. | ||
* | ||
* @param countryCode The ISO 3166-1 alpha-2 country code. | ||
* @param withDialCode A boolean indicating whether to include the country's dial code in the result. It has a default value of "true" | ||
* @returns The masked phone number as a string. | ||
*/ | ||
const getMaskedPhoneNumber = ( | ||
countryCode: CountryCodeType, | ||
withDialCode: boolean = true, | ||
): string => { | ||
// Throw errors if countryCode is invalid | ||
if (!countryCode) | ||
throw new Error(`Parameter "countryCode" is invalid: ${countryCode}`); | ||
|
||
// Retrieve the template for the given country code | ||
const formattingTemplate = PHONE_FORMATTER_MAPPER[countryCode]; | ||
|
||
// Check if the country code is valid and a template exists | ||
if (!formattingTemplate) { | ||
throw new Error(`Parameter "countryCode" is invalid: ${countryCode}`); | ||
} | ||
|
||
// If including the dial code, prepend it to the template with a space | ||
if (withDialCode) { | ||
const dialCode = getDialCodeByCountryCode(countryCode); | ||
return `${dialCode} ${formattingTemplate}`; | ||
} | ||
|
||
// Return the template directly if not including the dial code | ||
return formattingTemplate; | ||
}; | ||
|
||
export default withErrorBoundary<typeof getMaskedPhoneNumber>( | ||
getMaskedPhoneNumber, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters