Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions docs/pt-br/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ 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('1133334444'); // (11) 3333-4444
formatPhone('11933334444'); // (11) 93333-4444
formatPhone('11933334444', { withCountryCode: true }) // +55 (11) 93333-4444
```

## isValidPIS

Valida se o PIS é válido.
Expand Down
12 changes: 12 additions & 0 deletions docs/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ 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('1133334444'); // (11) 3333-4444
formatPhone('11933334444'); // (11) 93333-4444
formatPhone('11933334444', { withCountryCode: true }) // +55 (11) 93333-4444
```

## isValidPIS

Check if PIS 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 @@ -19,6 +19,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 { format as formatPJ } from './processo-juridico';
export { format as formatCEP, isValid as isValidCEP } from './cep';
Expand Down
23 changes: 23 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,25 @@ 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('1198888777')).toBe('(11) 9888-8777');
expect(format('11988887777')).toBe('(11) 98888-7777');
expect(format('119888877777')).toBe('(11) 9888-877777');
});

test('should format with country code', () => {
expect(format('1188887777', { withCountryCode: true })).toBe('+55 (11) 8888-7777');
});
});
25 changes: 25 additions & 0 deletions src/utilities/phone/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,28 @@ export function isValid(phone: string): boolean {

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

type FormatOptions = {
withCountryCode?: boolean;
};

export function format(phone: string, options?: FormatOptions): string {
const { digits } = parsePhoneDigits(phone);
const HYPHEN_INDEXES = digits.length === PHONE_MAX_LENGTH ? [6] : [5];

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

if ([0].indexOf(i) >= 0) return `(${result}`;
if ([1].indexOf(i) >= 0) return `${result}) `;
if (HYPHEN_INDEXES.indexOf(i) >= 0) return `${result}-`;

return result;
}, '');

if (options?.withCountryCode) return '+55 ' + result;
return result;
}