Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/pt-br/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ import { isValidLandlinePhone } from '@brazilian-utils/brazilian-utils';
isValidLandlinePhone('1130000000'); // true
```

## formatPhone

Formata no padrão mais comum no Brasil

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

formatPhone('1188887777'); // (11) 8888-7777
formatPhone('11988887777'); // (11) 98888-7777
formatPhone('551188887777'); // +55 (11) 8888-7777
formatPhone('5511988887777'); // +55 (11) 98888-7777
```

## isValidLicensePlate

Valida se a placa de carro é válida.
Expand Down
14 changes: 14 additions & 0 deletions docs/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ import { isValidLandlinePhone } from '@brazilian-utils/brazilian-utils';
isValidLandlinePhone('1130000000'); // true
```


## formatPhone

Formats in the most common pattern in Brazil

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

formatPhone('1188887777'); // (11) 8888-7777
formatPhone('11988887777'); // (11) 98888-7777
formatPhone('551188887777'); // +55 (11) 8888-7777
formatPhone('5511988887777'); // +55 (11) 98888-7777
```

## isValidLicensePlate

Check if license plate is valid.
Expand Down
1 change: 1 addition & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('Public API', () => {
'isValidPhone',
'isValidMobilePhone',
'isValidLandlinePhone',
'formatPhone',
'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, isValidMobilePhone, isValidLandlinePhone } from './phone';
export { isValid as isValidPhone, format as formatPhone, isValidMobilePhone, isValidLandlinePhone } from './phone';
export { isValid as isValidEmail } from './email';
export { isValid as isValidLicensePlate } from './licensePlate';
export { format as formatPJ } from './processo-juridico';
Expand Down
24 changes: 24 additions & 0 deletions src/utilities/phone/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
isValid,
isValidMobilePhone,
isValidLandlinePhone,
format,
VALID_AREA_CODES,
PHONE_MIN_LENGTH,
PHONE_MAX_LENGTH,
Expand Down Expand Up @@ -107,3 +108,26 @@ describe('isValid', () => {
});
});
});

describe('format', () => {
test('should format phone', () => {
expect(format('')).toBe('');
expect(format('1')).toBe('(1');
expect(format('11')).toBe('(11) ');
expect(format('119')).toBe('(11) 9');
expect(format('1198')).toBe('(11) 98');
expect(format('11988')).toBe('(11) 988');
expect(format('119888')).toBe('(11) 9888-');
expect(format('1198888')).toBe('(11) 9888-8');
expect(format('11988887')).toBe('(11) 9888-87');
expect(format('119888877')).toBe('(11) 9888-877');
expect(format('1188887777')).toBe('(11) 8888-7777');
expect(format('11988887777')).toBe('(11) 98888-7777');
expect(format('51188887777')).toBe('(51) 18888-7777');
expect(format('551188887777')).toBe('+55 (11) 8888-7777');
expect(format('5511988887777')).toBe('+55 (11) 98888-7777');
expect(format('55119888877776666')).toBe('+55 (11) 98888-77776666');
expect(format('1188#887AB777')).toBe('(11) 8888-7777');
expect(format('11988#887AB777')).toBe('(11) 98888-7777');
});
});
32 changes: 32 additions & 0 deletions src/utilities/phone/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,35 @@ export function isValid(phone: string): boolean {

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

export function format(phone: string): string {
const { digits } = parsePhoneDigits(phone);
const hasCountry = digits.length > PHONE_MAX_LENGTH;

const getHiphenIndex = () => {
if (hasCountry) return digits.length === 12 ? [7] : [8];
return digits.length === PHONE_MAX_LENGTH ? [6] : [5];
};

const result = digits
.slice(0, digits.length)
.split('')
.reduce((acc, digit, i) => {
const result = `${acc}${digit}`;

if (hasCountry) {
if ([0].indexOf(i) >= 0) return `+${result}`;
if ([1].indexOf(i) >= 0) return `${result} (`;
if ([3].indexOf(i) >= 0) return `${result}) `;
} else {
if ([0].indexOf(i) >= 0) return `(${result}`;
if ([1].indexOf(i) >= 0) return `${result}) `;
}

if (getHiphenIndex().indexOf(i) >= 0) return `${result}-`;

return result;
}, '');

return result;
}