Skip to content

Commit

Permalink
feat: add functions isValidLandlinePhone() and isValidMobilePhone()
Browse files Browse the repository at this point in the history
  • Loading branch information
silvioprog committed Jul 7, 2020
1 parent 2c6b8b1 commit e8fe8bd
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 8 deletions.
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@
"doc",
"test"
]
},
{
"login": "silvioprog",
"name": "Silvio Clécio",
"avatar_url": "https://avatars0.githubusercontent.com/u/1456829?v=4",
"profile": "http://fb.com/silvioclecio",
"contributions": [
"code",
"doc",
"test"
]
}
],
"repoType": "github",
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Our "thank you" goes to these wonderful people ([emoji key](https://github.com/k
<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>
<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>
<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>
<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>
</tr>
</table>

Expand Down
22 changes: 21 additions & 1 deletion docs/pt-br/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,34 @@ isValidEmail('john.doe@hotmail.com'); // true

## isValidPhone

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

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

isValidPhone('11900000000'); // true
```

## isValidMobilePhone

Valida se o número de telefone celular é valido.

```javascript
import { isValidMobilePhone } from '@brazilian-utils/brazilian-utils';

isValidMobilePhone('11900000000'); // true
```

## isValidLandlinePhone

Valida se o número de telefone residencial é valido.

```javascript
import { isValidLandlinePhone } from '@brazilian-utils/brazilian-utils';

isValidLandlinePhone('1130000000'); // true
```

## isValidPIS

Valida se o PIS é válido.
Expand Down
20 changes: 20 additions & 0 deletions docs/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ import { isValidPhone } from '@brazilian-utils/brazilian-utils';
isValidPhone('11900000000'); // true
```

## isValidMobilePhone

Check if mobile phone number is valid.

```javascript
import { isValidMobilePhone } from '@brazilian-utils/brazilian-utils';

isValidMobilePhone('11900000000'); // true
```

## isValidLandlinePhone

Check if landline phone number is valid.

```javascript
import { isValidLandlinePhone } from '@brazilian-utils/brazilian-utils';

isValidLandlinePhone('1130000000'); // true
```

## isValidPIS

Check if PIS is valid.
Expand Down
2 changes: 2 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ describe('Public API', () => {
'generateCPF',
'isValidEmail',
'isValidPhone',
'isValidMobilePhone',
'isValidLandlinePhone',
'generateCNPJ',
'formatBoleto',
'isValidBoleto',
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { isValid as isValidIE } from './inscricao-estadual';
export { isValid as isValidPIS } from './pis';
export { isValid as isValidPhone } from './phone';
export { isValid as isValidPhone, isValidMobilePhone, isValidLandlinePhone } from './phone';
export { isValid as isValidEmail } from './email';
export { format as formatPJ } from './processo-juridico';
export { format as formatCEP, isValid as isValidCEP } from './cep';
Expand Down
35 changes: 34 additions & 1 deletion src/utilities/phone/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,83 @@
import { isValid, VALID_AREA_CODES, PHONE_MIN_LENGTH, PHONE_MAX_LENGTH } from '.';
import {
isValid,
isValidMobilePhone,
isValidLandlinePhone,
VALID_AREA_CODES,
PHONE_MIN_LENGTH,
PHONE_MAX_LENGTH,
} from '.';

describe('isValid', () => {
describe('should return false', () => {
test('when is a empty string', () => {
expect(isValid('')).toBe(false);
expect(isValidMobilePhone('')).toBe(false);
expect(isValidLandlinePhone('')).toBe(false);
});

test('when is null', () => {
expect(isValid(null as any)).toBe(false);
expect(isValidMobilePhone(null as any)).toBe(false);
expect(isValidLandlinePhone(null as any)).toBe(false);
});

test('when is undefined', () => {
expect(isValid(undefined as any)).toBe(false);
expect(isValidMobilePhone(undefined as any)).toBe(false);
expect(isValidLandlinePhone(undefined as any)).toBe(false);
});

test('when is a boolean', () => {
expect(isValid(true as any)).toBe(false);
expect(isValid(false as any)).toBe(false);
expect(isValidMobilePhone(false as any)).toBe(false);
expect(isValidLandlinePhone(true as any)).toBe(false);
expect(isValidMobilePhone(false as any)).toBe(false);
expect(isValidLandlinePhone(true as any)).toBe(false);
});

test('when is a object', () => {
expect(isValid({} as any)).toBe(false);
expect(isValidMobilePhone({} as any)).toBe(false);
expect(isValidLandlinePhone({} as any)).toBe(false);
});

test('when is a array', () => {
expect(isValid([] as any)).toBe(false);
expect(isValidMobilePhone([] as any)).toBe(false);
expect(isValidLandlinePhone([] as any)).toBe(false);
});

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

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

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

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

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

test(`when dont match with phone max length (${PHONE_MAX_LENGTH})`, () => {
expect(isValid('11300000001130000000')).toBe(false);
expect(isValidMobilePhone('11300000001130000000')).toBe(false);
expect(isValidLandlinePhone('11300000001130000000')).toBe(false);
});
});

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

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

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

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

test('when is a landline valid without mask', () => {
expect(isValid('1130000000')).toBe(true);
expect(isValidLandlinePhone('1130000000')).toBe(true);
});
});
});
46 changes: 41 additions & 5 deletions src/utilities/phone/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,56 @@ export function isValidDDD(phone: string): boolean {
return VALID_AREA_CODES.includes(Number(phone.substr(0, 2)));
}

export function isValidMobilePhoneLength(phone: string): boolean {
return phone.length === PHONE_MAX_LENGTH;
}

export function isValidLandlinePhoneLength(phone: string): boolean {
return phone.length >= PHONE_MIN_LENGTH && phone.length < PHONE_MAX_LENGTH;
}

export function isValidLength(phone: string): boolean {
return phone.length >= PHONE_MIN_LENGTH && phone.length <= PHONE_MAX_LENGTH;
return isValidLandlinePhoneLength(phone) || isValidMobilePhoneLength(phone);
}

export function isValidMobilePhoneFirstNumber(phone: string): boolean {
return MOBILE_VALID_FIRST_NUMBERS.includes(Number(phone.charAt(2)));
}

export function isValidLandlinePhoneFirstNumber(phone: string): boolean {
return LANDLINE_VALID_FIRST_NUMBERS.includes(Number(phone.charAt(2)));
}

export function isValidFirstNumber(phone: string): boolean {
return phone.length === PHONE_MIN_LENGTH
? LANDLINE_VALID_FIRST_NUMBERS.includes(Number(phone.charAt(2)))
: MOBILE_VALID_FIRST_NUMBERS.includes(Number(phone.charAt(2)));
? isValidLandlinePhoneFirstNumber(phone)
: isValidMobilePhoneFirstNumber(phone);
}

function tryPhoneDigits(phone: string): { isValidDigits: boolean; digits: string } {
return { isValidDigits: !!phone && typeof phone === 'string', digits: onlyNumbers(phone) };
}

export function isValidMobilePhone(phone: string): boolean {
const { isValidDigits, digits } = tryPhoneDigits(phone);

if (!isValidDigits) return false;

return isValidMobilePhoneLength(digits) && isValidMobilePhoneFirstNumber(digits) && isValidDDD(digits);
}

export function isValidLandlinePhone(phone: string): boolean {
const { isValidDigits, digits } = tryPhoneDigits(phone);

if (!isValidDigits) return false;

return isValidLandlinePhoneLength(digits) && isValidLandlinePhoneFirstNumber(digits) && isValidDDD(digits);
}

export function isValid(phone: string): boolean {
if (!phone || typeof phone !== 'string') return false;
const { isValidDigits, digits } = tryPhoneDigits(phone);

const digits = onlyNumbers(phone);
if (!isValidDigits) return false;

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

0 comments on commit e8fe8bd

Please sign in to comment.