Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix phone number format according to Anatel #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/formatters/formatToPhone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import mapToNumeric from '../helpers/mapToNumeric';
* //=> '(11) 9716-26'
*
* formatToPhone('11971626799')
* //=> '(11) 9 7162-6799'
* //=> '(11) 97162-6799'
* ```
* @param value
*/
Expand All @@ -21,7 +21,7 @@ const formatToPhone = (
.replace(/(\d{1,2})/, '($1')
.replace(/(\(\d{2})(\d{1,4})/, '$1) $2')
.replace(/( \d{4})(\d{1,4})/, '$1-$2')
.replace(/( \d{1})(\d{3})(?:-)(\d{1})(\d{4})/, '$1 $2$3-$4')
.replace(/( \d{4})(?:-)(\d{1})(\d{4})/, '$1$2-$3')
);

export default formatToPhone;
8 changes: 4 additions & 4 deletions src/validators/isPhone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import isDDD from './isDDD';
* Pattern for common brazilian telephone number formats, optionally with DDI,
* DDD and the ninth digit.
*/
const PHONE_PATTERN = /^(\+55)? ?\(?(\d{2})?\)? ?9? ?\d{4}[-| ]?\d{4}$/;
const PHONE_PATTERN = /^(\+55)? ?\(?(\d{2})?\)? ?9?\d{4}[-| ]?\d{4}$/;

/**
* Check if value is a valid brazilian phone number. It can check a wide
* variety of formats optionally with DDI, DDD and the ninth digit.
*
* @example ```js
* isPhone('+55 (11) 9 8273-1182')
* isPhone('+55 (11) 98273-1182')
* //=> true
*
* isPhone('11 9 8273 1182')
* isPhone('11 98273 1182')
* //=> true
*
* isPhone('1139723768')
Expand All @@ -23,7 +23,7 @@ const PHONE_PATTERN = /^(\+55)? ?\(?(\d{2})?\)? ?9? ?\d{4}[-| ]?\d{4}$/;
* isPhone('(23) 3972-3768')
* //=> false
*
* isPhone('(13) 6 5093-2093')
* isPhone('(13) 65093-2093')
* //=> false
*
* isPhone('(81) 555 178')
Expand Down
2 changes: 1 addition & 1 deletion test/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ test('formatToPhone', (context) => {
context.is(formatToPhone('11'), '(11');
context.is(formatToPhone('11971626'), '(11) 9716-26');
context.is(formatToPhone('1197162679'), '(11) 9716-2679');
context.is(formatToPhone('11971626799'), '(11) 9 7162-6799');
context.is(formatToPhone('11971626799'), '(11) 97162-6799');
});

test('formatToRG', (context) => {
Expand Down
4 changes: 2 additions & 2 deletions test/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ test('isDDD', (context) => {
});

test('isPhone', (context) => {
context.true(isPhone('+55 (11) 9 8273-1182'));
context.true(isPhone('11 9 8273 1182'));
context.true(isPhone('+55 (11) 98273-1182'));
context.true(isPhone('11 98273 1182'));
context.true(isPhone('1139723768'));
context.false(isPhone('(23) 3972-3768'));
context.false(isPhone('(13) 6 5093-2093'));
Expand Down