Skip to content

Commit 89975f9

Browse files
authored
Merge pull request #98 from silvioprog/master
feat: add functions isValidLandlinePhone() and isValidMobilePhone()
2 parents 2c6b8b1 + 3394479 commit 89975f9

8 files changed

Lines changed: 131 additions & 8 deletions

File tree

.all-contributorsrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@
209209
"doc",
210210
"test"
211211
]
212+
},
213+
{
214+
"login": "silvioprog",
215+
"name": "Silvio Clécio",
216+
"avatar_url": "https://avatars0.githubusercontent.com/u/1456829?v=4",
217+
"profile": "http://fb.com/silvioclecio",
218+
"contributions": [
219+
"code",
220+
"doc",
221+
"test"
222+
]
212223
}
213224
],
214225
"repoType": "github",

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Our "thank you" goes to these wonderful people ([emoji key](https://github.com/k
5050
<td align="center"><a href="https://www.linkedin.com/in/felipe-nolleto-nascimento-a2a23788/"><img src="https://avatars2.githubusercontent.com/u/2437673?v=4" width="100px;" alt=""/><br /><sub><b>Felipe Nolleto Nascimento</b></sub></a><br /><a href="https://github.com/brazilian-utils/brazilian-utils/commits?author=nolleto" title="Code">💻</a> <a href="https://github.com/brazilian-utils/brazilian-utils/commits?author=nolleto" title="Documentation">📖</a> <a href="https://github.com/brazilian-utils/brazilian-utils/commits?author=nolleto" title="Tests">⚠️</a></td>
5151
<td align="center"><a href="http://linkedin.com/in/saulojoab"><img src="https://avatars2.githubusercontent.com/u/37988252?v=4" width="100px;" alt=""/><br /><sub><b>Saulo Joab</b></sub></a><br /><a href="https://github.com/brazilian-utils/brazilian-utils/commits?author=saulojoab" title="Documentation">📖</a></td>
5252
<td align="center"><a href="http://fb.com/arantespp"><img src="https://avatars0.githubusercontent.com/u/16626980?v=4" width="100px;" alt=""/><br /><sub><b>Pedro Arantes</b></sub></a><br /><a href="https://github.com/brazilian-utils/brazilian-utils/commits?author=arantespp" title="Code">💻</a> <a href="https://github.com/brazilian-utils/brazilian-utils/commits?author=arantespp" title="Documentation">📖</a> <a href="https://github.com/brazilian-utils/brazilian-utils/commits?author=arantespp" title="Tests">⚠️</a></td>
53+
<td align="center"><a href="http://fb.com/silvioprog"><img src="https://avatars0.githubusercontent.com/u/1456829?v=4" width="100px;" alt=""/><br /><sub><b>Silvio Clécio</b></sub></a><br /><a href="https://github.com/brazilian-utils/brazilian-utils/commits?author=silvioprog" title="Code">💻</a> <a href="https://github.com/brazilian-utils/brazilian-utils/commits?author=silvioprog" title="Documentation">📖</a> <a href="https://github.com/brazilian-utils/brazilian-utils/commits?author=silvioprog" title="Tests">⚠️</a></td>
5354
</tr>
5455
</table>
5556

docs/pt-br/utilities.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,34 @@ isValidEmail('john.doe@hotmail.com'); // true
7676

7777
## isValidPhone
7878

79-
Valida se o numero de telefone (celular ou residencial) é valido.
79+
Valida se o número de telefone (celular ou residencial) é valido.
8080

8181
```javascript
8282
import { isValidPhone } from '@brazilian-utils/brazilian-utils';
8383

8484
isValidPhone('11900000000'); // true
8585
```
8686

87+
## isValidMobilePhone
88+
89+
Valida se o número de telefone celular é valido.
90+
91+
```javascript
92+
import { isValidMobilePhone } from '@brazilian-utils/brazilian-utils';
93+
94+
isValidMobilePhone('11900000000'); // true
95+
```
96+
97+
## isValidLandlinePhone
98+
99+
Valida se o número de telefone residencial é valido.
100+
101+
```javascript
102+
import { isValidLandlinePhone } from '@brazilian-utils/brazilian-utils';
103+
104+
isValidLandlinePhone('1130000000'); // true
105+
```
106+
87107
## isValidPIS
88108

89109
Valida se o PIS é válido.

docs/utilities.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ import { isValidPhone } from '@brazilian-utils/brazilian-utils';
8484
isValidPhone('11900000000'); // true
8585
```
8686

87+
## isValidMobilePhone
88+
89+
Check if mobile phone number is valid.
90+
91+
```javascript
92+
import { isValidMobilePhone } from '@brazilian-utils/brazilian-utils';
93+
94+
isValidMobilePhone('11900000000'); // true
95+
```
96+
97+
## isValidLandlinePhone
98+
99+
Check if landline phone number is valid.
100+
101+
```javascript
102+
import { isValidLandlinePhone } from '@brazilian-utils/brazilian-utils';
103+
104+
isValidLandlinePhone('1130000000'); // true
105+
```
106+
87107
## isValidPIS
88108

89109
Check if PIS is valid.

src/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ describe('Public API', () => {
1717
'generateCPF',
1818
'isValidEmail',
1919
'isValidPhone',
20+
'isValidMobilePhone',
21+
'isValidLandlinePhone',
2022
'generateCNPJ',
2123
'formatBoleto',
2224
'isValidBoleto',

src/utilities/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export { isValid as isValidIE } from './inscricao-estadual';
22
export { isValid as isValidPIS } from './pis';
3-
export { isValid as isValidPhone } from './phone';
3+
export { isValid as isValidPhone, isValidMobilePhone, isValidLandlinePhone } from './phone';
44
export { isValid as isValidEmail } from './email';
55
export { format as formatPJ } from './processo-juridico';
66
export { format as formatCEP, isValid as isValidCEP } from './cep';

src/utilities/phone/index.test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,83 @@
1-
import { isValid, VALID_AREA_CODES, PHONE_MIN_LENGTH, PHONE_MAX_LENGTH } from '.';
1+
import {
2+
isValid,
3+
isValidMobilePhone,
4+
isValidLandlinePhone,
5+
VALID_AREA_CODES,
6+
PHONE_MIN_LENGTH,
7+
PHONE_MAX_LENGTH,
8+
} from '.';
29

310
describe('isValid', () => {
411
describe('should return false', () => {
512
test('when is a empty string', () => {
613
expect(isValid('')).toBe(false);
14+
expect(isValidMobilePhone('')).toBe(false);
15+
expect(isValidLandlinePhone('')).toBe(false);
716
});
817

918
test('when is null', () => {
1019
expect(isValid(null as any)).toBe(false);
20+
expect(isValidMobilePhone(null as any)).toBe(false);
21+
expect(isValidLandlinePhone(null as any)).toBe(false);
1122
});
1223

1324
test('when is undefined', () => {
1425
expect(isValid(undefined as any)).toBe(false);
26+
expect(isValidMobilePhone(undefined as any)).toBe(false);
27+
expect(isValidLandlinePhone(undefined as any)).toBe(false);
1528
});
1629

1730
test('when is a boolean', () => {
1831
expect(isValid(true as any)).toBe(false);
1932
expect(isValid(false as any)).toBe(false);
33+
expect(isValidMobilePhone(false as any)).toBe(false);
34+
expect(isValidLandlinePhone(true as any)).toBe(false);
35+
expect(isValidMobilePhone(false as any)).toBe(false);
36+
expect(isValidLandlinePhone(true as any)).toBe(false);
2037
});
2138

2239
test('when is a object', () => {
2340
expect(isValid({} as any)).toBe(false);
41+
expect(isValidMobilePhone({} as any)).toBe(false);
42+
expect(isValidLandlinePhone({} as any)).toBe(false);
2443
});
2544

2645
test('when is a array', () => {
2746
expect(isValid([] as any)).toBe(false);
47+
expect(isValidMobilePhone([] as any)).toBe(false);
48+
expect(isValidLandlinePhone([] as any)).toBe(false);
2849
});
2950

3051
test('when is a mobile phone with mask and code state invalid', () => {
3152
expect(isValid('(00) 3 0000-0000')).toBe(false);
53+
expect(isValidMobilePhone('(00) 3 0000-0000')).toBe(false);
3254
});
3355

3456
test('when is a landline with mask and code state invalid', () => {
3557
expect(isValid('(11) 9000-0000')).toBe(false);
58+
expect(isValidLandlinePhone('(11) 9000-0000')).toBe(false);
3659
});
3760

3861
test('when is a mobile phone invalid with mask', () => {
3962
expect(isValid('(11) 3 0000-0000')).toBe(false);
63+
expect(isValidMobilePhone('(11) 3 0000-0000')).toBe(false);
4064
});
4165

4266
test('when is a landline invalid with mask', () => {
4367
expect(isValid('(11) 9000-0000')).toBe(false);
68+
expect(isValidLandlinePhone('(11) 9000-0000')).toBe(false);
4469
});
4570

4671
test(`when dont match with phone min length (${PHONE_MIN_LENGTH})`, () => {
4772
expect(isValid('11')).toBe(false);
73+
expect(isValidMobilePhone('11')).toBe(false);
74+
expect(isValidLandlinePhone('11')).toBe(false);
4875
});
4976

5077
test(`when dont match with phone max length (${PHONE_MAX_LENGTH})`, () => {
5178
expect(isValid('11300000001130000000')).toBe(false);
79+
expect(isValidMobilePhone('11300000001130000000')).toBe(false);
80+
expect(isValidLandlinePhone('11300000001130000000')).toBe(false);
5281
});
5382
});
5483

@@ -59,18 +88,22 @@ describe('isValid', () => {
5988

6089
test('when is a mobile phone valid with mask', () => {
6190
expect(isValid('(11) 9 0000-0000')).toBe(true);
91+
expect(isValidMobilePhone('(11) 9 0000-0000')).toBe(true);
6292
});
6393

6494
test('when is a landline valid with mask', () => {
6595
expect(isValid('(11) 3000-0000')).toBe(true);
96+
expect(isValidLandlinePhone('(11) 3000-0000')).toBe(true);
6697
});
6798

6899
test('when is a mobile phone valid without mask', () => {
69100
expect(isValid('11900000000')).toBe(true);
101+
expect(isValidMobilePhone('11900000000')).toBe(true);
70102
});
71103

72104
test('when is a landline valid without mask', () => {
73105
expect(isValid('1130000000')).toBe(true);
106+
expect(isValidLandlinePhone('1130000000')).toBe(true);
74107
});
75108
});
76109
});

src/utilities/phone/index.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,56 @@ export function isValidDDD(phone: string): boolean {
1818
return VALID_AREA_CODES.includes(Number(phone.substr(0, 2)));
1919
}
2020

21+
export function isValidMobilePhoneLength(phone: string): boolean {
22+
return phone.length === PHONE_MAX_LENGTH;
23+
}
24+
25+
export function isValidLandlinePhoneLength(phone: string): boolean {
26+
return phone.length >= PHONE_MIN_LENGTH && phone.length < PHONE_MAX_LENGTH;
27+
}
28+
2129
export function isValidLength(phone: string): boolean {
22-
return phone.length >= PHONE_MIN_LENGTH && phone.length <= PHONE_MAX_LENGTH;
30+
return isValidLandlinePhoneLength(phone) || isValidMobilePhoneLength(phone);
31+
}
32+
33+
export function isValidMobilePhoneFirstNumber(phone: string): boolean {
34+
return MOBILE_VALID_FIRST_NUMBERS.includes(Number(phone.charAt(2)));
35+
}
36+
37+
export function isValidLandlinePhoneFirstNumber(phone: string): boolean {
38+
return LANDLINE_VALID_FIRST_NUMBERS.includes(Number(phone.charAt(2)));
2339
}
2440

2541
export function isValidFirstNumber(phone: string): boolean {
2642
return phone.length === PHONE_MIN_LENGTH
27-
? LANDLINE_VALID_FIRST_NUMBERS.includes(Number(phone.charAt(2)))
28-
: MOBILE_VALID_FIRST_NUMBERS.includes(Number(phone.charAt(2)));
43+
? isValidLandlinePhoneFirstNumber(phone)
44+
: isValidMobilePhoneFirstNumber(phone);
45+
}
46+
47+
function parsePhoneDigits(phone: string): { isValidDigits: boolean; digits: string } {
48+
return { isValidDigits: !!phone && typeof phone === 'string', digits: onlyNumbers(phone) };
49+
}
50+
51+
export function isValidMobilePhone(phone: string): boolean {
52+
const { isValidDigits, digits } = parsePhoneDigits(phone);
53+
54+
if (!isValidDigits) return false;
55+
56+
return isValidMobilePhoneLength(digits) && isValidMobilePhoneFirstNumber(digits) && isValidDDD(digits);
57+
}
58+
59+
export function isValidLandlinePhone(phone: string): boolean {
60+
const { isValidDigits, digits } = parsePhoneDigits(phone);
61+
62+
if (!isValidDigits) return false;
63+
64+
return isValidLandlinePhoneLength(digits) && isValidLandlinePhoneFirstNumber(digits) && isValidDDD(digits);
2965
}
3066

3167
export function isValid(phone: string): boolean {
32-
if (!phone || typeof phone !== 'string') return false;
68+
const { isValidDigits, digits } = parsePhoneDigits(phone);
3369

34-
const digits = onlyNumbers(phone);
70+
if (!isValidDigits) return false;
3571

3672
return isValidLength(digits) && isValidFirstNumber(digits) && isValidDDD(digits);
3773
}

0 commit comments

Comments
 (0)