Skip to content

Commit

Permalink
Merge pull request #4 from GainTime/v4
Browse files Browse the repository at this point in the history
V4.1
  • Loading branch information
jprodrigues70 authored Oct 23, 2020
2 parents 836e6f1 + bf3ce8a commit 04399a9
Show file tree
Hide file tree
Showing 45 changed files with 2,564 additions and 304 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
index.html
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
src/
node_modules/
.prettierrc
tsconfig.json
webpack.config.js
.gitignore
index.html
*.map
build/
9 changes: 9 additions & 0 deletions .npmignore-script
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
src/
node_modules/
.prettierrc
tsconfig.json
webpack.config.js
.gitignore
index.html
*.map
dist/
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": false,
"arrowParens": "always",
"singleQuote": true,
"trailingComma": "none"
}
63 changes: 58 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,63 @@
## Como começar

- Importe o nosso JavaScript.
```js
<script type="text/javascript" src="br-validator/dist/gaintime.br-validator.min.js"></script>
### ESM

```
npm install br-validator
```
- Adicione o atributo data-validate aos inputs que deseja validar, tal que o valor do atributo pode ser cpf, cnpj, date, brPhone, cep, text ou num.

```js
<input type="text" data-validate="cpf">
import Br from 'br-validator'

// options é opcional, se vc não passar nada, será:
const options = {
css: true // Utilizar nosso stylesheet,
messages: true // Utilizar nossas mensagens de erro
}

const br = new Br(options)
const BR = br.init()
```

### text/javascript

```
npm install [email protected]
```

Será criada uma variável global chamada `br`

```html
<script type="text/javascript" src="br-validator/dist/index.js"></script>
<script>
br.init() // Configuração padrão
// ou
new br.Br(options).init() // Assim você pode personalizar
</script>
```

## Como utilizar

Adicione o atributo data-validate aos inputs que deseja validar, tal que o valor do atributo pode ser cpf, cnpj, date, brPhone, cep, text ou num.

```html
<input type="text" data-validate="cpf" />
```

Para validar ceps, utilizamos um formato diferente:

```html
<script>
const BR = br.init()
BR.fields.cep.forEach((e) => {
e.addEventListener('blur', function (f) {
console.log(f)
br.cep(f.target, (response) => {
console.log(response)
})
})
})
</script>
```
1 change: 1 addition & 0 deletions build/index.js

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

1 change: 1 addition & 0 deletions build/index.js.map

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions dist/formatters/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var cpf_1 = require("./modules/cpf");
var phone_1 = require("./modules/phone");
var cnpj_1 = require("./modules/cnpj");
var cep_1 = require("./modules/cep");
var date_1 = require("./modules/date");
exports.default = {
cpf: cpf_1.default,
phone: phone_1.default,
cnpj: cnpj_1.default,
cep: cep_1.default,
date: date_1.default
};
18 changes: 18 additions & 0 deletions dist/formatters/modules/cep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function cep(raw) {
var cleaned = ('' + raw).replace(/\D/g, '');
var match = cleaned.match(/^(\d{1,5})?[- ]??[\s]?(\d{1,3})?(.*)?$/);
for (var i = 1; i <= 2; i++) {
if (!match[i]) {
match[i] = '';
}
}
if (!match[1] || cleaned.length < 5) {
return raw;
}
else {
return match[1] + "-" + match[2];
}
}
exports.default = cep;
31 changes: 31 additions & 0 deletions dist/formatters/modules/cnpj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function cnpj(raw) {
var cleaned = ('' + raw).replace(/\D/g, '');
var regex = /^(\d{1,2})?[- ]??[\s]?(\d{1,3})?[\s]?(\d{1,3})?(\d{1,4})?(\d{1,2})?(.*)?$/;
var match = cleaned.match(regex);
for (var i = 1; i <= 5; i++) {
if (!match[i]) {
match[i] = '';
}
}
if (!match[1] || cleaned.length < 3) {
return raw;
}
else if (match[1] && !match[2]) {
return match[1] + ".";
}
else if (match[2] && !match[3]) {
return match[1] + "." + match[2] + (cleaned.length < 5 ? '' : '.');
}
else if (match[3] && !match[4]) {
return match[1] + "." + match[2] + "." + match[3] + (cleaned.length < 8 ? '' : '/');
}
else if (match[4] && !match[5]) {
return match[1] + "." + match[2] + "." + match[3] + "/" + match[4] + (cleaned.length < 12 ? '' : '-');
}
else {
return match[1] + "." + match[2] + "." + match[3] + "/" + match[4] + match[5];
}
}
exports.default = cnpj;
27 changes: 27 additions & 0 deletions dist/formatters/modules/cpf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function cpf(raw) {
var cleaned = ('' + raw).replace(/\D/g, '');
var regex = /^(\d{1,3})?[- ]??[\s]?(\d{1,3})?[\s]?(\d{1,3})?(.*)?$/;
var match = cleaned.match(regex);
for (var i = 1; i <= 4; i++) {
if (!match[i])
match[i] = '';
}
if (!match[1] || cleaned.length < 3) {
return raw;
}
else if (match[1] && !match[2]) {
return match[1] + ".";
}
else if (match[2] && !match[3]) {
return match[1] + "." + match[2] + (cleaned.length < 6 ? '' : '.');
}
else if (match[3] && !match[4]) {
return match[1] + "." + match[2] + "." + match[3] + (cleaned.length < 9 ? '' : '-');
}
else {
return match[1] + "." + match[2] + "." + match[3] + "-" + match[4];
}
}
exports.default = cpf;
24 changes: 24 additions & 0 deletions dist/formatters/modules/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function date(raw) {
var cleaned = ('' + raw).replace(/\D/g, '');
var match = cleaned.match(/^(\d{1,2})?[- ]??[\s]?(\d{1,2})?(.*)?$/);
for (var i = 1; i <= 3; i++) {
if (!match[i]) {
match[i] = '';
}
}
if (!match[1] || cleaned.length < 2) {
return raw;
}
else if (match[1] && !match[2]) {
return match[1] + "/";
}
else if (match[2] && !match[3]) {
return match[1] + "/" + match[2] + (cleaned.length < 4 ? '' : '/');
}
else if (match[3]) {
return match[1] + "/" + match[2] + "/" + match[3];
}
}
exports.default = date;
30 changes: 30 additions & 0 deletions dist/formatters/modules/phone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function phone(raw) {
var cleaned = ('' + raw).replace(/\D/g, '');
var match = [];
if (cleaned.length <= 10) {
match = cleaned.match(/^(\d{1,2})?[- ]?(\d{1,4})?(\d{1,4})?(.*)?$/);
}
else {
match = cleaned.match(/^(\d{1,2})?[- ]?(\d{1,5})?(\d{1,4})?(.*)?$/);
}
for (var i = 1; i <= 3; i++) {
if (!match[i]) {
match[i] = '';
}
}
if (!match[1] || cleaned.length < 2) {
return raw;
}
else if (match[1] && !match[2]) {
return "(" + match[1] + ") ";
}
else if (match[2] && !match[3]) {
return "(" + match[1] + ") " + match[2] + (cleaned.length < 6 ? '' : '-');
}
else if (match[3]) {
return "(" + match[1] + ") " + match[2] + "-" + match[3];
}
}
exports.default = phone;
Loading

0 comments on commit 04399a9

Please sign in to comment.