Skip to content

Commit

Permalink
[feat]: add new api for masked contact number
Browse files Browse the repository at this point in the history
  • Loading branch information
RgnDunes committed May 8, 2024
1 parent ef44e4f commit 4f1dccd
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
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');
});
});
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,
);
1 change: 1 addition & 0 deletions packages/i18nify-js/src/modules/phoneNumber/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { default as formatPhoneNumber } from './formatPhoneNumber';
export { default as parsePhoneNumber } from './parsePhoneNumber';
export { default as getDialCodes } from './getDialCodes';
export { default as getDialCodeByCountryCode } from './getDialCodeByCountryCode';
export { default as getMaskedPhoneNumber } from './getMaskedPhoneNumber';

0 comments on commit 4f1dccd

Please sign in to comment.