-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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 #16804 from koko57/feat/16099-format-the-phone-num…
…bers feat: create formatPhoneNumber util function
- Loading branch information
Showing
22 changed files
with
189 additions
and
179 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,53 +1,73 @@ | ||
import lodashGet from 'lodash/get'; | ||
import lodashTrim from 'lodash/trim'; | ||
import lodashIncludes from 'lodash/includes'; | ||
import lodashStartsWith from 'lodash/startsWith'; | ||
import Onyx from 'react-native-onyx'; | ||
import Str from 'expensify-common/lib/str'; | ||
import translations from '../languages/translations'; | ||
import {parsePhoneNumber} from 'awesome-phonenumber'; | ||
import ONYXKEYS from '../ONYXKEYS'; | ||
|
||
/** | ||
* Returns a locally converted phone number without the country code | ||
* | ||
* @param {String} locale eg 'en', 'es-ES' | ||
* @param {String} number | ||
* @returns {String} | ||
*/ | ||
function toLocalPhone(locale, number) { | ||
const numString = lodashTrim(number); | ||
const withoutPlusNum = lodashIncludes(numString, '+') ? Str.cutBefore(numString, '+') : numString; | ||
const country = lodashGet(translations, [locale, 'phoneCountryCode']); | ||
|
||
if (country) { | ||
if (lodashStartsWith(withoutPlusNum, country)) { | ||
return Str.cutBefore(withoutPlusNum, country); | ||
let currentUserEmail; | ||
Onyx.connect({ | ||
key: ONYXKEYS.SESSION, | ||
callback: (val) => { | ||
// When signed out, val is undefined | ||
if (!val) { | ||
return; | ||
} | ||
return numString; | ||
} | ||
return number; | ||
} | ||
|
||
currentUserEmail = val.email; | ||
}, | ||
}); | ||
|
||
let currentUserPersonalDetails; | ||
Onyx.connect({ | ||
key: ONYXKEYS.PERSONAL_DETAILS, | ||
callback: (val) => { | ||
currentUserPersonalDetails = lodashGet(val, currentUserEmail, {}); | ||
}, | ||
}); | ||
|
||
let countryCodeByIP; | ||
Onyx.connect({ | ||
key: ONYXKEYS.COUNTRY_CODE, | ||
callback: val => countryCodeByIP = val || 1, | ||
}); | ||
|
||
/** | ||
* Returns an internationally converted phone number with the country code | ||
* Returns a locally converted phone number for numbers from the same region | ||
* and an internationally converted phone number with the country code for numbers from other regions | ||
* | ||
* @param {String} locale eg 'en', 'es-ES' | ||
* @param {String} number | ||
* @returns {String} | ||
*/ | ||
function fromLocalPhone(locale, number) { | ||
const numString = lodashTrim(number); | ||
const withoutPlusNum = lodashIncludes(numString, '+') ? Str.cutBefore(numString, '+') : numString; | ||
const country = lodashGet(translations, [locale, 'phoneCountryCode']); | ||
|
||
if (country) { | ||
if (lodashStartsWith(withoutPlusNum, country)) { | ||
return `+${withoutPlusNum}`; | ||
} | ||
return `+${country}${withoutPlusNum}`; | ||
function formatPhoneNumber(number) { | ||
const parsedPhoneNumber = parsePhoneNumber(Str.removeSMSDomain(number)); | ||
|
||
// return the string untouched if it's not a phone number | ||
if (!parsedPhoneNumber.valid) { | ||
return number; | ||
} | ||
|
||
let signedInUserCountryCode; | ||
|
||
/** | ||
* if there is a phone number saved in the user's personal details we format the other numbers depending on | ||
* the phone number's country code, otherwise we use country code based on the user's IP | ||
*/ | ||
if (currentUserPersonalDetails.phoneNumber) { | ||
signedInUserCountryCode = parsePhoneNumber(currentUserPersonalDetails.phoneNumber).countryCode; | ||
} else { | ||
signedInUserCountryCode = countryCodeByIP; | ||
} | ||
return number; | ||
|
||
const regionCode = parsedPhoneNumber.countryCode; | ||
|
||
if (regionCode === signedInUserCountryCode) { | ||
return parsedPhoneNumber.number.national; | ||
} | ||
|
||
return parsedPhoneNumber.number.international; | ||
} | ||
|
||
export { | ||
toLocalPhone, | ||
fromLocalPhone, | ||
// eslint-disable-next-line import/prefer-default-export | ||
formatPhoneNumber, | ||
}; |
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
Oops, something went wrong.