├── .gitignore
├── .travis.yml
├── src
├── cep.js
├── cpf.js
├── cnpj.js
├── cnpj-base.js
├── cpf-cnpj.js
├── nfe-access-key.js
├── phone.js
├── finance.js
└── ie.js
├── test
├── cep.test.js
├── phone.test.js
├── cnpj-base.test.js
├── cnpj.test.js
├── cpf.test.js
├── nfe-access-key.test.js
├── cpf-cnpj.test.js
├── finance.test.js
└── ie.test.js
├── bower.json
├── LICENSE
├── CHANGELOG.md
├── package.json
├── releases
├── br-masks.min.js
├── br-masks-standalone.min.js
├── br-masks.js
└── br-masks-standalone.js
├── README.md
├── gulpfile.js
└── .jshintrc
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | bower_components
3 | npm-debug.log
4 | coverage
5 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - "6"
4 | - "5"
5 | - "4"
6 | - "0.11"
7 | before_install:
8 | - npm install -g bower
9 |
--------------------------------------------------------------------------------
/src/cep.js:
--------------------------------------------------------------------------------
1 | /*exported CEP */
2 | var CEP = function(value) {
3 | var cepMask = new StringMask('00000-000');
4 | if(!value) {
5 | return value;
6 | }
7 | var processed = cepMask.process(value);
8 | return processed.result;
9 | };
10 |
--------------------------------------------------------------------------------
/src/cpf.js:
--------------------------------------------------------------------------------
1 | /*exported CPF */
2 | var CPF = function(value) {
3 | var cpfPattern = new StringMask('000.000.000-00');
4 | if(!value) {
5 | return value;
6 | }
7 | var formatedValue = cpfPattern.apply(value);
8 | return formatedValue;
9 | };
10 |
--------------------------------------------------------------------------------
/src/cnpj.js:
--------------------------------------------------------------------------------
1 | /*exported CNPJ */
2 | var CNPJ = function(value) {
3 | if(!value) {
4 | return value;
5 | }
6 | var cnpjPattern = new StringMask('00.000.000\/0000-00');
7 | var formatedValue = cnpjPattern.apply(value);
8 | return formatedValue;
9 | };
10 |
--------------------------------------------------------------------------------
/src/cnpj-base.js:
--------------------------------------------------------------------------------
1 | /*exported CNPJBASE */
2 | var CNPJBASE = function(value) {
3 | if(!value) {
4 | return value;
5 | }
6 | var cnpjBasePattern = new StringMask('00.000.000');
7 | var formatedValue = cnpjBasePattern.apply(value);
8 | return formatedValue;
9 | };
10 |
--------------------------------------------------------------------------------
/src/cpf-cnpj.js:
--------------------------------------------------------------------------------
1 | /*exported CPFCNPJ */
2 | /*globals CPF, CNPJ*/
3 | var CPFCNPJ = function(value) {
4 | if (!value || !value.length) {
5 | return value;
6 | } else if (value.length <= 11) {
7 | return CPF(value);
8 | } else {
9 | return CNPJ(value);
10 | }
11 | };
12 |
--------------------------------------------------------------------------------
/src/nfe-access-key.js:
--------------------------------------------------------------------------------
1 | /*exported NFEACCESSKEY */
2 | var NFEACCESSKEY = function(value) {
3 | if(!value) {
4 | return value;
5 | }
6 |
7 | var maskPattern = '0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000';
8 | var nfeMask = new StringMask(maskPattern);
9 | var formatedValue = nfeMask.apply(value);
10 | return formatedValue;
11 | };
12 |
--------------------------------------------------------------------------------
/test/cep.test.js:
--------------------------------------------------------------------------------
1 | var should = require('should'),
2 | BrM = require('../releases/br-masks');
3 |
4 | describe('CEP', function(){
5 | it('should not mask empty values', function(done) {
6 | should(BrM.cep(null)).be.eql(null);
7 | should(BrM.cep(undefined)).be.eql(undefined);
8 | should(BrM.cep('')).be.eql('');
9 | should(BrM.cep(0)).be.eql(0);
10 | should(BrM.cep(false)).be.eql(false);
11 | done();
12 | });
13 |
14 | it('should maks 30480530 to 30480-530', function(done) {
15 | should(BrM.cep('30480530')).be.eql('30480-530');
16 | done();
17 | });
18 | });
19 |
--------------------------------------------------------------------------------
/src/phone.js:
--------------------------------------------------------------------------------
1 | /*exported PHONE */
2 | var PHONE = function(value) {
3 | var phoneMask8D = new StringMask('(00) 0000-0000'),
4 | phoneMask9D = new StringMask('(00) 00000-0000'),
5 | phoneMask0800 = new StringMask('0000-000-0000');
6 |
7 | if(!value) {
8 | return value;
9 | }
10 |
11 | var formatedValue;
12 | value = value + '';
13 | if (value.indexOf('0800') === 0) {
14 | formatedValue = phoneMask0800.apply(value);
15 | }else if(value.length < 11){
16 | formatedValue = phoneMask8D.apply(value);
17 | }else{
18 | formatedValue = phoneMask9D.apply(value);
19 | }
20 |
21 | return formatedValue;
22 | };
23 |
--------------------------------------------------------------------------------
/src/finance.js:
--------------------------------------------------------------------------------
1 | /*exported FINANCE */
2 | var FINANCE = function(value, precision, decimalSep, groupSep) {
3 | precision = (!precision && precision !== 0) || precision < 0? 2 : precision;
4 | decimalSep = decimalSep || '.';
5 | groupSep = groupSep || '';
6 |
7 | var decimalsPattern = precision > 0 ? decimalSep + new Array(precision + 1).join('0') : '';
8 | var maskPattern = '#'+groupSep+'##0'+decimalsPattern;
9 |
10 | value = parseFloat(value);
11 | if (!value) {
12 | value = 0;
13 | }
14 |
15 | var negative = false;
16 | if (value < 0) {
17 | value = value * -1;
18 | negative = true;
19 | }
20 | var financeMask = new StringMask(maskPattern, {reverse: true});
21 | var masked = financeMask.apply(value.toFixed(precision).replace(/[^\d]+/g,''));
22 | return negative ? '('+masked+')' : masked;
23 | };
24 |
--------------------------------------------------------------------------------
/test/phone.test.js:
--------------------------------------------------------------------------------
1 | var should = require('should'),
2 | BrM = require('../releases/br-masks');
3 |
4 | describe('PHONE', function(){
5 | it('should not mask empty values', function(done) {
6 | should(BrM.phone(null)).be.eql(null);
7 | should(BrM.phone(undefined)).be.eql(undefined);
8 | should(BrM.phone('')).be.eql('');
9 | should(BrM.phone(0)).be.eql(0);
10 | should(BrM.phone(false)).be.eql(false);
11 | done();
12 | });
13 | it('should maks 3133340167 to (31) 3334-0167', function(done) {
14 | should(BrM.phone('3133340167')).be.eql('(31) 3334-0167');
15 | done();
16 | });
17 | it('should maks 38212201255 to (38) 21220-1255', function(done) {
18 | should(BrM.phone('38212201255')).be.eql('(38) 21220-1255');
19 | done();
20 | });
21 | it('should maks 08001234567 to 0800-123-4567', function(done) {
22 | should(BrM.phone('08001234567')).be.eql('0800-123-4567');
23 | done();
24 | });
25 | });
26 |
--------------------------------------------------------------------------------
/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "br-masks",
3 | "version": "0.5.0",
4 | "description": "A library of masks applicable to several Brazilian data like I.E., CNPJ, CPF and others",
5 | "homepage": "http://github.com/the-darc/br-masks",
6 | "repository": {
7 | "type": "git",
8 | "url": "git@github.com:the-darc/br-masks.git"
9 | },
10 | "main": "releases/br-masks.js",
11 | "author": [
12 | {
13 | "name": "Daniel Campos",
14 | "email": "darc.tec@gmail.com"
15 | }
16 | ],
17 | "keywords": [
18 | "mask",
19 | "cpf",
20 | "cnpj",
21 | "cnpj base",
22 | "inscrição estadual",
23 | "cep",
24 | "telefone"
25 | ],
26 | "license": "MIT",
27 | "ignore": [
28 | "**/.*",
29 | "node_modules",
30 | "bower_components",
31 | "src",
32 | "**/*.test.js",
33 | "package.json"
34 | ],
35 | "dependencies": {
36 | "string-mask": "^0.3.0"
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Daniel
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/test/cnpj-base.test.js:
--------------------------------------------------------------------------------
1 | var should = require('should'),
2 | BrM = require('../releases/br-masks');
3 |
4 | describe('CNPJ Base', function(){
5 | it('should not mask empty values', function(done) {
6 | should(BrM.cnpjBase(null)).be.eql(null);
7 | should(BrM.cnpjBase(undefined)).be.eql(undefined);
8 | should(BrM.cnpjBase('')).be.eql('');
9 | should(BrM.cnpjBase(0)).be.eql(0);
10 | should(BrM.cnpjBase(false)).be.eql(false);
11 | done();
12 | });
13 |
14 | it('should maks 10157471 to 10.157.471', function(done) {
15 | should(BrM.cnpjBase('10157471')).be.eql('10.157.471');
16 | done();
17 | });
18 | it('should maks 54506158 to 54.506.158', function(done) {
19 | should(BrM.cnpjBase('54506158')).be.eql('54.506.158');
20 | done();
21 | });
22 | it('should maks 79121383 to 79.121.383', function(done) {
23 | should(BrM.cnpjBase('79121383')).be.eql('79.121.383');
24 | done();
25 | });
26 | it('should maks 12871891 to 12.871.891', function(done) {
27 | should(BrM.cnpjBase('12871891')).be.eql('12.871.891');
28 | done();
29 | });
30 | it('should maks 01781192 to 01.781.192', function(done) {
31 | should(BrM.cnpjBase('01781192')).be.eql('01.781.192');
32 | done();
33 | });
34 | });
35 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 |
2 | # [0.5.0](https://github.com/the-darc/br-masks/compare/0.4.1...0.5.0) (2016-11-09)
3 |
4 |
5 | ### Features
6 |
7 | * **phone:** added support for 0800 phone numbers ([8fb2f46](https://github.com/the-darc/br-masks/commit/8fb2f46)), closes [#8](https://github.com/the-darc/br-masks/pull/8)
8 | * **cnpj-base:** added support for cnpj-base.js ([5cc07ff](https://github.com/the-darc/br-masks/commit/5cc07ff)), closes [#7](https://github.com/the-darc/br-masks/pull/7)
9 |
10 |
11 |
12 | ### 0.4.1 (2015-09-01)
13 |
14 |
15 | #### Bug Fixes
16 |
17 | * **package.json:** remove postinstall script ([999b5d49](http://github.com/the-darc/br-masks/commit/999b5d49))
18 |
19 |
20 |
21 | ## 0.4.0 (2015-05-15)
22 |
23 | #### Features
24 |
25 | * **cpf-cnpj:** New mask for cpf/cnpj inputs ([d9651e24](http://github.com/the-darc/br-masks/commit/d9651e24))
26 |
27 |
28 |
29 | ### 0.3.4 (2015-05-15)
30 |
31 |
32 | #### Bug Fixes
33 |
34 | * **i.e.:** i.e. masks should only works for string values ([c0b4f06f](http://github.com/the-darc/br-masks/commit/c0b4f06f))
35 | * **phone:** phone mask should TRY to work with numbers ([58563318](http://github.com/the-darc/br-masks/commit/58563318))
36 |
37 |
--------------------------------------------------------------------------------
/test/cnpj.test.js:
--------------------------------------------------------------------------------
1 | var should = require('should'),
2 | BrM = require('../releases/br-masks');
3 |
4 | describe('CNPJ', function(){
5 | it('should not mask empty values', function(done) {
6 | should(BrM.cnpj(null)).be.eql(null);
7 | should(BrM.cnpj(undefined)).be.eql(undefined);
8 | should(BrM.cnpj('')).be.eql('');
9 | should(BrM.cnpj(0)).be.eql(0);
10 | should(BrM.cnpj(false)).be.eql(false);
11 | done();
12 | });
13 |
14 | it('should maks 10157471000161 to 10.157.471/0001-61', function(done) {
15 | should(BrM.cnpj('10157471000161')).be.eql('10.157.471/0001-61');
16 | done();
17 | });
18 | it('should maks 54506158000167 to 54.506.158/0001-67', function(done) {
19 | should(BrM.cnpj('54506158000167')).be.eql('54.506.158/0001-67');
20 | done();
21 | });
22 | it('should maks 79121383000106 to 79.121.383/0001-06', function(done) {
23 | should(BrM.cnpj('79121383000106')).be.eql('79.121.383/0001-06');
24 | done();
25 | });
26 | it('should maks 12871891000134 to 12.871.891/0001-34', function(done) {
27 | should(BrM.cnpj('12871891000134')).be.eql('12.871.891/0001-34');
28 | done();
29 | });
30 | it('should maks 01781192000120 to 01.781.192/0001-20', function(done) {
31 | should(BrM.cnpj('01781192000120')).be.eql('01.781.192/0001-20');
32 | done();
33 | });
34 | });
35 |
--------------------------------------------------------------------------------
/test/cpf.test.js:
--------------------------------------------------------------------------------
1 | var should = require('should'),
2 | BrM = require('../releases/br-masks');
3 |
4 | describe('CPF ', function() {
5 | it('should not mask empty values', function(done) {
6 | should(BrM.cpf(null)).be.eql(null);
7 | should(BrM.cpf(undefined)).be.eql(undefined);
8 | should(BrM.cpf('')).be.eql('');
9 | should(BrM.cpf(0)).be.eql(0);
10 | should(BrM.cpf(false)).be.eql(false);
11 | done();
12 | });
13 |
14 | it('should mask 97070868669 to 970.708.686-69', function(done) {
15 | should(BrM.cpf('97070868669')).be.eql('970.708.686-69');
16 | done();
17 | });
18 | it('should mask 21984171208 to 219.841.712-08', function(done) {
19 | should(BrM.cpf('21984171208')).be.eql('219.841.712-08');
20 | done();
21 | });
22 | it('should mask 24653511098 to 246.535.110-98', function(done) {
23 | should(BrM.cpf('24653511098')).be.eql('246.535.110-98');
24 | done();
25 | });
26 | it('should mask 24650512070 to 246.505.120-70', function(done) {
27 | should(BrM.cpf('24650512070')).be.eql('246.505.120-70');
28 | done();
29 | });
30 | it('should mask 14351512013 to 143.515.120-13', function(done) {
31 | should(BrM.cpf('14351512013')).be.eql('143.515.120-13');
32 | done();
33 | });
34 | it('should mask 143515 to 143.515', function(done) {
35 | should(BrM.cpf('1435151')).be.eql('143.515.1');
36 | done();
37 | });
38 | });
39 |
--------------------------------------------------------------------------------
/test/nfe-access-key.test.js:
--------------------------------------------------------------------------------
1 | var should = require('should'),
2 | BrM = require('../releases/br-masks');
3 |
4 | describe('NFE ACCESS KEY', function(){
5 | it('should not mask empty values', function(done) {
6 | should(BrM.nfeAccessKey(null)).be.eql(null);
7 | should(BrM.nfeAccessKey(undefined)).be.eql(undefined);
8 | should(BrM.nfeAccessKey('')).be.eql('');
9 | should(BrM.nfeAccessKey(0)).be.eql(0);
10 | should(BrM.nfeAccessKey(false)).be.eql(false);
11 | done();
12 | });
13 |
14 | var nfes = [{
15 | key: '35140111724258000157550010006882191630386000',
16 | expected: '3514 0111 7242 5800 0157 5500 1000 6882 1916 3038 6000'
17 | },{
18 | key: '35131105827094000867550010011845921014997330',
19 | expected: '3513 1105 8270 9400 0867 5500 1001 1845 9210 1499 7330'
20 | },{
21 | key: '35140100776574000741550010253751881713008970',
22 | expected: '3514 0100 7765 7400 0741 5500 1025 3751 8817 1300 8970'
23 | },{
24 | key: '31130803420926007994550020000778901926162951',
25 | expected: '3113 0803 4209 2600 7994 5500 2000 0778 9019 2616 2951'
26 | },{
27 | key: '35130161365284015136550140061338751137432219',
28 | expected: '3513 0161 3652 8401 5136 5501 4006 1338 7511 3743 2219'
29 | }];
30 |
31 | for (var i = 0; i < nfes.length; i ++) {
32 | var nak = nfes[i];
33 | it('should mask nfe access key '+nak.key, function(done) {
34 | should(BrM.nfeAccessKey(nak.key)).be.eql(nak.expected);
35 | done();
36 | });
37 | }
38 | });
39 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "br-masks",
3 | "version": "0.5.0",
4 | "description": "A library of masks applicable to several Brazilian data like I.E., CNPJ, CPF and others",
5 | "id": "/br-masks",
6 | "main": "releases/br-masks.js",
7 | "repository": {
8 | "type": "git",
9 | "url": "https://github.com/the-darc/br-masks.git"
10 | },
11 | "homepage": "http://github.com/the-darc/br-masks",
12 | "bugs": {
13 | "url": "https://github.com/the-darc/br-masks/issues"
14 | },
15 | "scripts": {
16 | "test": "gulp test-coverage",
17 | "release": "conventional-changelog -p angular -i CHANGELOG.md -s"
18 | },
19 | "keywords": [
20 | "masks",
21 | "mascara",
22 | "cpf",
23 | "cnpj",
24 | "cnpj base",
25 | "inscrição estadual",
26 | "cep"
27 | ],
28 | "author": {
29 | "name": "Daniel Campos",
30 | "email": "darc.tec@gmail.com"
31 | },
32 | "license": "MIT",
33 | "dependencies": {
34 | "string-mask": "^0.3.0"
35 | },
36 | "devDependencies": {
37 | "conventional-changelog": "^1.1.0",
38 | "gulp": "^3.9.1",
39 | "gulp-concat": "^2.6.0",
40 | "gulp-coveralls": "^0.1.4",
41 | "gulp-footer": "^1.0.5",
42 | "gulp-header": "^1.0.5",
43 | "gulp-istanbul": "^1.1.1",
44 | "gulp-jshint": "^2.0.2",
45 | "gulp-load-plugins": "^1.4.0",
46 | "gulp-mocha": "^3.0.1",
47 | "gulp-uglify": "^2.0.0",
48 | "jshint": "^2.9.4",
49 | "jshint-stylish": "^2.2.1",
50 | "should": "^11.1.1"
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/test/cpf-cnpj.test.js:
--------------------------------------------------------------------------------
1 | var should = require('should'),
2 | BrM = require('../releases/br-masks');
3 |
4 | describe('CPF/CNPJ', function(){
5 | it('should not mask empty values', function(done) {
6 | should(BrM.cpfCnpj(null)).be.eql(null);
7 | should(BrM.cpfCnpj(undefined)).be.eql(undefined);
8 | should(BrM.cpfCnpj('')).be.eql('');
9 | should(BrM.cpfCnpj(0)).be.eql(0);
10 | should(BrM.cpfCnpj(false)).be.eql(false);
11 | done();
12 | });
13 |
14 | it('should maks 10157471000161 to 10.157.471/0001-61', function(done) {
15 | should(BrM.cpfCnpj('10157471000161')).be.eql('10.157.471/0001-61');
16 | done();
17 | });
18 | it('should maks 54506158000167 to 54.506.158/0001-67', function(done) {
19 | should(BrM.cpfCnpj('54506158000167')).be.eql('54.506.158/0001-67');
20 | done();
21 | });
22 | it('should maks 79121383000106 to 79.121.383/0001-06', function(done) {
23 | should(BrM.cpfCnpj('79121383000106')).be.eql('79.121.383/0001-06');
24 | done();
25 | });
26 | it('should maks 12871891000134 to 12.871.891/0001-34', function(done) {
27 | should(BrM.cpfCnpj('12871891000134')).be.eql('12.871.891/0001-34');
28 | done();
29 | });
30 | it('should maks 01781192000120 to 01.781.192/0001-20', function(done) {
31 | should(BrM.cpfCnpj('01781192000120')).be.eql('01.781.192/0001-20');
32 | done();
33 | });
34 |
35 | // --- //
36 |
37 | it('should mask 97070868669 to 970.708.686-69', function(done) {
38 | should(BrM.cpfCnpj('97070868669')).be.eql('970.708.686-69');
39 | done();
40 | });
41 | it('should mask 21984171208 to 219.841.712-08', function(done) {
42 | should(BrM.cpfCnpj('21984171208')).be.eql('219.841.712-08');
43 | done();
44 | });
45 | it('should mask 24653511098 to 246.535.110-98', function(done) {
46 | should(BrM.cpfCnpj('24653511098')).be.eql('246.535.110-98');
47 | done();
48 | });
49 | it('should mask 24650512070 to 246.505.120-70', function(done) {
50 | should(BrM.cpfCnpj('24650512070')).be.eql('246.505.120-70');
51 | done();
52 | });
53 | it('should mask 14351512013 to 143.515.120-13', function(done) {
54 | should(BrM.cpfCnpj('14351512013')).be.eql('143.515.120-13');
55 | done();
56 | });
57 | it('should mask 143515 to 143.515', function(done) {
58 | should(BrM.cpfCnpj('1435151')).be.eql('143.515.1');
59 | done();
60 | });
61 | });
62 |
--------------------------------------------------------------------------------
/test/finance.test.js:
--------------------------------------------------------------------------------
1 | var should = require('should'),
2 | BrM = require('../releases/br-masks');
3 |
4 | describe('FINANCE', function(){
5 | it('should maks 125 to 125.00', function(done) {
6 | should(BrM.finance('125')).be.eql('125.00');
7 | should(BrM.finance(125)).be.eql('125.00');
8 | done();
9 | });
10 | it('should maks -125 to (125.00)', function(done) {
11 | should(BrM.finance('-125')).be.eql('(125.00)');
12 | should(BrM.finance(-125)).be.eql('(125.00)');
13 | done();
14 | });
15 | it('should round numbers', function(done) {
16 | should(BrM.finance('1.1234')).be.eql('1.12');
17 | should(BrM.finance(1.1234)).be.eql('1.12');
18 | should(BrM.finance('-1.1234')).be.eql('(1.12)');
19 | should(BrM.finance(-1.1234)).be.eql('(1.12)');
20 |
21 | should(BrM.finance('1.1274')).be.eql('1.13');
22 | should(BrM.finance(1.1274)).be.eql('1.13');
23 | should(BrM.finance('-1.1274')).be.eql('(1.13)');
24 | should(BrM.finance(-1.1274)).be.eql('(1.13)');
25 | done();
26 | });
27 | it('should change precision', function(done) {
28 | should(BrM.finance('123.1237123', 3)).be.eql('123.124');
29 | should(BrM.finance(0.6234,0)).be.eql('1');
30 | should(BrM.finance(123555.6234,-1)).be.eql('123555.62');
31 |
32 | should(BrM.finance('-123.1237123', 3)).be.eql('(123.124)');
33 | should(BrM.finance(-0.6234,0)).be.eql('(1)');
34 | should(BrM.finance(-123555.6234,-1)).be.eql('(123555.62)');
35 | done();
36 | });
37 | it('should change separators', function(done) {
38 | should(BrM.finance('9123.1237123', 3, ',', '.')).be.eql('9.123,124');
39 | should(BrM.finance(0.6234,0, ',', '.')).be.eql('1');
40 | should(BrM.finance(87123555.6234,-1, ',', '.')).be.eql('87.123.555,62');
41 |
42 | should(BrM.finance('-9123.1237123', 3, ',', '.')).be.eql('(9.123,124)');
43 | should(BrM.finance(-0.6234,0, ',', '.')).be.eql('(1)');
44 | should(BrM.finance(-87123555.6234,-1, ',', '.')).be.eql('(87.123.555,62)');
45 | done();
46 | });
47 | it('should mask invalid numbers to 0.00', function(done) {
48 | should(BrM.finance('0')).be.eql('0.00');
49 | should(BrM.finance('-0')).be.eql('0.00');
50 |
51 | should(BrM.finance('')).be.eql('0.00');
52 | should(BrM.finance(undefined)).be.eql('0.00');
53 | should(BrM.finance(null)).be.eql('0.00');
54 | should(BrM.finance()).be.eql('0.00');
55 |
56 | should(BrM.finance('a')).be.eql('0.00');
57 | should(BrM.finance(' ')).be.eql('0.00');
58 |
59 | should(BrM.finance('123a')).be.eql('123.00');
60 | should(BrM.finance('123.a.1231')).be.eql('123.00');
61 | should(BrM.finance('1.1256a.1231')).be.eql('1.13');
62 | done();
63 | });
64 | });
65 |
--------------------------------------------------------------------------------
/src/ie.js:
--------------------------------------------------------------------------------
1 | /*exported IE */
2 | var IE = function(value, uf) {
3 | if (!value || typeof value !== 'string') {
4 | return value;
5 | }
6 |
7 | var ieMasks = {
8 | 'AC': [{mask: new StringMask('00.000.000/000-00')}],
9 | 'AL': [{mask: new StringMask('000000000')}],
10 | 'AM': [{mask: new StringMask('00.000.000-0')}],
11 | 'AP': [{mask: new StringMask('000000000')}],
12 | 'BA': [{chars: 8, mask: new StringMask('000000-00')},
13 | {mask: new StringMask('0000000-00')}],
14 | 'CE': [{mask: new StringMask('00000000-0')}],
15 | 'DF': [{mask: new StringMask('00000000000-00')}],
16 | 'ES': [{mask: new StringMask('00000000-0')}],
17 | 'GO': [{mask: new StringMask('00.000.000-0')}],
18 | 'MA': [{mask: new StringMask('000000000')}],
19 | 'MG': [{mask: new StringMask('000.000.000/0000')}],
20 | 'MS': [{mask: new StringMask('000000000')}],
21 | 'MT': [{mask: new StringMask('0000000000-0')}],
22 | 'PA': [{mask: new StringMask('00-000000-0')}],
23 | 'PB': [{mask: new StringMask('00000000-0')}],
24 | 'PE': [{chars: 9, mask: new StringMask('0000000-00')},
25 | {mask: new StringMask('00.0.000.0000000-0')}],
26 | 'PI': [{mask: new StringMask('000000000')}],
27 | 'PR': [{mask: new StringMask('000.00000-00')}],
28 | 'RJ': [{mask: new StringMask('00.000.00-0')}],
29 | 'RN': [{chars: 9, mask: new StringMask('00.000.000-0')},
30 | {mask: new StringMask('00.0.000.000-0')}],
31 | 'RO': [{mask: new StringMask('0000000000000-0')}],
32 | 'RR': [{mask: new StringMask('00000000-0')}],
33 | 'RS': [{mask: new StringMask('000/0000000')}],
34 | 'SC': [{mask: new StringMask('000.000.000')}],
35 | 'SE': [{mask: new StringMask('00000000-0')}],
36 | 'SP': [{mask: new StringMask('000.000.000.000')},
37 | {mask: new StringMask('-00000000.0/000')}],
38 | 'TO': [{mask: new StringMask('00000000000')}]
39 | };
40 |
41 | function clearValue (value) {
42 | return value.replace(/[^0-9]/g, '');
43 | }
44 |
45 | function getMask(uf, value) {
46 | if(!uf || !ieMasks[uf]) {
47 | return undefined;
48 | }
49 | var _uf = uf.toUpperCase();
50 | if (_uf === 'SP' && /^P/i.test(value)) {
51 | return ieMasks.SP[1].mask;
52 | }
53 | var masks = ieMasks[uf];
54 | var i = 0;
55 | while(masks[i].chars && masks[i].chars < clearValue(value).length && i < masks.length - 1) {
56 | i++;
57 | }
58 | return masks[i].mask;
59 | }
60 |
61 | var mask = getMask(uf, value);
62 | if(!mask) {
63 | return value;
64 | }
65 | var processed = mask.process(clearValue(value));
66 | if (uf && uf.toUpperCase() === 'SP' && /^p/i.test(value)) {
67 | return 'P'+processed.result;
68 | }
69 | return processed.result;
70 | };
71 |
--------------------------------------------------------------------------------
/releases/br-masks.min.js:
--------------------------------------------------------------------------------
1 | !function(n,e){"function"==typeof define&&define.amd?define(["string-mask"],e):"object"==typeof exports?module.exports=e(require("string-mask")):n.BrM=e(n.StringMask)}(this,function(n){if(!n)throw new Error("StringMask not found");var e=function(e){var r=new n("00000-000");if(!e)return e;var a=r.process(e);return a.result},r=function(e){if(!e)return e;var r=new n("00.000.000"),a=r.apply(e);return a},a=function(e){if(!e)return e;var r=new n("00.000.000/0000-00"),a=r.apply(e);return a},t=function(n){return n&&n.length?n.length<=11?s(n):a(n):n},s=function(e){var r=new n("000.000.000-00");if(!e)return e;var a=r.apply(e);return a},w=function(e,r,a,t){r=!r&&0!==r||r<0?2:r,a=a||".",t=t||"";var s=r>0?a+new Array(r+1).join("0"):"",w="#"+t+"##0"+s;e=parseFloat(e),e||(e=0);var i=!1;e<0&&(e*=-1,i=!0);var u=new n(w,{reverse:!0}),f=u.apply(e.toFixed(r).replace(/[^\d]+/g,""));return i?"("+f+")":f},i=function(e,r){function a(n){return n.replace(/[^0-9]/g,"")}function t(n,e){if(n&&s[n]){var r=n.toUpperCase();if("SP"===r&&/^P/i.test(e))return s.SP[1].mask;for(var t=s[n],w=0;t[w].chars&&t[w].chars',
22 | ' * <%= pkg.description %>',
23 | ' * @version v<%= pkg.version %>',
24 | ' * @link <%= pkg.homepage %>',
25 | ' * @license <%= pkg.license %>',
26 | ' */',
27 | '(function (root, factory) {',
28 | ' /* istanbul ignore next */',
29 | ' if (typeof define === \'function\' && define.amd) {',
30 | ' // AMD. Register as an anonymous module.',
31 | ' define([\'string-mask\'], factory);',
32 | ' } else if (typeof exports === \'object\') {',
33 | ' // Node. Does not work with strict CommonJS, but',
34 | ' // only CommonJS-like environments that support module.exports,',
35 | ' // like Node.',
36 | ' module.exports = factory(require(\'string-mask\'));',
37 | ' } else {',
38 | ' // Browser globals (root is window)',
39 | ' root.BrM = factory(root.StringMask);',
40 | ' }',
41 | '}(this, function (StringMask) {',
42 | ' /* istanbul ignore if */',
43 | ' if (!StringMask) {',
44 | ' throw new Error(\'StringMask not found\');',
45 | ' }',
46 | ''].join('\n');
47 |
48 | var footer = ['',
49 | ' return {',
50 | ' ie: IE,',
51 | ' cpf: CPF,',
52 | ' cnpj: CNPJ,',
53 | ' cnpjBase: CNPJBASE,',
54 | ' phone: PHONE,',
55 | ' cep: CEP,',
56 | ' finance: FINANCE,',
57 | ' nfeAccessKey: NFEACCESSKEY,',
58 | ' cpfCnpj: CPFCNPJ',
59 | ' };',
60 | '}));'].join('\n');
61 |
62 | gulp.task('build:lib', function() {
63 | return gulp.src([
64 | config.src.files
65 | ])
66 | .pipe(plugins.concat('br-masks.js'))
67 | .pipe(plugins.header(header, {pkg: pkg}))
68 | .pipe(plugins.footer(footer))
69 | .pipe(gulp.dest('./releases'))
70 | .pipe(plugins.uglify())
71 | .pipe(plugins.concat('br-masks.min.js'))
72 | .pipe(gulp.dest('./releases'));
73 | });
74 |
75 | gulp.task('build:standalone', ['build:lib'], function() {
76 | return gulp.src([
77 | 'bower_components/string-mask/src/string-mask.js',
78 | 'releases/br-masks.js'
79 | ])
80 | .pipe(plugins.concat('br-masks-standalone.js'))
81 | .pipe(gulp.dest('./releases'))
82 | .pipe(plugins.uglify())
83 | .pipe(plugins.concat('br-masks-standalone.min.js'))
84 | .pipe(gulp.dest('./releases'));
85 | });
86 |
87 | gulp.task('build', ['build:standalone']);
88 |
89 | gulp.task('jshint', function(done) {
90 | gulp.src(config.src.files)
91 | .pipe(plugins.jshint('.jshintrc'))
92 | .pipe(plugins.jshint.reporter(jshintReporter));
93 | done();
94 | });
95 |
96 | function mochaRunnerFactory(reporter) {
97 | return plugins.mocha({
98 | reporter: reporter || 'spec'
99 | });
100 | }
101 |
102 | gulp.task('runtestdot', ['jshint', 'build'], function() {
103 | gulp.src(config.test.files, {read: false})
104 | .pipe(mochaRunnerFactory('dot'))
105 | .on('error', console.warn.bind(console));
106 | });
107 |
108 | gulp.task('runtest', ['jshint', 'build'], function() {
109 | gulp.src(config.test.files, {read: false})
110 | .pipe(mochaRunnerFactory())
111 | .on('error', console.warn.bind(console));
112 | });
113 |
114 | gulp.task('default', ['jshint', 'build', 'runtestdot'], function() {
115 | gulp.watch(config.src.files, ['jshint', 'build', 'runtestdot']);
116 | });
117 |
118 | gulp.task('test', ['jshint', 'build', 'runtest']);
119 |
120 | gulp.task('test-watch', ['jshint', 'build', 'runtest'], function() {
121 | gulp.watch(config.src.files, ['jshint', 'build', 'runtest']);
122 | });
123 |
124 | gulp.task('test-coverage', function(done) {
125 | gulp.src(config.src.release)
126 | .pipe(plugins.istanbul())
127 | .pipe(plugins.istanbul.hookRequire())
128 | .on('finish', function() {
129 | gulp.src(config.test.files, {
130 | cwd: process.env.PWD,
131 | read: false
132 | })
133 | .pipe(mochaRunnerFactory('spec'))
134 | .pipe(plugins.istanbul.writeReports())
135 | .on('end', function() {
136 | if (process.env.TRAVIS) {
137 | gulp.src('./coverage/**/lcov.info')
138 | .pipe(plugins.coveralls())
139 | .on('end', done);
140 | } else {
141 | done();
142 | }
143 | });
144 | });
145 | });
146 |
--------------------------------------------------------------------------------
/releases/br-masks-standalone.min.js:
--------------------------------------------------------------------------------
1 | !function(e,n){"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?module.exports=n():e.StringMask=n()}(this,function(){function e(e,n){for(var t=0,r=n-1,s={escape:!0};r>=0&&s&&s.escape;)s=a[e.charAt(r)],t+=s&&s.escape?1:0,r--;return t>0&&t%2===1}function n(e,n){var t=e.replace(/[^0]/g,"").length,r=n.replace(/[^\d]/g,"").length;return r-t}function t(e,n,t,r){return r&&"function"==typeof r.transform&&(n=r.transform(n)),t.reverse?n+e:e+n}function r(e,n,t){var s=e.charAt(n),i=a[s];return""!==s&&(!(!i||i.escape)||r(e,n+t,t))}function s(e,n,t){var r=e.split("");return r.splice(t>=0?t:0,0,n),r.join("")}function i(e,n){this.options=n||{},this.options={reverse:this.options.reverse||!1,usedefaults:this.options.usedefaults||this.options.reverse},this.pattern=e}var a={0:{pattern:/\d/,_default:"0"},9:{pattern:/\d/,optional:!0},"#":{pattern:/\d/,optional:!0,recursive:!0},S:{pattern:/[a-zA-Z]/},U:{pattern:/[a-zA-Z]/,transform:function(e){return e.toLocaleUpperCase()}},L:{pattern:/[a-zA-Z]/,transform:function(e){return e.toLocaleLowerCase()}},$:{escape:!0}};return i.prototype.process=function(i){function o(e){if(!w&&r(p,k,m.inc))return!0;if(w||(w=v.length>0),w){var n=v.shift();if(v.push(n),e.reverse&&c>=0)return k++,p=s(p,n,k),!0;if(!e.reverse&&c=0}if(!i)return"";i+="";for(var p=this.pattern,u=!0,f="",c=this.options.reverse?i.length-1:0,l=n(p,i),h=!1,v=[],w=!1,m={start:this.options.reverse?p.length-1:0,end:this.options.reverse?-1:p.length,inc:this.options.reverse?-1:1},k=m.start;o(this.options);k+=m.inc){var d=p.charAt(k),g=i.charAt(c),y=a[d];if(!w||g){if(this.options.reverse&&e(p,k)){f=t(f,d,this.options,y),k+=m.inc;continue}if(!this.options.reverse&&h){f=t(f,d,this.options,y),h=!1;continue}if(!this.options.reverse&&y&&y.escape){h=!0;continue}}if(!w&&y&&y.recursive)v.push(d);else{if(w&&!g){y&&y.recursive||(f=t(f,d,this.options,y));continue}if(v.length>0&&y&&!y.recursive){u=!1;continue}if(!w&&v.length>0&&!g)continue}if(y)if(y.optional){if(y.pattern.test(g)&&l)f=t(f,g,this.options,y),c+=m.inc,l--;else if(v.length>0&&g){u=!1;break}}else if(y.pattern.test(g))f=t(f,g,this.options,y),c+=m.inc;else{if(g||!y._default||!this.options.usedefaults){u=!1;break}f=t(f,y._default,this.options,y)}else f=t(f,d,this.options,y),!w&&v.length&&v.push(d)}return{result:f,valid:u}},i.prototype.apply=function(e){return this.process(e).result},i.prototype.validate=function(e){return this.process(e).valid},i.process=function(e,n,t){return new i(n,t).process(e)},i.apply=function(e,n,t){return new i(n,t).apply(e)},i.validate=function(e,n,t){return new i(n,t).validate(e)},i}),function(e,n){"function"==typeof define&&define.amd?define(["string-mask"],n):"object"==typeof exports?module.exports=n(require("string-mask")):e.BrM=n(e.StringMask)}(this,function(e){if(!e)throw new Error("StringMask not found");var n=function(n){var t=new e("00000-000");if(!n)return n;var r=t.process(n);return r.result},t=function(n){if(!n)return n;var t=new e("00.000.000"),r=t.apply(n);return r},r=function(n){if(!n)return n;var t=new e("00.000.000/0000-00"),r=t.apply(n);return r},s=function(e){return e&&e.length?e.length<=11?i(e):r(e):e},i=function(n){var t=new e("000.000.000-00");if(!n)return n;var r=t.apply(n);return r},a=function(n,t,r,s){t=!t&&0!==t||t<0?2:t,r=r||".",s=s||"";var i=t>0?r+new Array(t+1).join("0"):"",a="#"+s+"##0"+i;n=parseFloat(n),n||(n=0);var o=!1;n<0&&(n*=-1,o=!0);var p=new e(a,{reverse:!0}),u=p.apply(n.toFixed(t).replace(/[^\d]+/g,""));return o?"("+u+")":u},o=function(n,t){function r(e){return e.replace(/[^0-9]/g,"")}function s(e,n){if(e&&i[e]){var t=e.toUpperCase();if("SP"===t&&/^P/i.test(n))return i.SP[1].mask;for(var s=i[e],a=0;s[a].chars&&s[a].chars 0 ? decimalSep + new Array(precision + 1).join('0') : '';
86 | var maskPattern = '#'+groupSep+'##0'+decimalsPattern;
87 |
88 | value = parseFloat(value);
89 | if (!value) {
90 | value = 0;
91 | }
92 |
93 | var negative = false;
94 | if (value < 0) {
95 | value = value * -1;
96 | negative = true;
97 | }
98 | var financeMask = new StringMask(maskPattern, {reverse: true});
99 | var masked = financeMask.apply(value.toFixed(precision).replace(/[^\d]+/g,''));
100 | return negative ? '('+masked+')' : masked;
101 | };
102 |
103 | /*exported IE */
104 | var IE = function(value, uf) {
105 | if (!value || typeof value !== 'string') {
106 | return value;
107 | }
108 |
109 | var ieMasks = {
110 | 'AC': [{mask: new StringMask('00.000.000/000-00')}],
111 | 'AL': [{mask: new StringMask('000000000')}],
112 | 'AM': [{mask: new StringMask('00.000.000-0')}],
113 | 'AP': [{mask: new StringMask('000000000')}],
114 | 'BA': [{chars: 8, mask: new StringMask('000000-00')},
115 | {mask: new StringMask('0000000-00')}],
116 | 'CE': [{mask: new StringMask('00000000-0')}],
117 | 'DF': [{mask: new StringMask('00000000000-00')}],
118 | 'ES': [{mask: new StringMask('00000000-0')}],
119 | 'GO': [{mask: new StringMask('00.000.000-0')}],
120 | 'MA': [{mask: new StringMask('000000000')}],
121 | 'MG': [{mask: new StringMask('000.000.000/0000')}],
122 | 'MS': [{mask: new StringMask('000000000')}],
123 | 'MT': [{mask: new StringMask('0000000000-0')}],
124 | 'PA': [{mask: new StringMask('00-000000-0')}],
125 | 'PB': [{mask: new StringMask('00000000-0')}],
126 | 'PE': [{chars: 9, mask: new StringMask('0000000-00')},
127 | {mask: new StringMask('00.0.000.0000000-0')}],
128 | 'PI': [{mask: new StringMask('000000000')}],
129 | 'PR': [{mask: new StringMask('000.00000-00')}],
130 | 'RJ': [{mask: new StringMask('00.000.00-0')}],
131 | 'RN': [{chars: 9, mask: new StringMask('00.000.000-0')},
132 | {mask: new StringMask('00.0.000.000-0')}],
133 | 'RO': [{mask: new StringMask('0000000000000-0')}],
134 | 'RR': [{mask: new StringMask('00000000-0')}],
135 | 'RS': [{mask: new StringMask('000/0000000')}],
136 | 'SC': [{mask: new StringMask('000.000.000')}],
137 | 'SE': [{mask: new StringMask('00000000-0')}],
138 | 'SP': [{mask: new StringMask('000.000.000.000')},
139 | {mask: new StringMask('-00000000.0/000')}],
140 | 'TO': [{mask: new StringMask('00000000000')}]
141 | };
142 |
143 | function clearValue (value) {
144 | return value.replace(/[^0-9]/g, '');
145 | }
146 |
147 | function getMask(uf, value) {
148 | if(!uf || !ieMasks[uf]) {
149 | return undefined;
150 | }
151 | var _uf = uf.toUpperCase();
152 | if (_uf === 'SP' && /^P/i.test(value)) {
153 | return ieMasks.SP[1].mask;
154 | }
155 | var masks = ieMasks[uf];
156 | var i = 0;
157 | while(masks[i].chars && masks[i].chars < clearValue(value).length && i < masks.length - 1) {
158 | i++;
159 | }
160 | return masks[i].mask;
161 | }
162 |
163 | var mask = getMask(uf, value);
164 | if(!mask) {
165 | return value;
166 | }
167 | var processed = mask.process(clearValue(value));
168 | if (uf && uf.toUpperCase() === 'SP' && /^p/i.test(value)) {
169 | return 'P'+processed.result;
170 | }
171 | return processed.result;
172 | };
173 |
174 | /*exported NFEACCESSKEY */
175 | var NFEACCESSKEY = function(value) {
176 | if(!value) {
177 | return value;
178 | }
179 |
180 | var maskPattern = '0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000';
181 | var nfeMask = new StringMask(maskPattern);
182 | var formatedValue = nfeMask.apply(value);
183 | return formatedValue;
184 | };
185 |
186 | /*exported PHONE */
187 | var PHONE = function(value) {
188 | var phoneMask8D = new StringMask('(00) 0000-0000'),
189 | phoneMask9D = new StringMask('(00) 00000-0000'),
190 | phoneMask0800 = new StringMask('0000-000-0000');
191 |
192 | if(!value) {
193 | return value;
194 | }
195 |
196 | var formatedValue;
197 | value = value + '';
198 | if (value.indexOf('0800') === 0) {
199 | formatedValue = phoneMask0800.apply(value);
200 | }else if(value.length < 11){
201 | formatedValue = phoneMask8D.apply(value);
202 | }else{
203 | formatedValue = phoneMask9D.apply(value);
204 | }
205 |
206 | return formatedValue;
207 | };
208 |
209 | return {
210 | ie: IE,
211 | cpf: CPF,
212 | cnpj: CNPJ,
213 | cnpjBase: CNPJBASE,
214 | phone: PHONE,
215 | cep: CEP,
216 | finance: FINANCE,
217 | nfeAccessKey: NFEACCESSKEY,
218 | cpfCnpj: CPFCNPJ
219 | };
220 | }));
--------------------------------------------------------------------------------
/.jshintrc:
--------------------------------------------------------------------------------
1 | {
2 | // --------------------------------------------------------------------
3 | // JSHint Configuration, Strict Edition
4 | // --------------------------------------------------------------------
5 | //
6 | // This is a options template for [JSHint][1], using [JSHint example][2]
7 | // and [Ory Band's example][3] as basis and setting config values to
8 | // be most strict:
9 | //
10 | // * set all enforcing options to true
11 | // * set all relaxing options to false
12 | // * set all environment options to false, except the browser value
13 | // * set all JSLint legacy options to false
14 | //
15 | // [1]: http://www.jshint.com/
16 | // [2]: https://github.com/jshint/node-jshint/blob/master/example/config.json
17 | // [3]: https://github.com/oryband/dotfiles/blob/master/jshintrc
18 | //
19 | // @author http://michael.haschke.biz/
20 | // @license http://unlicense.org/
21 |
22 | // == Enforcing Options ===============================================
23 | //
24 | // These options tell JSHint to be more strict towards your code. Use
25 | // them if you want to allow only a safe subset of JavaScript, very
26 | // useful when your codebase is shared with a big number of developers
27 | // with different skill levels.
28 |
29 | "bitwise" : false, // Prohibit bitwise operators (&, |, ^, etc.).
30 | "camelcase" : true, // Force all variable names to use either camelCase style or UPPER_CASE with underscores.
31 | "curly" : true, // Require {} for every new block or scope.
32 | "eqeqeq" : true, // Require triple equals i.e. `===`.
33 | "forin" : true, // Tolerate `for in` loops without `hasOwnPrototype`.
34 | "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
35 | "latedef" : true, // Prohibit variable use before definition.
36 | "maxcomplexity" : 10, // Lets you control cyclomatic complexity throughout your code.
37 | "maxdepth" : 4, // Lets you control how nested do you want your blocks to be
38 | "maxlen" : 120, // Lets you set the maximum length of a line.
39 | "maxparams" : false, // Lets you set the max number of formal parameters allowed per function
40 | "maxstatements" : 20, // Lets you set the max number of statements allowed per function
41 | "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`.
42 | "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`.
43 | "noempty" : true, // Prohibit use of empty blocks.
44 | "nonew" : true, // Prohibit use of constructors for side-effects.
45 | "plusplus" : false, // Prohibit use of `++` & `--`.
46 | "quotmark" : "single", // Enforces the consistency of quotation marks used throughout your code.
47 | "regexp" : true, // Prohibit `.` and `[^...]` in regular expressions.
48 | "undef" : true, // Require all non-global variables be declared before they are used.
49 | "unused" : true, // Prohibit neved used defined variables.
50 | "strict" : false, // Require `use strict` pragma in every file.
51 | "trailing" : true, // Prohibit trailing whitespaces.
52 |
53 | // == Relaxing Options ================================================
54 | //
55 | // These options allow you to suppress certain types of warnings. Use
56 | // them only if you are absolutely positive that you know what you are
57 | // doing.
58 |
59 | "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons).
60 | "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments.
61 | "debug" : false, // Allow debugger statements e.g. browser breakpoints.
62 | "eqnull" : false, // Tolerate use of `== null`.
63 | "es5" : false, // Allow EcmaScript 5 syntax.
64 | "esnext" : false, // Allow ES.next specific features such as `const` and `let`.
65 | "evil" : false, // Tolerate use of `eval`.
66 | "expr" : true, // Tolerate `ExpressionStatement` as Programs.
67 | "funcscope" : false, // Tolerate declarations of variables inside of control structures while accessing them later from the outside.
68 | "globalstrict" : false, // Allow global "use strict" (also enables 'strict').
69 | "iterator" : false, // Allow usage of __iterator__ property.
70 | "lastsemic" : false, // Tolerat missing semicolons when the it is omitted for the last statement in a one-line block.
71 | "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons.
72 | "laxcomma" : false, // Suppress warnings about comma-first coding style.
73 | "loopfunc" : false, // Allow functions to be defined within loops.
74 | "multistr" : false, // Tolerate multi-line strings.
75 | "onecase" : false, // Tolerate switches with just one case.
76 | "proto" : false, // Tolerate __proto__ property. This property is deprecated.
77 | "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`.
78 | "scripturl" : false, // Tolerate script-targeted URLs.
79 | "smarttabs" : false, // Tolerate mixed tabs and spaces when the latter are used for alignmnent only.
80 | "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`.
81 | "sub" : false, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`.
82 | "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`.
83 | "validthis" : false, // Tolerate strict violations when the code is running in strict mode and you use this in a non-constructor function.
84 |
85 | // == Environments ====================================================
86 | //
87 | // These options pre-define global variables that are exposed by
88 | // popular JavaScript libraries and runtime environments—such as
89 | // browser or node.js.
90 |
91 | "browser" : true, // Standard browser globals e.g. `window`, `document`.
92 | "couch" : false, // Enable globals exposed by CouchDB.
93 | "devel" : false, // Allow development statements e.g. `console.log();`.
94 | "dojo" : false, // Enable globals exposed by Dojo Toolkit.
95 | "jquery" : false, // Enable globals exposed by jQuery JavaScript library.
96 | "mootools" : false, // Enable globals exposed by MooTools JavaScript framework.
97 | "node" : true, // Enable globals available when code is running inside of the NodeJS runtime environment.
98 | "nonstandard" : false, // Define non-standard but widely adopted globals such as escape and unescape.
99 | "prototypejs" : false, // Enable globals exposed by Prototype JavaScript framework.
100 | "rhino" : false, // Enable globals available when your code is running inside of the Rhino runtime environment.
101 | "wsh" : false, // Enable globals available when your code is running as a script for the Windows Script Host.
102 |
103 | // == Undocumented Options ============================================
104 | //
105 | // While I've found these options in [example1][2] and [example2][3]
106 | // they are not described in the [JSHint Options documentation][4].
107 | //
108 | // [4]: http://www.jshint.com/options/
109 | "globals": {
110 | "after": true,
111 | "angular": true,
112 | "before": true,
113 | "describe": true,
114 | "it": true,
115 | "StringMask": true
116 | },
117 | "predef" : [ // Extra globals.
118 | ],
119 | "indent" : 4 // Specify indentation spacing
120 | }
121 |
--------------------------------------------------------------------------------
/releases/br-masks-standalone.js:
--------------------------------------------------------------------------------
1 | (function (root, factory) {
2 | if (typeof define === 'function' && define.amd) {
3 | // AMD. Register as an anonymous module.
4 | define([], factory);
5 | } else if (typeof exports === 'object') {
6 | // Node. Does not work with strict CommonJS, but
7 | // only CommonJS-like environments that support module.exports,
8 | // like Node.
9 | module.exports = factory();
10 | } else {
11 | // Browser globals (root is window)
12 | root.StringMask = factory();
13 | }
14 | }(this, function () {
15 | var tokens = {
16 | '0': {pattern: /\d/, _default: '0'},
17 | '9': {pattern: /\d/, optional: true},
18 | '#': {pattern: /\d/, optional: true, recursive: true},
19 | 'S': {pattern: /[a-zA-Z]/},
20 | 'U': {pattern: /[a-zA-Z]/, transform: function (c) { return c.toLocaleUpperCase(); }},
21 | 'L': {pattern: /[a-zA-Z]/, transform: function (c) { return c.toLocaleLowerCase(); }},
22 | '$': {escape: true}
23 | };
24 |
25 | function isEscaped(pattern, pos) {
26 | var count = 0;
27 | var i = pos - 1;
28 | var token = {escape: true};
29 | while (i >= 0 && token && token.escape) {
30 | token = tokens[pattern.charAt(i)];
31 | count += token && token.escape ? 1 : 0;
32 | i--;
33 | }
34 | return count > 0 && count%2 === 1;
35 | }
36 |
37 | function calcOptionalNumbersToUse(pattern, value) {
38 | var numbersInP = pattern.replace(/[^0]/g,'').length;
39 | var numbersInV = value.replace(/[^\d]/g,'').length;
40 | return numbersInV - numbersInP;
41 | }
42 |
43 | function concatChar(text, character, options, token) {
44 | if (token && typeof token.transform === 'function') character = token.transform(character);
45 | if (options.reverse) return character + text;
46 | return text + character;
47 | }
48 |
49 | function hasMoreTokens(pattern, pos, inc) {
50 | var pc = pattern.charAt(pos);
51 | var token = tokens[pc];
52 | if (pc === '') return false;
53 | return token && !token.escape ? true : hasMoreTokens(pattern, pos + inc, inc);
54 | }
55 |
56 | function insertChar(text, char, position) {
57 | var t = text.split('');
58 | t.splice(position >= 0 ? position: 0, 0, char);
59 | return t.join('');
60 | }
61 |
62 | function StringMask(pattern, opt) {
63 | this.options = opt || {};
64 | this.options = {
65 | reverse: this.options.reverse || false,
66 | usedefaults: this.options.usedefaults || this.options.reverse
67 | };
68 | this.pattern = pattern;
69 | }
70 |
71 | StringMask.prototype.process = function proccess(value) {
72 | if (!value) return '';
73 | value = value + '';
74 | var pattern2 = this.pattern;
75 | var valid = true;
76 | var formatted = '';
77 | var valuePos = this.options.reverse ? value.length - 1 : 0;
78 | var optionalNumbersToUse = calcOptionalNumbersToUse(pattern2, value);
79 | var escapeNext = false;
80 | var recursive = [];
81 | var inRecursiveMode = false;
82 |
83 | var steps = {
84 | start: this.options.reverse ? pattern2.length - 1 : 0,
85 | end: this.options.reverse ? -1 : pattern2.length,
86 | inc: this.options.reverse ? -1 : 1
87 | };
88 |
89 | function continueCondition(options) {
90 | if (!inRecursiveMode && hasMoreTokens(pattern2, i, steps.inc)) {
91 | return true;
92 | } else if (!inRecursiveMode) {
93 | inRecursiveMode = recursive.length > 0;
94 | }
95 |
96 | if (inRecursiveMode) {
97 | var pc = recursive.shift();
98 | recursive.push(pc);
99 | if (options.reverse && valuePos >= 0) {
100 | i++;
101 | pattern2 = insertChar(pattern2, pc, i);
102 | return true;
103 | } else if (!options.reverse && valuePos < value.length) {
104 | pattern2 = insertChar(pattern2, pc, i);
105 | return true;
106 | }
107 | }
108 | return i < pattern2.length && i >= 0;
109 | }
110 |
111 | for (var i = steps.start; continueCondition(this.options); i = i + steps.inc) {
112 | var pc = pattern2.charAt(i);
113 | var vc = value.charAt(valuePos);
114 | var token = tokens[pc];
115 | if (!inRecursiveMode || vc) {
116 | if (this.options.reverse && isEscaped(pattern2, i)) {
117 | formatted = concatChar(formatted, pc, this.options, token);
118 | i = i + steps.inc;
119 | continue;
120 | } else if (!this.options.reverse && escapeNext) {
121 | formatted = concatChar(formatted, pc, this.options, token);
122 | escapeNext = false;
123 | continue;
124 | } else if (!this.options.reverse && token && token.escape) {
125 | escapeNext = true;
126 | continue;
127 | }
128 | }
129 |
130 | if (!inRecursiveMode && token && token.recursive) {
131 | recursive.push(pc);
132 | } else if (inRecursiveMode && !vc) {
133 | if (!token || !token.recursive) formatted = concatChar(formatted, pc, this.options, token);
134 | continue;
135 | } else if (recursive.length > 0 && token && !token.recursive) {
136 | // Recursive tokens most be the last tokens of the pattern
137 | valid = false;
138 | continue;
139 | } else if (!inRecursiveMode && recursive.length > 0 && !vc) {
140 | continue;
141 | }
142 |
143 | if (!token) {
144 | formatted = concatChar(formatted, pc, this.options, token);
145 | if (!inRecursiveMode && recursive.length) {
146 | recursive.push(pc);
147 | }
148 | } else if (token.optional) {
149 | if (token.pattern.test(vc) && optionalNumbersToUse) {
150 | formatted = concatChar(formatted, vc, this.options, token);
151 | valuePos = valuePos + steps.inc;
152 | optionalNumbersToUse--;
153 | } else if (recursive.length > 0 && vc) {
154 | valid = false;
155 | break;
156 | }
157 | } else if (token.pattern.test(vc)) {
158 | formatted = concatChar(formatted, vc, this.options, token);
159 | valuePos = valuePos + steps.inc;
160 | } else if (!vc && token._default && this.options.usedefaults) {
161 | formatted = concatChar(formatted, token._default, this.options, token);
162 | } else {
163 | valid = false;
164 | break;
165 | }
166 | }
167 |
168 | return {result: formatted, valid: valid};
169 | };
170 |
171 | StringMask.prototype.apply = function(value) {
172 | return this.process(value).result;
173 | };
174 |
175 | StringMask.prototype.validate = function(value) {
176 | return this.process(value).valid;
177 | };
178 |
179 | StringMask.process = function(value, pattern, options) {
180 | return new StringMask(pattern, options).process(value);
181 | };
182 |
183 | StringMask.apply = function(value, pattern, options) {
184 | return new StringMask(pattern, options).apply(value);
185 | };
186 |
187 | StringMask.validate = function(value, pattern, options) {
188 | return new StringMask(pattern, options).validate(value);
189 | };
190 |
191 | return StringMask;
192 | }));
193 |
194 | /**
195 | * br-masks
196 | * A library of masks applicable to several Brazilian data like I.E., CNPJ, CPF and others
197 | * @version v0.5.0
198 | * @link http://github.com/the-darc/br-masks
199 | * @license MIT
200 | */
201 | (function (root, factory) {
202 | /* istanbul ignore next */
203 | if (typeof define === 'function' && define.amd) {
204 | // AMD. Register as an anonymous module.
205 | define(['string-mask'], factory);
206 | } else if (typeof exports === 'object') {
207 | // Node. Does not work with strict CommonJS, but
208 | // only CommonJS-like environments that support module.exports,
209 | // like Node.
210 | module.exports = factory(require('string-mask'));
211 | } else {
212 | // Browser globals (root is window)
213 | root.BrM = factory(root.StringMask);
214 | }
215 | }(this, function (StringMask) {
216 | /* istanbul ignore if */
217 | if (!StringMask) {
218 | throw new Error('StringMask not found');
219 | }
220 | /*exported CEP */
221 | var CEP = function(value) {
222 | var cepMask = new StringMask('00000-000');
223 | if(!value) {
224 | return value;
225 | }
226 | var processed = cepMask.process(value);
227 | return processed.result;
228 | };
229 |
230 | /*exported CNPJBASE */
231 | var CNPJBASE = function(value) {
232 | if(!value) {
233 | return value;
234 | }
235 | var cnpjBasePattern = new StringMask('00.000.000');
236 | var formatedValue = cnpjBasePattern.apply(value);
237 | return formatedValue;
238 | };
239 |
240 | /*exported CNPJ */
241 | var CNPJ = function(value) {
242 | if(!value) {
243 | return value;
244 | }
245 | var cnpjPattern = new StringMask('00.000.000\/0000-00');
246 | var formatedValue = cnpjPattern.apply(value);
247 | return formatedValue;
248 | };
249 |
250 | /*exported CPFCNPJ */
251 | /*globals CPF, CNPJ*/
252 | var CPFCNPJ = function(value) {
253 | if (!value || !value.length) {
254 | return value;
255 | } else if (value.length <= 11) {
256 | return CPF(value);
257 | } else {
258 | return CNPJ(value);
259 | }
260 | };
261 |
262 | /*exported CPF */
263 | var CPF = function(value) {
264 | var cpfPattern = new StringMask('000.000.000-00');
265 | if(!value) {
266 | return value;
267 | }
268 | var formatedValue = cpfPattern.apply(value);
269 | return formatedValue;
270 | };
271 |
272 | /*exported FINANCE */
273 | var FINANCE = function(value, precision, decimalSep, groupSep) {
274 | precision = (!precision && precision !== 0) || precision < 0? 2 : precision;
275 | decimalSep = decimalSep || '.';
276 | groupSep = groupSep || '';
277 |
278 | var decimalsPattern = precision > 0 ? decimalSep + new Array(precision + 1).join('0') : '';
279 | var maskPattern = '#'+groupSep+'##0'+decimalsPattern;
280 |
281 | value = parseFloat(value);
282 | if (!value) {
283 | value = 0;
284 | }
285 |
286 | var negative = false;
287 | if (value < 0) {
288 | value = value * -1;
289 | negative = true;
290 | }
291 | var financeMask = new StringMask(maskPattern, {reverse: true});
292 | var masked = financeMask.apply(value.toFixed(precision).replace(/[^\d]+/g,''));
293 | return negative ? '('+masked+')' : masked;
294 | };
295 |
296 | /*exported IE */
297 | var IE = function(value, uf) {
298 | if (!value || typeof value !== 'string') {
299 | return value;
300 | }
301 |
302 | var ieMasks = {
303 | 'AC': [{mask: new StringMask('00.000.000/000-00')}],
304 | 'AL': [{mask: new StringMask('000000000')}],
305 | 'AM': [{mask: new StringMask('00.000.000-0')}],
306 | 'AP': [{mask: new StringMask('000000000')}],
307 | 'BA': [{chars: 8, mask: new StringMask('000000-00')},
308 | {mask: new StringMask('0000000-00')}],
309 | 'CE': [{mask: new StringMask('00000000-0')}],
310 | 'DF': [{mask: new StringMask('00000000000-00')}],
311 | 'ES': [{mask: new StringMask('00000000-0')}],
312 | 'GO': [{mask: new StringMask('00.000.000-0')}],
313 | 'MA': [{mask: new StringMask('000000000')}],
314 | 'MG': [{mask: new StringMask('000.000.000/0000')}],
315 | 'MS': [{mask: new StringMask('000000000')}],
316 | 'MT': [{mask: new StringMask('0000000000-0')}],
317 | 'PA': [{mask: new StringMask('00-000000-0')}],
318 | 'PB': [{mask: new StringMask('00000000-0')}],
319 | 'PE': [{chars: 9, mask: new StringMask('0000000-00')},
320 | {mask: new StringMask('00.0.000.0000000-0')}],
321 | 'PI': [{mask: new StringMask('000000000')}],
322 | 'PR': [{mask: new StringMask('000.00000-00')}],
323 | 'RJ': [{mask: new StringMask('00.000.00-0')}],
324 | 'RN': [{chars: 9, mask: new StringMask('00.000.000-0')},
325 | {mask: new StringMask('00.0.000.000-0')}],
326 | 'RO': [{mask: new StringMask('0000000000000-0')}],
327 | 'RR': [{mask: new StringMask('00000000-0')}],
328 | 'RS': [{mask: new StringMask('000/0000000')}],
329 | 'SC': [{mask: new StringMask('000.000.000')}],
330 | 'SE': [{mask: new StringMask('00000000-0')}],
331 | 'SP': [{mask: new StringMask('000.000.000.000')},
332 | {mask: new StringMask('-00000000.0/000')}],
333 | 'TO': [{mask: new StringMask('00000000000')}]
334 | };
335 |
336 | function clearValue (value) {
337 | return value.replace(/[^0-9]/g, '');
338 | }
339 |
340 | function getMask(uf, value) {
341 | if(!uf || !ieMasks[uf]) {
342 | return undefined;
343 | }
344 | var _uf = uf.toUpperCase();
345 | if (_uf === 'SP' && /^P/i.test(value)) {
346 | return ieMasks.SP[1].mask;
347 | }
348 | var masks = ieMasks[uf];
349 | var i = 0;
350 | while(masks[i].chars && masks[i].chars < clearValue(value).length && i < masks.length - 1) {
351 | i++;
352 | }
353 | return masks[i].mask;
354 | }
355 |
356 | var mask = getMask(uf, value);
357 | if(!mask) {
358 | return value;
359 | }
360 | var processed = mask.process(clearValue(value));
361 | if (uf && uf.toUpperCase() === 'SP' && /^p/i.test(value)) {
362 | return 'P'+processed.result;
363 | }
364 | return processed.result;
365 | };
366 |
367 | /*exported NFEACCESSKEY */
368 | var NFEACCESSKEY = function(value) {
369 | if(!value) {
370 | return value;
371 | }
372 |
373 | var maskPattern = '0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000';
374 | var nfeMask = new StringMask(maskPattern);
375 | var formatedValue = nfeMask.apply(value);
376 | return formatedValue;
377 | };
378 |
379 | /*exported PHONE */
380 | var PHONE = function(value) {
381 | var phoneMask8D = new StringMask('(00) 0000-0000'),
382 | phoneMask9D = new StringMask('(00) 00000-0000'),
383 | phoneMask0800 = new StringMask('0000-000-0000');
384 |
385 | if(!value) {
386 | return value;
387 | }
388 |
389 | var formatedValue;
390 | value = value + '';
391 | if (value.indexOf('0800') === 0) {
392 | formatedValue = phoneMask0800.apply(value);
393 | }else if(value.length < 11){
394 | formatedValue = phoneMask8D.apply(value);
395 | }else{
396 | formatedValue = phoneMask9D.apply(value);
397 | }
398 |
399 | return formatedValue;
400 | };
401 |
402 | return {
403 | ie: IE,
404 | cpf: CPF,
405 | cnpj: CNPJ,
406 | cnpjBase: CNPJBASE,
407 | phone: PHONE,
408 | cep: CEP,
409 | finance: FINANCE,
410 | nfeAccessKey: NFEACCESSKEY,
411 | cpfCnpj: CPFCNPJ
412 | };
413 | }));
--------------------------------------------------------------------------------
/test/ie.test.js:
--------------------------------------------------------------------------------
1 | var should = require('should'),
2 | BrM = require('../releases/br-masks');
3 |
4 | describe('I.E. ', function() {
5 | it('should not mask empty values', function(done) {
6 | should(BrM.ie(null)).be.eql(null);
7 | should(BrM.ie(undefined)).be.eql(undefined);
8 | should(BrM.ie('')).be.eql('');
9 | should(BrM.ie(0)).be.eql(0);
10 | should(BrM.ie(false)).be.eql(false);
11 | done();
12 | });
13 |
14 | it('should not mask invalid UFs', function(done) {
15 | should(BrM.ie('032141840', null)).be.eql('032141840');
16 | should(BrM.ie('18100100000059', 'XX')).be.eql('18100100000059');
17 | done();
18 | });
19 |
20 | describe('I.E.: Group01', function() {
21 | describe('- PE', function() {
22 | var uf = 'PE';
23 | it('Shoud mask 0321418-40', function(done) {
24 | should(BrM.ie('032141840', uf)).be.eql('0321418-40');
25 | done();
26 | });
27 | it('should mask 1321418-40', function(done) {
28 | should(BrM.ie('132141840', uf)).be.eql('1321418-40');
29 | done();
30 | });
31 | it('Shoud mask 18.1.001.0000004-9', function(done) {
32 | should(BrM.ie('18100100000049', uf)).be.eql('18.1.001.0000004-9');
33 | done();
34 | });
35 | it('should mask 18.1.001.0000005-9', function(done) {
36 | should(BrM.ie('18100100000059', uf)).be.eql('18.1.001.0000005-9');
37 | done();
38 | });
39 | it('should not mask numbers values', function(done) {
40 | should(BrM.ie(032141840, uf)).be.eql(32141840);
41 | done();
42 | });
43 | });
44 | describe('- RS', function() {
45 | var uf = 'RS';
46 | it('Shoud mask 224/3658792', function(done) {
47 | should(BrM.ie('2243658792', uf)).be.eql('224/3658792');
48 | done();
49 | });
50 | it('should mask 224/4658792', function(done) {
51 | should(BrM.ie('2244658792', uf)).be.eql('224/4658792');
52 | done();
53 | });
54 | });
55 | describe('- AC', function() {
56 | var uf = 'AC';
57 | it('Shoud mask 01.004.823/001-12', function(done) {
58 | should(BrM.ie('0100482300112', uf)).be.eql('01.004.823/001-12');
59 | done();
60 | });
61 | it('should mask 01.004.923/201-12', function(done) {
62 | should(BrM.ie('0100492320112', uf)).be.eql('01.004.923/201-12');
63 | done();
64 | });
65 | it('should mask 02.004.923/201-55', function(done) {
66 | should(BrM.ie('0200492320155', uf)).be.eql('02.004.923/201-55');
67 | done();
68 | });
69 | });
70 | describe('- MG', function() {
71 | var uf = 'MG';
72 | it('Shoud mask 062.307.904/0081', function(done) {
73 | should(BrM.ie('0623079040081', uf)).be.eql('062.307.904/0081');
74 | done();
75 | });
76 | it('should mask 062.347.934/0081', function(done) {
77 | should(BrM.ie('0623479340081', uf)).be.eql('062.347.934/0081');
78 | done();
79 | });
80 | });
81 | describe('- SP', function() {
82 | var uf = 'SP';
83 | it('Shoud mask 110.042.490.114', function(done) {
84 | should(BrM.ie('110042490114', uf)).be.eql('110.042.490.114');
85 | done();
86 | });
87 | it('should mask 110.045.490.124', function(done) {
88 | should(BrM.ie('110045490124', uf)).be.eql('110.045.490.124');
89 | done();
90 | });
91 | it('should mask 011.004.243.002', function(done) {
92 | should(BrM.ie('011004243002', uf)).be.eql('011.004.243.002');
93 | done();
94 | });
95 | it('Shoud mask P-01100424.3/002', function(done) {
96 | should(BrM.ie('P011004243002', uf)).be.eql('P-01100424.3/002');
97 | done();
98 | });
99 | it('should mask P-358874770.9/710', function(done) {
100 | should(BrM.ie('P358874779710', uf)).be.eql('P-35887477.9/710');
101 | done();
102 | });
103 | it('Shoud mask p-01100524.3/002', function(done) {
104 | should(BrM.ie('p011005240002', uf)).be.eql('P-01100524.0/002');
105 | done();
106 | });
107 | });
108 | describe('- DF', function() {
109 | var uf = 'DF';
110 | it('Shoud mask 07300001001-09', function(done) {
111 | should(BrM.ie('0730000100109', uf)).be.eql('07300001001-09');
112 | done();
113 | });
114 | it('should mask 07301002001-09', function(done) {
115 | should(BrM.ie('0730100200109', uf)).be.eql('07301002001-09');
116 | done();
117 | });
118 | });
119 | describe('- ES', function() {
120 | var uf = 'ES';
121 | it('Shoud mask 99999999-0', function(done) {
122 | should(BrM.ie('999999990', uf)).be.eql('99999999-0');
123 | done();
124 | });
125 | it('Shoud mask 19871230-8', function(done) {
126 | should(BrM.ie('198712308', uf)).be.eql('19871230-8');
127 | done();
128 | });
129 | it('should mask 99912999-0', function(done) {
130 | should(BrM.ie('999129990', uf)).be.eql('99912999-0');
131 | done();
132 | });
133 | });
134 | describe('- BA', function() {
135 | var uf = 'BA';
136 | describe('BA: 8 digits', function() {
137 | it('Shoud mask 090493-87', function(done) {
138 | should(BrM.ie('09049387', uf)).be.eql('090493-87');
139 | done();
140 | });
141 | it('Shoud mask 123456-63', function(done) {
142 | should(BrM.ie('12345663', uf)).be.eql('123456-63');
143 | done();
144 | });
145 | it('Shoud mask 290493-30', function(done) {
146 | should(BrM.ie('29049303', uf)).be.eql('290493-03');
147 | done();
148 | });
149 | it('Shoud mask 390493-60', function(done) {
150 | should(BrM.ie('39049366', uf)).be.eql('390493-66');
151 | done();
152 | });
153 | it('Shoud mask 490493-29', function(done) {
154 | should(BrM.ie('49049329', uf)).be.eql('490493-29');
155 | done();
156 | });
157 | it('Shoud mask 590493-20', function(done) {
158 | should(BrM.ie('59049382', uf)).be.eql('590493-82');
159 | done();
160 | });
161 | it('Shoud mask 890493-10', function(done) {
162 | should(BrM.ie('89049361', uf)).be.eql('890493-61');
163 | done();
164 | });
165 | it('should mask 026456-63', function(done) {
166 | should(BrM.ie('02645663', uf)).be.eql('026456-63');
167 | done();
168 | });
169 |
170 | it('Shoud mask 612345-57', function(done) {
171 | should(BrM.ie('61234557', uf)).be.eql('612345-57');
172 | done();
173 | });
174 | it('Shoud mask 723024-69', function(done) {
175 | should(BrM.ie('72302469', uf)).be.eql('723024-69');
176 | done();
177 | });
178 | it('Shoud mask 923024-76', function(done) {
179 | should(BrM.ie('92302476', uf)).be.eql('923024-76');
180 | done();
181 | });
182 | it('should mask 602355-57', function(done) {
183 | should(BrM.ie('60235557', uf)).be.eql('602355-57');
184 | done();
185 | });
186 | });
187 | describe('BA: 9 digits', function() {
188 | it('Shoud mask 0629333-16', function(done) {
189 | should(BrM.ie('062933316', uf)).be.eql('0629333-16');
190 | done();
191 | });
192 | it('Shoud mask 1000003-06', function(done) {
193 | should(BrM.ie('100000306', uf)).be.eql('1000003-06');
194 | done();
195 | });
196 | it('Shoud mask 2190493-84', function(done) {
197 | should(BrM.ie('219049384', uf)).be.eql('2190493-84');
198 | done();
199 | });
200 | it('Shoud mask 3190493-56', function(done) {
201 | should(BrM.ie('319049356', uf)).be.eql('3190493-56');
202 | done();
203 | });
204 | it('Shoud mask 4190493-28', function(done) {
205 | should(BrM.ie('419049328', uf)).be.eql('4190493-28');
206 | done();
207 | });
208 | it('Shoud mask 5190493-00', function(done) {
209 | should(BrM.ie('519049390', uf)).be.eql('5190493-90');
210 | done();
211 | });
212 | it('Shoud mask 8190493-06', function(done) {
213 | should(BrM.ie('819049306', uf)).be.eql('8190493-06');
214 | done();
215 | });
216 | it('should mask 1010203-06', function(done) {
217 | should(BrM.ie('101020306', uf)).be.eql('1010203-06');
218 | done();
219 | });
220 |
221 | it('Shoud mask 6112345-05', function(done) {
222 | should(BrM.ie('611234535', uf)).be.eql('6112345-35');
223 | done();
224 | });
225 | it('Shoud mask 7123024-06', function(done) {
226 | should(BrM.ie('712302456', uf)).be.eql('7123024-56');
227 | done();
228 | });
229 | it('Shoud mask 9123024-90', function(done) {
230 | should(BrM.ie('912302490', uf)).be.eql('9123024-90');
231 | done();
232 | });
233 | it('should mask 6102355-57', function(done) {
234 | should(BrM.ie('610235557', uf)).be.eql('6102355-57');
235 | done();
236 | });
237 | });
238 | });
239 | describe('- AM', function() {
240 | var uf = 'AM';
241 | it('Shoud mask 99.999.999-0', function(done) {
242 | should(BrM.ie('999999990', uf)).be.eql('99.999.999-0');
243 | done();
244 | });
245 | it('Shoud mask 19.871.230-8', function(done) {
246 | should(BrM.ie('198712308', uf)).be.eql('19.871.230-8');
247 | done();
248 | });
249 | it('should mask 99.912.999-0', function(done) {
250 | should(BrM.ie('999129990', uf)).be.eql('99.912.999-0');
251 | done();
252 | });
253 | });
254 | describe('- RN', function() {
255 | var uf = 'RN';
256 | it('Shoud mask 20.040.040-1', function(done) {
257 | should(BrM.ie('200400401', uf)).be.eql('20.040.040-1');
258 | done();
259 | });
260 | it('should mask 20.042.140-1', function(done) {
261 | should(BrM.ie('200421401', uf)).be.eql('20.042.140-1');
262 | done();
263 | });
264 | it('Shoud mask 20.0.040.040-0', function(done) {
265 | should(BrM.ie('2000400400', uf)).be.eql('20.0.040.040-0');
266 | done();
267 | });
268 | it('Shoud mask 20.0.340.040-0', function(done) {
269 | should(BrM.ie('2003400400', uf)).be.eql('20.0.340.040-0');
270 | done();
271 | });
272 | it('should mask 20.0.341.140-0', function(done) {
273 | should(BrM.ie('2003411400', uf)).be.eql('20.0.341.140-0');
274 | done();
275 | });
276 | });
277 | describe('- RO', function() {
278 | var uf = 'RO';
279 | it('Shoud mask 0000000062521-3', function(done) {
280 | should(BrM.ie('00000000625213', uf)).be.eql('0000000062521-3');
281 | done();
282 | });
283 | it('Shoud mask 0601230662521-7', function(done) {
284 | should(BrM.ie('06012306625217', uf)).be.eql('0601230662521-7');
285 | done();
286 | });
287 | it('should mask 0601230662521-3', function(done) {
288 | should(BrM.ie('06012306625213', uf)).be.eql('0601230662521-3');
289 | done();
290 | });
291 | });
292 | describe('- PR', function() {
293 | var uf = 'PR';
294 | it('Shoud mask 123.45678-50', function(done) {
295 | should(BrM.ie('1234567850', uf)).be.eql('123.45678-50');
296 | done();
297 | });
298 | it('Shoud mask 153.07274-47', function(done) {
299 | should(BrM.ie('1530727447', uf)).be.eql('153.07274-47');
300 | done();
301 | });
302 | it('should mask 153.07274-50', function(done) {
303 | should(BrM.ie('1530727450', uf)).be.eql('153.07274-50');
304 | done();
305 | });
306 | });
307 | });
308 | describe('I.E.: Group02', function() {
309 | describe('- SC', function() {
310 | var uf = 'SC';
311 | it('Shoud mask 251.040.852', function(done) {
312 | should(BrM.ie('251040852', uf)).be.eql('251.040.852');
313 | done();
314 | });
315 | it('Shoud mask 251.341.852', function(done) {
316 | should(BrM.ie('251341852', uf)).be.eql('251.341.852');
317 | done();
318 | });
319 | it('should mask 251.321.852', function(done) {
320 | should(BrM.ie('251321852', uf)).be.eql('251.321.852');
321 | done();
322 | });
323 | });
324 | describe('- RJ', function() {
325 | var uf = 'RJ';
326 | it('Shoud mask 99.999.99-3', function(done) {
327 | should(BrM.ie('99999993', uf)).be.eql('99.999.99-3');
328 | done();
329 | });
330 | it('Shoud mask 40.732.12-8', function(done) {
331 | should(BrM.ie('40732128', uf)).be.eql('40.732.12-8');
332 | done();
333 | });
334 | it('should mask 40.732.12-3', function(done) {
335 | should(BrM.ie('40732123', uf)).be.eql('40.732.12-3');
336 | done();
337 | });
338 | });
339 | describe('- PA', function() {
340 | var uf = 'PA';
341 | it('Shoud mask 15-999999-5', function(done) {
342 | should(BrM.ie('159999995', uf)).be.eql('15-999999-5');
343 | done();
344 | });
345 | it('Shoud mask 15-740351-3', function(done) {
346 | should(BrM.ie('157403513', uf)).be.eql('15-740351-3');
347 | done();
348 | });
349 | it('should mask 15-740351-5', function(done) {
350 | should(BrM.ie('157403515', uf)).be.eql('15-740351-5');
351 | done();
352 | });
353 | it('should mask 14-740351-0', function(done) {
354 | should(BrM.ie('147403510', uf)).be.eql('14-740351-0');
355 | done();
356 | });
357 | });
358 | describe('- SE', function() {
359 | var uf = 'SE';
360 | it('Shoud mask 27123456-3', function(done) {
361 | should(BrM.ie('271234563', uf)).be.eql('27123456-3');
362 | done();
363 | });
364 | it('should mask 15740351-5', function(done) {
365 | should(BrM.ie('157403515', uf)).be.eql('15740351-5');
366 | done();
367 | });
368 | });
369 | describe('- PB', function() {
370 | var uf = 'PB';
371 | it('Shoud mask 06000001-5', function(done) {
372 | should(BrM.ie('060000015', uf)).be.eql('06000001-5');
373 | done();
374 | });
375 | it('should mask 15740351-5', function(done) {
376 | should(BrM.ie('157403515', uf)).be.eql('15740351-5');
377 | done();
378 | });
379 | });
380 | describe('- CE', function() {
381 | var uf = 'CE';
382 | it('Shoud mask 06000001-5', function(done) {
383 | should(BrM.ie('060000015', uf)).be.eql('06000001-5');
384 | done();
385 | });
386 | it('should mask 15740351-5', function(done) {
387 | should(BrM.ie('157403515', uf)).be.eql('15740351-5');
388 | done();
389 | });
390 | });
391 | describe('- PI', function() {
392 | var uf = 'PI';
393 | it('Shoud mask 012345679', function(done) {
394 | should(BrM.ie('012345679', uf)).be.eql('012345679');
395 | done();
396 | });
397 | it('Shoud mask 060000015', function(done) {
398 | should(BrM.ie('060000015', uf)).be.eql('060000015');
399 | done();
400 | });
401 | it('Shoud mask 147403510', function(done) {
402 | should(BrM.ie('147403510', uf)).be.eql('147403510');
403 | done();
404 | });
405 | it('should mask 157403515', function(done) {
406 | should(BrM.ie('157403515', uf)).be.eql('157403515');
407 | done();
408 | });
409 | });
410 | describe('- MA', function() {
411 | var uf = 'MA';
412 | it('Shoud mask 120000385', function(done) {
413 | should(BrM.ie('120000385', uf)).be.eql('120000385');
414 | done();
415 | });
416 | it('should mask 060000015', function(done) {
417 | should(BrM.ie('060000015', uf)).be.eql('060000015');
418 | done();
419 | });
420 | it('should mask 127403510', function(done) {
421 | should(BrM.ie('127403510', uf)).be.eql('127403510');
422 | done();
423 | });
424 | it('Shoud mask 127403515', function(done) {
425 | should(BrM.ie('127403515', uf)).be.eql('127403515');
426 | done();
427 | });
428 | });
429 | describe('- MT', function() {
430 | var uf = 'MT';
431 | it('Shoud mask 0013000001-9', function(done) {
432 | should(BrM.ie('00130000019', uf)).be.eql('0013000001-9');
433 | done();
434 | });
435 | it('Shoud mask 0013046011-7', function(done) {
436 | should(BrM.ie('00130460117', uf)).be.eql('0013046011-7');
437 | done();
438 | });
439 | it('should mask 0013046011-9', function(done) {
440 | should(BrM.ie('00130460119', uf)).be.eql('0013046011-9');
441 | done();
442 | });
443 | });
444 | describe('- MS', function() {
445 | var uf = 'MS';
446 | it('Shoud mask 285730383', function(done) {
447 | should(BrM.ie('280000383', uf)).be.eql('280000383');
448 | done();
449 | });
450 | it('should mask 127403515', function(done) {
451 | should(BrM.ie('127403515', uf)).be.eql('127403515');
452 | done();
453 | });
454 | it('should mask 280000015', function(done) {
455 | should(BrM.ie('280000015', uf)).be.eql('280000015');
456 | done();
457 | });
458 | });
459 | describe('- TO', function() {
460 | var uf = 'TO';
461 | it('Shoud mask 29010227836', function(done) {
462 | should(BrM.ie('29010227836', uf)).be.eql('29010227836');
463 | done();
464 | });
465 | it('should mask 29010237336', function(done) {
466 | should(BrM.ie('29010237336', uf)).be.eql('29010237336');
467 | done();
468 | });
469 | it('should mask 29090227836', function(done) {
470 | should(BrM.ie('29090227836', uf)).be.eql('29090227836');
471 | done();
472 | });
473 | });
474 | describe('- AL', function() {
475 | var uf = 'AL';
476 | it('Shoud mask 240000048', function(done) {
477 | should(BrM.ie('240000048', uf)).be.eql('240000048');
478 | done();
479 | });
480 | it('Shoud mask 240273044', function(done) {
481 | should(BrM.ie('240273044', uf)).be.eql('240273044');
482 | done();
483 | });
484 | it('should mask 241178045', function(done) {
485 | should(BrM.ie('241178045', uf)).be.eql('241178045');
486 | done();
487 | });
488 | it('should mask 213178044', function(done) {
489 | should(BrM.ie('213178044', uf)).be.eql('213178044');
490 | done();
491 | });
492 | it('should mask 240178040', function(done) {
493 | should(BrM.ie('240178040', uf)).be.eql('240178040');
494 | done();
495 | });
496 | });
497 | describe('- RR', function() {
498 | var uf = 'RR';
499 | it('Shoud mask 24006628-1', function(done) {
500 | should(BrM.ie('240066281', uf)).be.eql('24006628-1');
501 | done();
502 | });
503 | it('Shoud mask 24001755-6', function(done) {
504 | should(BrM.ie('240017556', uf)).be.eql('24001755-6');
505 | done();
506 | });
507 | it('Shoud mask 24003429-0', function(done) {
508 | should(BrM.ie('240034290', uf)).be.eql('24003429-0');
509 | done();
510 | });
511 | it('Shoud mask 24001360-3', function(done) {
512 | should(BrM.ie('240013603', uf)).be.eql('24001360-3');
513 | done();
514 | });
515 | it('Shoud mask 24008266-8', function(done) {
516 | should(BrM.ie('240082668', uf)).be.eql('24008266-8');
517 | done();
518 | });
519 | it('should mask 24002676-8', function(done) {
520 | should(BrM.ie('240026768', uf)).be.eql('24002676-8');
521 | done();
522 | });
523 | });
524 | describe('- GO', function() {
525 | var uf = 'GO';
526 | it('Shoud mask 10.987.654-7', function(done) {
527 | should(BrM.ie('109876547', uf)).be.eql('10.987.654-7');
528 | done();
529 | });
530 | it('Shoud mask 11.094.402-0', function(done) {
531 | should(BrM.ie('110944020', uf)).be.eql('11.094.402-0');
532 | done();
533 | });
534 | it('Shoud mask 10.115.996-1', function(done) {
535 | should(BrM.ie('101159961', uf)).be.eql('10.115.996-1');
536 | done();
537 | });
538 | it('Shoud mask 10.113.995-0', function(done) {
539 | should(BrM.ie('101139950', uf)).be.eql('10.113.995-0');
540 | done();
541 | });
542 | it('should mask 10.957.654-7', function(done) {
543 | should(BrM.ie('109576547', uf)).be.eql('10.957.654-7');
544 | done();
545 | });
546 | });
547 | describe('- AP', function() {
548 | var uf = 'AP';
549 | it('Shoud mask 030123459', function(done) {
550 | should(BrM.ie('030123459', uf)).be.eql('030123459');
551 | done();
552 | });
553 | it('Shoud mask 030183458', function(done) {
554 | should(BrM.ie('030183458', uf)).be.eql('030183458');
555 | done();
556 | });
557 | it('Shoud mask 030173452', function(done) {
558 | should(BrM.ie('030173452', uf)).be.eql('030173452');
559 | done();
560 | });
561 | it('Shoud mask 030153455', function(done) {
562 | should(BrM.ie('030153455', uf)).be.eql('030153455');
563 | done();
564 | });
565 | it('Shoud mask 030193451', function(done) {
566 | should(BrM.ie('030193451', uf)).be.eql('030193451');
567 | done();
568 | });
569 | it('Shoud mask 030223458', function(done) {
570 | should(BrM.ie('030223458', uf)).be.eql('030223458');
571 | done();
572 | });
573 | it('Shoud mask 037123459', function(done) {
574 | should(BrM.ie('037123459', uf)).be.eql('037123459');
575 | done();
576 | });
577 | it('should mask 031123459', function(done) {
578 | should(BrM.ie('031123459', uf)).be.eql('031123459');
579 | done();
580 | });
581 | });
582 | });
583 | });
584 |
--------------------------------------------------------------------------------