forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Expensify#33605 from barros001/patch-parse-phone-n…
…umber2 fix: treat certain US numbers as invalid
- Loading branch information
Showing
13 changed files
with
104 additions
and
13 deletions.
There are no files selected for viewing
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
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
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
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
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,43 @@ | ||
// eslint-disable-next-line no-restricted-imports | ||
import {parsePhoneNumber as originalParsePhoneNumber} from 'awesome-phonenumber'; | ||
import type {ParsedPhoneNumber, ParsedPhoneNumberInvalid, PhoneNumberParseOptions} from 'awesome-phonenumber'; | ||
import CONST from '@src/CONST'; | ||
|
||
/** | ||
* Wraps awesome-phonenumber's parsePhoneNumber function to handle the case where we want to treat | ||
* a US phone number that's technically valid as invalid. eg: +115005550009. | ||
* See https://github.com/Expensify/App/issues/28492 | ||
*/ | ||
function parsePhoneNumber(phoneNumber: string, options?: PhoneNumberParseOptions): ParsedPhoneNumber { | ||
const parsedPhoneNumber = originalParsePhoneNumber(phoneNumber, options); | ||
if (!parsedPhoneNumber.possible) { | ||
return parsedPhoneNumber; | ||
} | ||
|
||
const phoneNumberWithoutSpecialChars = phoneNumber.replace(CONST.REGEX.SPECIAL_CHARS_WITHOUT_NEWLINE, ''); | ||
if (!/^\+11[0-9]{10}$/.test(phoneNumberWithoutSpecialChars)) { | ||
return parsedPhoneNumber; | ||
} | ||
|
||
const countryCode = phoneNumberWithoutSpecialChars.substring(0, 2); | ||
const phoneNumberWithoutCountryCode = phoneNumberWithoutSpecialChars.substring(2); | ||
|
||
return { | ||
...parsedPhoneNumber, | ||
valid: false, | ||
possible: false, | ||
number: { | ||
...parsedPhoneNumber.number, | ||
|
||
// mimic the behavior of awesome-phonenumber | ||
e164: phoneNumberWithoutSpecialChars, | ||
international: `${countryCode} ${phoneNumberWithoutCountryCode}`, | ||
national: phoneNumberWithoutCountryCode, | ||
rfc3966: `tel:${countryCode}-${phoneNumberWithoutCountryCode}`, | ||
significant: phoneNumberWithoutCountryCode, | ||
}, | ||
} as ParsedPhoneNumberInvalid; | ||
} | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export {parsePhoneNumber}; |
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
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
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
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
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
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
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
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,43 @@ | ||
import {parsePhoneNumber} from '@libs/PhoneNumber'; | ||
|
||
describe('PhoneNumber', () => { | ||
describe('parsePhoneNumber', () => { | ||
it('Should return valid phone number', () => { | ||
const validNumbers = [ | ||
'+1 (234) 567-8901', | ||
'+12345678901', | ||
'+54 11 8765-4321', | ||
'+49 30 123456', | ||
'+44 20 8759 9036', | ||
'+34 606 49 95 99', | ||
' + 1 2 3 4 5 6 7 8 9 0 1', | ||
'+ 4 4 2 0 8 7 5 9 9 0 3 6', | ||
'+1 ( 2 3 4 ) 5 6 7 - 8 9 0 1', | ||
]; | ||
|
||
validNumbers.forEach((givenPhone) => { | ||
const parsedPhone = parsePhoneNumber(givenPhone); | ||
expect(parsedPhone.valid).toBe(true); | ||
expect(parsedPhone.possible).toBe(true); | ||
}); | ||
}); | ||
it('Should return invalid phone number if US number has extra 1 after country code', () => { | ||
const validNumbers = ['+1 1 (234) 567-8901', '+112345678901', '+115550123355', '+ 1 1 5 5 5 0 1 2 3 3 5 5']; | ||
|
||
validNumbers.forEach((givenPhone) => { | ||
const parsedPhone = parsePhoneNumber(givenPhone); | ||
expect(parsedPhone.valid).toBe(false); | ||
expect(parsedPhone.possible).toBe(false); | ||
}); | ||
}); | ||
it('Should return invalid phone number', () => { | ||
const invalidNumbers = ['+165025300001', 'John Doe', '123', '[email protected]']; | ||
|
||
invalidNumbers.forEach((givenPhone) => { | ||
const parsedPhone = parsePhoneNumber(givenPhone); | ||
expect(parsedPhone.valid).toBe(false); | ||
expect(parsedPhone.possible).toBe(false); | ||
}); | ||
}); | ||
}); | ||
}); |