Repository navigation
Expand file tree
/
Copy pathLocalePhoneNumber.ts
More file actions
89 lines (70 loc) · 2.91 KB
/
Copy pathLocalePhoneNumber.ts
File metadata and controls
89 lines (70 loc) · 2.91 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {Str} from 'expensify-common';
import Onyx from 'react-native-onyx';
import {parsePhoneNumber} from './PhoneNumber';
let countryCodeByIPOnyx: number;
Onyx.connect({
key: ONYXKEYS.COUNTRY_CODE,
callback: (val) => (countryCodeByIPOnyx = val ?? 1),
});
/**
* 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
*/
function formatPhoneNumber(number: string): string {
if (!number) {
return '';
}
// eslint-disable-next-line no-param-reassign
number = number.replaceAll(' ', '\u00A0');
// do not parse the string, if it doesn't contain the SMS domain and it's not a phone number
if (number.indexOf(CONST.SMS.DOMAIN) === -1 && !CONST.REGEX.DIGITS_AND_PLUS.test(number)) {
return number;
}
const numberWithoutSMSDomain = Str.removeSMSDomain(number);
const parsedPhoneNumber = parsePhoneNumber(numberWithoutSMSDomain);
// return the string untouched if it's not a phone number
if (!parsedPhoneNumber.valid) {
if (parsedPhoneNumber.number?.international) {
return parsedPhoneNumber.number.international;
}
return numberWithoutSMSDomain;
}
const regionCode = parsedPhoneNumber.countryCode;
if (regionCode === countryCodeByIPOnyx) {
return parsedPhoneNumber.number.national;
}
return parsedPhoneNumber.number.international;
}
/**
* This is a TEMPORARY function to be used until we have migrated away from using Onyx.Connect
* 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
*/
function formatPhoneNumberWithCountryCode(number: string, countryCodeByIP: number): string {
if (!number) {
return '';
}
// eslint-disable-next-line no-param-reassign
number = number.replaceAll(' ', '\u00A0');
// do not parse the string, if it doesn't contain the SMS domain and it's not a phone number
if (number.indexOf(CONST.SMS.DOMAIN) === -1 && !CONST.REGEX.DIGITS_AND_PLUS.test(number)) {
return number;
}
const numberWithoutSMSDomain = Str.removeSMSDomain(number);
const parsedPhoneNumber = parsePhoneNumber(numberWithoutSMSDomain);
// return the string untouched if it's not a phone number
if (!parsedPhoneNumber.valid) {
if (parsedPhoneNumber.number?.international) {
return parsedPhoneNumber.number.international;
}
return numberWithoutSMSDomain;
}
const regionCode = parsedPhoneNumber.countryCode;
if (regionCode === countryCodeByIP) {
return parsedPhoneNumber.number.national;
}
return parsedPhoneNumber.number.international;
}
export {formatPhoneNumber, formatPhoneNumberWithCountryCode};