-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
LocalePhoneNumber.js
53 lines (48 loc) · 1.55 KB
/
LocalePhoneNumber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import lodashGet from 'lodash/get';
import lodashTrim from 'lodash/trim';
import lodashIncludes from 'lodash/includes';
import lodashStartsWith from 'lodash/startsWith';
import Str from 'expensify-common/lib/str';
import translations from '../languages/translations';
/**
* 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);
}
return numString;
}
return number;
}
/**
* Returns an internationally converted phone number with the country code
*
* @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}`;
}
return number;
}
export {
toLocalPhone,
fromLocalPhone,
};