-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphone.js
More file actions
52 lines (43 loc) · 1.87 KB
/
phone.js
File metadata and controls
52 lines (43 loc) · 1.87 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
const formatPhone = (value, useParentheses = true) => {
if (!value) return 'Não informado';
const phone = String(value);
if (phone.length === 8) {
const isFourThousand =
phone.substring(0, 4) === '4004' || phone.substring(0, 4) === '4003';
if (isFourThousand)
return `${phone.substring(0, 4)} ${phone.substring(4, 8)}`;
return `${phone.substring(0, 4)}-${phone.substring(4, 8)}`;
}
if (phone.length === 9) {
return `${phone.substring(0, 5)}-${phone.substring(5)}`;
}
if (phone.length === 10) {
return useParentheses
? `(${phone.substring(0, 2)}) ${phone.substring(2, 6)}-${phone.substring(6)}`
: `${phone.substring(0, 2)} ${phone.substring(2, 6)}-${phone.substring(6)}`;
}
if (phone.length === 11) {
const isZeroEight = phone.substring(0, 4) === '0800';
if (isZeroEight)
return `${phone.substring(0, 4)} ${phone.substring(4, 7)} ${phone.substring(7)}`;
return useParentheses
? `(${phone.substring(0, 2)}) ${phone.substring(2, 7)}-${phone.substring(7)}`
: `${phone.substring(0, 2)} ${phone.substring(2, 7)}-${phone.substring(7)}`;
}
if (phone.length === 13) {
const isInternational = phone.startsWith('+55');
if (isInternational)
return useParentheses
? `${phone.substring(0, 3)} (${phone.substring(3, 5)}) ${phone.substring(5, 9)}-${phone.substring(9)}`
: `${phone.substring(0, 3)} ${phone.substring(3, 5)} ${phone.substring(5, 9)}-${phone.substring(9)}`;
}
if (phone.length === 14) {
const isInternational = phone.startsWith('+55');
if (isInternational)
return useParentheses
? `${phone.substring(0, 3)} (${phone.substring(3, 5)}) ${phone.substring(5, 10)}-${phone.substring(10)}`
: `${phone.substring(0, 3)} ${phone.substring(3, 5)} ${phone.substring(5, 10)}-${phone.substring(10)}`;
}
return phone;
};
module.exports = { formatPhone };