nx-lecom-helper 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # NxLecomHelper
2
+
3
+ Biblioteca UMD simples com funcoes utilitarias para CPF e CNPJ.
4
+
5
+ ## Como usar
6
+
7
+ No browser, as funcoes ficam disponiveis em `window.NxLecomHelper`.
8
+
9
+ ```js
10
+ NxLecomHelper.mascaraCpf("12345678901"); // "123.456.789-01"
11
+ NxLecomHelper.mascaraCnpj("12ABC34501DE35"); // "12.ABC.345/01DE-35"
12
+
13
+ NxLecomHelper.validarCpf("123.456.789-01"); // true/false
14
+ NxLecomHelper.validarCnpj("12.ABC.345/01DE-35"); // true/false
15
+ ```
16
+
17
+ ## Funcoes
18
+
19
+ - `alertTest(message)`: mostra um alerta para checar se a lib carregou.
20
+ - `mascaraCpf(valor)`: aplica mascara no CPF (aceita com ou sem pontuacao).
21
+ - `mascaraCnpj(valor)`: aplica mascara no CNPJ (aceita alfanumerico).
22
+ - `validarCpf(valor)`: valida CPF pelo modulo 11 (pontos e tracos sao opcionais).
23
+ - `validarCnpj(valor)`: valida CNPJ (numerico ou alfanumerico) com DV numerico.
24
+
25
+ ## Regras de validacao
26
+
27
+ - CPF: usa o calculo padrao de dois digitos verificadores (modulo 11).
28
+ - CNPJ alfanumerico: usa 12 caracteres base + 2 DVs numericos.
29
+ - Mapeamento de valor: valor do caractere = ASCII - 48 (0-9 = 0-9, A-Z = 17-42).
30
+ - Pesos: 2 a 9 da direita para a esquerda, repetindo (padrao da Receita/Serpro).
31
+
@@ -1,16 +1,143 @@
1
- (function (global) {
2
- "use strict";
3
-
4
- // Evita sobrescrever se já existir
5
- if (global.LecomAlert) return;
6
-
7
- function show(message) {
8
- alert(message || "LecomAlert funcionando!");
9
- }
10
-
11
- // API global mínima
12
- global.LecomAlert = Object.freeze({
13
- show
14
- });
15
-
16
- })(typeof window !== "undefined" ? window : this);
1
+ (function (global) {
2
+ "use strict";
3
+
4
+ if (global.NxLecomHelper) return;
5
+
6
+ function alertTest(message) {
7
+ alert(message || "NxLecomHelper funcionando!");
8
+ }
9
+
10
+ function somenteDigitos(valor) {
11
+ return String(valor || "").replace(/\D+/g, "");
12
+ }
13
+
14
+ function somenteAlfanumericos(valor) {
15
+ return String(valor || "").replace(/[^0-9a-zA-Z]+/g, "").toUpperCase();
16
+ }
17
+
18
+ function valorCaractereCnpj(caractere) {
19
+ var codigo = caractere.charCodeAt(0);
20
+ if (codigo >= 48 && codigo <= 57) return codigo - 48;
21
+ if (codigo >= 65 && codigo <= 90) return codigo - 48;
22
+ return NaN;
23
+ }
24
+
25
+ function mascaraCpf(valor) {
26
+ var digitos = somenteDigitos(valor).slice(0, 11);
27
+ if (!digitos) return "";
28
+ if (digitos.length <= 3) return digitos;
29
+ if (digitos.length <= 6)
30
+ return digitos.slice(0, 3) + "." + digitos.slice(3);
31
+ if (digitos.length <= 9)
32
+ return (
33
+ digitos.slice(0, 3) +
34
+ "." +
35
+ digitos.slice(3, 6) +
36
+ "." +
37
+ digitos.slice(6)
38
+ );
39
+ return (
40
+ digitos.slice(0, 3) +
41
+ "." +
42
+ digitos.slice(3, 6) +
43
+ "." +
44
+ digitos.slice(6, 9) +
45
+ "-" +
46
+ digitos.slice(9)
47
+ );
48
+ }
49
+
50
+ function mascaraCnpj(valor) {
51
+ var caracteres = somenteAlfanumericos(valor).slice(0, 14);
52
+ if (!caracteres) return "";
53
+ if (caracteres.length <= 2) return caracteres;
54
+ if (caracteres.length <= 5)
55
+ return caracteres.slice(0, 2) + "." + caracteres.slice(2);
56
+ if (caracteres.length <= 8)
57
+ return (
58
+ caracteres.slice(0, 2) +
59
+ "." +
60
+ caracteres.slice(2, 5) +
61
+ "." +
62
+ caracteres.slice(5)
63
+ );
64
+ if (caracteres.length <= 12)
65
+ return (
66
+ caracteres.slice(0, 2) +
67
+ "." +
68
+ caracteres.slice(2, 5) +
69
+ "." +
70
+ caracteres.slice(5, 8) +
71
+ "/" +
72
+ caracteres.slice(8)
73
+ );
74
+ return (
75
+ caracteres.slice(0, 2) +
76
+ "." +
77
+ caracteres.slice(2, 5) +
78
+ "." +
79
+ caracteres.slice(5, 8) +
80
+ "/" +
81
+ caracteres.slice(8, 12) +
82
+ "-" +
83
+ caracteres.slice(12)
84
+ );
85
+ }
86
+
87
+ function validarCpf(valor) {
88
+ var cpf = somenteDigitos(valor);
89
+ if (cpf.length !== 11) return false;
90
+ if (/^(\d)\1{10}$/.test(cpf)) return false;
91
+
92
+ var soma = 0;
93
+ for (var i = 0; i < 9; i++) {
94
+ soma += parseInt(cpf.charAt(i), 10) * (10 - i);
95
+ }
96
+ var resto = (soma * 10) % 11;
97
+ if (resto === 10) resto = 0;
98
+ if (resto !== parseInt(cpf.charAt(9), 10)) return false;
99
+
100
+ soma = 0;
101
+ for (var j = 0; j < 10; j++) {
102
+ soma += parseInt(cpf.charAt(j), 10) * (11 - j);
103
+ }
104
+ resto = (soma * 10) % 11;
105
+ if (resto === 10) resto = 0;
106
+ return resto === parseInt(cpf.charAt(10), 10);
107
+ }
108
+
109
+ function validarCnpj(valor) {
110
+ var cnpj = somenteAlfanumericos(valor);
111
+ if (cnpj.length !== 14) return false;
112
+ if (/^([A-Z0-9])\1{13}$/.test(cnpj)) return false;
113
+ if (!/^\d{2}$/.test(cnpj.slice(12))) return false;
114
+
115
+ var pesos1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
116
+ var pesos2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
117
+ var soma = 0;
118
+ for (var i = 0; i < 12; i++) {
119
+ var valor = valorCaractereCnpj(cnpj.charAt(i));
120
+ if (isNaN(valor)) return false;
121
+ soma += valor * pesos1[i];
122
+ }
123
+ var resultado = soma % 11 < 2 ? 0 : 11 - (soma % 11);
124
+ if (resultado !== parseInt(cnpj.charAt(12), 10)) return false;
125
+
126
+ soma = 0;
127
+ for (var j = 0; j < 13; j++) {
128
+ var valor2 = valorCaractereCnpj(cnpj.charAt(j));
129
+ if (isNaN(valor2)) return false;
130
+ soma += valor2 * pesos2[j];
131
+ }
132
+ resultado = soma % 11 < 2 ? 0 : 11 - (soma % 11);
133
+ return resultado === parseInt(cnpj.charAt(13), 10);
134
+ }
135
+
136
+ global.NxLecomHelper = Object.freeze({
137
+ alertTest,
138
+ mascaraCpf,
139
+ mascaraCnpj,
140
+ validarCpf,
141
+ validarCnpj,
142
+ });
143
+ })(typeof window !== "undefined" ? window : this);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nx-lecom-helper",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Utilitários JS para BPM Lecom (API global)",
5
5
  "main": "dist/nxLecom-helper.umd.js",
6
6
  "files": [
@@ -11,5 +11,8 @@
11
11
  "author": {
12
12
  "name": "Thiago Costa",
13
13
  "email": "thiago.costa@nexum.com.br"
14
+ },
15
+ "dependencies": {
16
+ "nx-lecom-helper": "^1.0.1"
14
17
  }
15
- }
18
+ }