Skip to content

Commit

Permalink
feat(cpf-cnpj): New mask for cpf/cnpj inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
the-darc committed May 15, 2015
1 parent d2a0d7f commit d9651e2
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 7 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ See: [Conferência de Inscrições Estaduais](http://www.sintegra.gov.br/insc_es
```javascript
var BrM = require('br-masks');
var cpf = '97070868669';
var isValid = BrM.cpf(cpf);
var masked = BrM.cpf(cpf);
// masked should be '970.708.686-69'
```

Expand All @@ -54,10 +54,20 @@ See: [Conferência de Inscrições Estaduais](http://www.sintegra.gov.br/insc_es
```javascript
var BrM = require('br-masks');
var cnpj = '79121383000106';
var isValid = BrM.cnpj(cnpj);
var masked = BrM.cnpj(cnpj);
// masked should be '79.121.383/0001-06'
```

### CPF/CNPJ ###

```javascript
var BrM = require('br-masks');
var maskedCpf = BrM.cpfCnpj('97070868669');
// maskedCpf should be '970.708.686-69'
var maskedCnpj = BrM.cpfCnpj('79121383000106');
// maskedCnpj should be '79.121.383/0001-06'
```

### CEP ###

```javascript
Expand Down
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ var footer = ['',
' phone: PHONE,',
' cep: CEP,',
' finance: FINANCE,',
' nfeAccessKey: NFEACCESSKEY',
' nfeAccessKey: NFEACCESSKEY,',
' cpfCnpj: CPFCNPJ',
' };',
'}));'].join('\n');

Expand Down
15 changes: 14 additions & 1 deletion releases/br-masks-standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ var CNPJ = function(value) {
return formatedValue;
};

/*exported CPFCNPJ */
/*globals CPF, CNPJ*/
var CPFCNPJ = function(value) {
if (!value || !value.length) {
return value;
} else if (value.length <= 11) {
return CPF(value);
} else {
return CNPJ(value);
}
};

/*exported CPF */
var CPF = function(value) {
var cpfPattern = new StringMask('000.000.000-00');
Expand Down Expand Up @@ -383,6 +395,7 @@ var PHONE = function(value) {
phone: PHONE,
cep: CEP,
finance: FINANCE,
nfeAccessKey: NFEACCESSKEY
nfeAccessKey: NFEACCESSKEY,
cpfCnpj: CPFCNPJ
};
}));
2 changes: 1 addition & 1 deletion releases/br-masks-standalone.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion releases/br-masks.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ var CNPJ = function(value) {
return formatedValue;
};

/*exported CPFCNPJ */
/*globals CPF, CNPJ*/
var CPFCNPJ = function(value) {
if (!value || !value.length) {
return value;
} else if (value.length <= 11) {
return CPF(value);
} else {
return CNPJ(value);
}
};

/*exported CPF */
var CPF = function(value) {
var cpfPattern = new StringMask('000.000.000-00');
Expand Down Expand Up @@ -190,6 +202,7 @@ var PHONE = function(value) {
phone: PHONE,
cep: CEP,
finance: FINANCE,
nfeAccessKey: NFEACCESSKEY
nfeAccessKey: NFEACCESSKEY,
cpfCnpj: CPFCNPJ
};
}));
2 changes: 1 addition & 1 deletion releases/br-masks.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/cpf-cnpj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*exported CPFCNPJ */
/*globals CPF, CNPJ*/
var CPFCNPJ = function(value) {
if (!value || !value.length) {
return value;
} else if (value.length <= 11) {
return CPF(value);
} else {
return CNPJ(value);
}
};
52 changes: 52 additions & 0 deletions test/cpf-cnpj.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var should = require('should'),
BrM = require('../releases/br-masks.min');

describe('CPF/CNPJ', function(){
it('should maks 10157471000161 to 10.157.471/0001-61', function(done) {
should(BrM.cpfCnpj('10157471000161')).be.eql('10.157.471/0001-61');
done();
});
it('should maks 54506158000167 to 54.506.158/0001-67', function(done) {
should(BrM.cpfCnpj('54506158000167')).be.eql('54.506.158/0001-67');
done();
});
it('should maks 79121383000106 to 79.121.383/0001-06', function(done) {
should(BrM.cpfCnpj('79121383000106')).be.eql('79.121.383/0001-06');
done();
});
it('should maks 12871891000134 to 12.871.891/0001-34', function(done) {
should(BrM.cpfCnpj('12871891000134')).be.eql('12.871.891/0001-34');
done();
});
it('should maks 01781192000120 to 01.781.192/0001-20', function(done) {
should(BrM.cpfCnpj('01781192000120')).be.eql('01.781.192/0001-20');
done();
});

// --- //

it('should mask 97070868669 to 970.708.686-69', function(done) {
should(BrM.cpfCnpj('97070868669')).be.eql('970.708.686-69');
done();
});
it('should mask 21984171208 to 219.841.712-08', function(done) {
should(BrM.cpfCnpj('21984171208')).be.eql('219.841.712-08');
done();
});
it('should mask 24653511098 to 246.535.110-98', function(done) {
should(BrM.cpfCnpj('24653511098')).be.eql('246.535.110-98');
done();
});
it('should mask 24650512070 to 246.505.120-70', function(done) {
should(BrM.cpfCnpj('24650512070')).be.eql('246.505.120-70');
done();
});
it('should mask 14351512013 to 143.515.120-13', function(done) {
should(BrM.cpfCnpj('14351512013')).be.eql('143.515.120-13');
done();
});
it('should mask 143515 to 143.515', function(done) {
should(BrM.cpfCnpj('1435151')).be.eql('143.515.1');
done();
});
});

0 comments on commit d9651e2

Please sign in to comment.