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

feat : Add Phone Number Module : [ATLAS-24] #1

Merged
merged 38 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e9973b5
add phone number module code
RgnDunes Oct 26, 2023
764e2b6
remove currency import
RgnDunes Oct 26, 2023
f664286
handle edge case
RgnDunes Oct 26, 2023
b9e0652
refactored regex
RgnDunes Oct 26, 2023
17445a0
revert: remove currency module
RgnDunes Oct 27, 2023
a6ee52e
feat: Add UT's for Phone number Module [ATLAS-23] (#4)
RgnDunes Nov 1, 2023
6a987f2
refactor code
RgnDunes Nov 1, 2023
e0ea2b0
refactor types
RgnDunes Nov 1, 2023
64dd4d9
refactor test cases
RgnDunes Nov 1, 2023
41b4c85
refactor test cases
RgnDunes Nov 1, 2023
2ef4db2
Merge origin/master
RgnDunes Nov 10, 2023
48a91c6
wrap phone functions with error boundries
RgnDunes Nov 10, 2023
636fa12
remove redundant file
RgnDunes Nov 10, 2023
d1cc70d
remove comment
RgnDunes Nov 16, 2023
2437533
Merge branch 'master' of github.com:razorpay/i18nify into phone-numbe…
tarun-khanna Nov 17, 2023
e30e571
add parsePhoneNumber
RgnDunes Nov 20, 2023
eab9c10
Merge remote-tracking branch 'origin/master' into phone-number-module
RgnDunes Nov 20, 2023
8bb554f
Merge remote-tracking branch 'origin/phone-number-module' into phone-…
RgnDunes Nov 20, 2023
b1a0544
refactor regex
RgnDunes Nov 20, 2023
a759eeb
refactor code
RgnDunes Nov 20, 2023
13f010a
extend country code support and refactor api contract for phone numbe…
RgnDunes Nov 27, 2023
4f7c6f7
rename validatePhoneNumber to isValidPhoneNumber
RgnDunes Nov 27, 2023
6f244b5
add UT
RgnDunes Nov 27, 2023
5612f03
resolve review comments
RgnDunes Nov 28, 2023
4659a99
revert yarn.lock changes
RgnDunes Nov 28, 2023
7f2e817
Merge remote-tracking branch 'origin/master' into phone-number-module
RgnDunes Nov 28, 2023
9472398
fix lint errors
RgnDunes Dec 5, 2023
1937131
change detectCountryCodeFromDialCode logic
RgnDunes Dec 5, 2023
937107a
fix test cases
RgnDunes Dec 5, 2023
bb04c19
fix lint issue
RgnDunes Dec 5, 2023
b394243
fix lint issue
RgnDunes Dec 5, 2023
f020356
fix lint issue
RgnDunes Dec 5, 2023
8149d33
fix lint issue
RgnDunes Dec 5, 2023
147ba3a
fix lint issue
RgnDunes Dec 5, 2023
5f87c74
resolve review comments
RgnDunes Dec 6, 2023
042af09
add console in error boundary
RgnDunes Dec 6, 2023
ade2656
remove ternary
RgnDunes Dec 6, 2023
a3af1fd
remove unused import
RgnDunes Dec 6, 2023
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
1 change: 1 addition & 0 deletions src/common/errorBoundary/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
* @param fn utility that is wrapped in error boundary
* @returns {Function} returns the function wrapped in try/catch block
*/
export const withErrorBoundary = <T extends (...args: unknown[]) => unknown>(

Check warning on line 21 in src/common/errorBoundary/index.ts

View workflow job for this annotation

GitHub Actions / Validate Source code

Unexpected any. Specify a different type

Check warning on line 21 in src/common/errorBoundary/index.ts

View workflow job for this annotation

GitHub Actions / Validate Source code

Unexpected any. Specify a different type
fn: T,
): ((...args: Parameters<T>) => ReturnType<T>) => {
return function (...rest: Parameters<T>) {
try {
return fn.call(this, ...rest);
} catch (err) {
console.warn('[I18N Error]: ', err);
// Currently, we are throwing the error as it is to consumers.
// In the future, this can be modified as per our requirement, like an error logging service.
throw new I18nifyError(err);
Expand Down
43 changes: 43 additions & 0 deletions src/modules/phoneNumber/__tests__/cleanPhoneNumber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { cleanPhoneNumber } from '../utils';

describe('cleanPhoneNumber', () => {
it('should remove non-numeric characters from a phone number', () => {
const phoneNumber = '+1 (123)-456-7890'; // Phone number with various non-numeric characters

const cleanedNumber = cleanPhoneNumber(phoneNumber);

expect(cleanedNumber).toBe('+11234567890'); // Expecting only numeric characters and '+' at the start
});

it('should handle an already clean phone number without non-numeric characters', () => {
const phoneNumber = '+9876543210'; // Clean phone number without non-numeric characters

const cleanedNumber = cleanPhoneNumber(phoneNumber);

expect(cleanedNumber).toBe('+9876543210'); // Expecting no change in the phone number
});

it('should handle an empty phone number string', () => {
const phoneNumber = '';

const cleanedNumber = cleanPhoneNumber(phoneNumber);

expect(cleanedNumber).toBe('');
});

it('should handle a phone number starting with non-numeric characters', () => {
const phoneNumber = 'abc+123456789'; // Phone number starting with non-numeric characters

const cleanedNumber = cleanPhoneNumber(phoneNumber);

expect(cleanedNumber).toBe('123456789'); // Expecting non-numeric characters at the start to be removed, will also remove + as it is not the starting character
});

it('should handle a phone number with only non-numeric characters', () => {
const phoneNumber = '---'; // Phone number with only non-numeric characters

const cleanedNumber = cleanPhoneNumber(phoneNumber);

expect(cleanedNumber).toBe('');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { detectCountryCodeFromDialCode } from '../utils';

describe('detectCountryCodeFromDialCode', () => {
it('should detect country code for valid phone numbers', () => {
expect(detectCountryCodeFromDialCode('+919876543210')).toBe('IN');
expect(detectCountryCodeFromDialCode('60123456789')).toBe('MY');
expect(detectCountryCodeFromDialCode('+12125551234')).toBe('US');
});

it('should throw an error for invalid phone numbers', () => {
expect(detectCountryCodeFromDialCode('123')).toBe('');
});
});
32 changes: 32 additions & 0 deletions src/modules/phoneNumber/__tests__/formatPhoneNumber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import formatPhoneNumber from '../formatPhoneNumber';

describe('formatPhoneNumber', () => {
it('should format an Indian phone number', () => {
const phoneNumber = '+917394926646';
const countryCode = 'IN';
const formatted = formatPhoneNumber(phoneNumber, countryCode);
expect(formatted).toBe('+91 7394 926646');
tarun-khanna marked this conversation as resolved.
Show resolved Hide resolved
});

it('should format a Malaysian phone number', () => {
const phoneNumber = '+60123456789';
const countryCode = 'MY';
const formatted = formatPhoneNumber(phoneNumber, countryCode);
expect(formatted).toBe('+60 12 34567 89');
});

it('should handle a invalid country code and detect it from phone number to format', () => {
const phoneNumber = '+917394926646';
const countryCode = 'XYZ';
const formatted = formatPhoneNumber(phoneNumber, countryCode);
expect(formatted).toBe('+91 7394 926646');
});

it('should handle a missing phoneNumber', () => {
const phoneNumber = '';
const countryCode = 'MY';
expect(() => formatPhoneNumber(phoneNumber, countryCode)).toThrow(
'Parameter `phoneNumber` is invalid!',
);
});
});
47 changes: 47 additions & 0 deletions src/modules/phoneNumber/__tests__/isValidPhoneNumber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import isValidPhoneNumber from '../isValidPhoneNumber';

describe('isValidPhoneNumber', () => {
const validTestDataSet = [
{ countryCode: 'IN', phoneNumber: '+917394926646' },
{ countryCode: 'MY', phoneNumber: '+60123456789' },
];

const invalidTestDataSet = [
{ countryCode: 'IN', phoneNumber: '1234' },
{ countryCode: 'MY', phoneNumber: '60123' },
];

validTestDataSet.map((dataset) => {
it(`should validate a valid phone number for ${dataset.countryCode}`, () => {
const isValid = isValidPhoneNumber(
dataset.phoneNumber,
dataset.countryCode,
);
expect(isValid).toBe(true);
});
});

invalidTestDataSet.map((dataset) => {
it(`should reject an invalid phone number for ${dataset.countryCode}`, () => {
const isValid = isValidPhoneNumber(
dataset.phoneNumber,
dataset.countryCode,
);
expect(isValid).toBe(false);
});
});

it('should handle a invalid country code and detect it from phone number to validate it', () => {
const phoneNumber = '1234567890';
const countryCode = 'XYZ';
const isValid = isValidPhoneNumber(phoneNumber, countryCode);
expect(isValid).toBe(true);
});

it('should handle a missing phoneNumber', () => {
const phoneNumber = '';
const countryCode = 'MY';
const isValid = isValidPhoneNumber(phoneNumber, countryCode);
expect(isValid).toBe(false);
});
});
39 changes: 39 additions & 0 deletions src/modules/phoneNumber/__tests__/parsePhoneNumber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import parsePhoneNumber from '../parsePhoneNumber';

describe('parsePhoneNumber function', () => {
it('should correctly parse a valid phone number with country code', () => {
const phoneNumber = '+15853042806';
const country = 'US';

const parsedInfo = parsePhoneNumber(phoneNumber, country);

expect(parsedInfo).toEqual({
countryCode: 'US',
dialCode: '+1',
formattedPhoneNumber: '+1 585-304-2806',
formatTemplate: 'xxx-xxx-xxxx',
});
});

it('should correctly parse a valid phone number without specifying country', () => {
const phoneNumber = '+447123456789';

const parsedInfo = parsePhoneNumber(phoneNumber);

expect(parsedInfo.countryCode).toBe('GB');
expect(parsedInfo.dialCode).toBe('+44');
expect(parsedInfo.formattedPhoneNumber).toBe('+44 7123 456 789');
expect(parsedInfo.formatTemplate).toBe('xxxx xxx xxx');
});

it('should return unformatted number for invalid phone number', () => {
const phoneNumber = '+1969123456789';

const parsedInfo = parsePhoneNumber(phoneNumber);

expect(parsedInfo.countryCode).toBe('');
expect(parsedInfo.dialCode).toBe('');
expect(parsedInfo.formattedPhoneNumber).toBe('+1969123456789');
expect(parsedInfo.formatTemplate).toBe('');
});
});
Loading
Loading