boletos-ademitech 1.0.0
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/.eslintrc.json +25 -0
- package/LICENSE +661 -0
- package/README.md +50 -0
- package/examples/gerar-boleto-BancoDoBrasil.js +72 -0
- package/examples/gerar-boleto-bradesco.js +58 -0
- package/examples/gerar-boleto-cecred.js +59 -0
- package/index.d.ts +372 -0
- package/lib/boleto/bancos/banco-do-brasil.js +125 -0
- package/lib/boleto/bancos/bradesco.js +115 -0
- package/lib/boleto/bancos/caixa.js +140 -0
- package/lib/boleto/bancos/cecred.js +117 -0
- package/lib/boleto/bancos/itau.js +124 -0
- package/lib/boleto/bancos/logotipos/ailos.png +0 -0
- package/lib/boleto/bancos/logotipos/banco-do-brasil.png +0 -0
- package/lib/boleto/bancos/logotipos/bradesco.png +0 -0
- package/lib/boleto/bancos/logotipos/caixa-economica-federal.png +0 -0
- package/lib/boleto/bancos/logotipos/itau.png +0 -0
- package/lib/boleto/bancos/logotipos/santander.png +0 -0
- package/lib/boleto/bancos/logotipos/sicoob.png +0 -0
- package/lib/boleto/bancos/logotipos/sicredi.png +0 -0
- package/lib/boleto/bancos/santander.js +118 -0
- package/lib/boleto/bancos/sicoob.js +118 -0
- package/lib/boleto/bancos/sicredi.js +143 -0
- package/lib/boleto/codigo-de-barras-builder.js +38 -0
- package/lib/boleto/fontes/Code25I.ttf +0 -0
- package/lib/boleto/fontes/Times New Roman Bold Italic.ttf +0 -0
- package/lib/boleto/fontes/Times New Roman Bold.ttf +0 -0
- package/lib/boleto/fontes/Times New Roman Italic.ttf +0 -0
- package/lib/boleto/fontes/Times New Roman.ttf +0 -0
- package/lib/boleto/gerador-de-boleto.js +1198 -0
- package/lib/boleto/gerador-de-digito-padrao.js +42 -0
- package/lib/boleto/gerador-de-linha-digitavel.js +26 -0
- package/lib/boleto/imagens/tesoura128.png +0 -0
- package/lib/boleto/valida-codigo-barras.js +12 -0
- package/lib/index.js +14 -0
- package/lib/metodosPublicos/boletoMetodos.js +50 -0
- package/lib/pdf-gerador.js +26 -0
- package/lib/stringify/boletoStringify.js +49 -0
- package/lib/utils/array-utils.js +13 -0
- package/lib/utils/date-utils.js +8 -0
- package/lib/utils/functions/bancosUtils.js +104 -0
- package/lib/utils/functions/boletoUtils.js +895 -0
- package/lib/utils/functions/formatacoesUtils.js +287 -0
- package/lib/utils/functions/validacoesUtils.js +233 -0
- package/lib/utils/math-utils.js +53 -0
- package/lib/utils/object-utils.js +8 -0
- package/lib/utils/stream-utils.js +6 -0
- package/lib/utils/string-utils.js +60 -0
- package/package.json +60 -0
- package/tests/bancos-test.js +116 -0
- package/tests/boleto/bancos/bradesco-test.js +145 -0
- package/tests/boleto/bancos/caixa-test.js +428 -0
- package/tests/boleto/bancos/itau-test.js +342 -0
- package/tests/boleto/bancos/sicoob-test.js +341 -0
- package/tests/boleto/gerador-de-digito-padrao-test.js +22 -0
- package/tests/boleto-test.js +349 -0
- package/tests/brasil-test.js +66 -0
- package/tests/formatacoes-test.js +349 -0
- package/tests/validacoes-test.js +309 -0
- package/tmp/boletos/.gitkeep +0 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
const StringUtils = require('../string-utils');
|
|
2
|
+
const ObjectUtils = require('../object-utils');
|
|
3
|
+
const DateUtils = require('../date-utils');
|
|
4
|
+
const pad = StringUtils.pad;
|
|
5
|
+
const insert = StringUtils.insert;
|
|
6
|
+
const merge = ObjectUtils.merge;
|
|
7
|
+
const isValidDate = DateUtils.isValidDate;
|
|
8
|
+
const validacoes = require('../functions/validacoesUtils');
|
|
9
|
+
|
|
10
|
+
function dinheiro(_numero, args) {
|
|
11
|
+
if (isNaN(_numero)) {
|
|
12
|
+
return _numero;
|
|
13
|
+
}
|
|
14
|
+
args = merge({
|
|
15
|
+
separadorDecimal: ',',
|
|
16
|
+
separadorDeMilhar: '.',
|
|
17
|
+
casasDecimais: 2,
|
|
18
|
+
simbolo: 'R$ ',
|
|
19
|
+
posicionamento: 'esquerda'
|
|
20
|
+
}, args);
|
|
21
|
+
|
|
22
|
+
if (args.posicionamento === 'esquerda') {
|
|
23
|
+
return args.simbolo + numero(_numero, args);
|
|
24
|
+
} else {
|
|
25
|
+
return numero(_numero, args) + args.simbolo;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports.dinheiro = dinheiro;
|
|
30
|
+
|
|
31
|
+
function dinheiroPorExtenso(numero) {
|
|
32
|
+
/*
|
|
33
|
+
O código desta função foi obtido em http://jsfromhell.com/pt/string/extenso
|
|
34
|
+
---------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
+ Carlos R. L. Rodrigues
|
|
37
|
+
@ http://jsfromhell.com/string/extenso [rev. #3]
|
|
38
|
+
|
|
39
|
+
"Copyright: Autorizamos a cópia e modificação de todos os códigos contidos no site, desde que se mantenha os créditos do autor original."
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
var ex = [
|
|
43
|
+
['zero', 'um', 'dois', 'três', 'quatro', 'cinco', 'seis', 'sete', 'oito', 'nove', 'dez', 'onze', 'doze', 'treze', 'quatorze', 'quinze', 'dezesseis', 'dezessete', 'dezoito', 'dezenove'],
|
|
44
|
+
['dez', 'vinte', 'trinta', 'quarenta', 'cinquenta', 'sessenta', 'setenta', 'oitenta', 'noventa'],
|
|
45
|
+
['cem', 'cento', 'duzentos', 'trezentos', 'quatrocentos', 'quinhentos', 'seiscentos', 'setecentos', 'oitocentos', 'novecentos'],
|
|
46
|
+
['mil', 'milhão', 'bilhão', 'trilhão', 'quadrilhão', 'quintilhão', 'sextilhão', 'setilhão', 'octilhão', 'nonilhão', 'decilhão', 'undecilhão', 'dodecilhão', 'tredecilhão', 'quatrodecilhão', 'quindecilhão', 'sedecilhão', 'septendecilhão', 'octencilhão', 'nonencilhão']
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
numero = numero.toString();
|
|
50
|
+
numero = numero.replace('.', ',');
|
|
51
|
+
var c = true;
|
|
52
|
+
|
|
53
|
+
var a, n, v, i, n = numero.replace(c ? /[^,\d]/g : /\D/g, '').split(','), e = ' e ', $ = 'real', d = 'centavo', sl;
|
|
54
|
+
|
|
55
|
+
for (var f = n.length - 1, l, j = -1, r = [], s = [], t = ''; ++j <= f; s = []) {
|
|
56
|
+
j && (n[j] = (('.' + n[j]) * 1).toFixed(2).slice(2));
|
|
57
|
+
if (!(a = (v = n[j]).slice((l = v.length) % 3).match(/\d{3}/g), v = l % 3 ? [v.slice(0, l % 3)] : [], v = a ? v.concat(a) : v).length) continue;
|
|
58
|
+
for (a = -1, l = v.length; ++a < l; t = '') {
|
|
59
|
+
if (!(i = v[a] * 1)) continue;
|
|
60
|
+
i % 100 < 20 && (t += ex[0][i % 100]) ||
|
|
61
|
+
i % 100 + 1 && (t += ex[1][(i % 100 / 10 >> 0) - 1] + (i % 10 ? e + ex[0][i % 10] : ''));
|
|
62
|
+
s.push((i < 100 ? t : !(i % 100) ? ex[2][i == 100 ? 0 : i / 100 >> 0] : (ex[2][i / 100 >> 0] + e + t)) +
|
|
63
|
+
((t = l - a - 2) > -1 ? ' ' + (i > 1 && t > 0 ? ex[3][t].replace('?o', '?es') : ex[3][t]) : ''));
|
|
64
|
+
}
|
|
65
|
+
a = ((sl = s.length) > 1 ? (a = s.pop(), s.join(' ') + e + a) : s.join('') || ((!j && (n[j + 1] * 1 > 0) || r.length) ? '' : ex[0][0]));
|
|
66
|
+
a && r.push(a + (c ? (' ' + (v.join('') * 1 > 1 ? j ? d + 's' : (/0{6,}$/.test(n[0]) ? 'de ' : '') + $.replace('l', 'is') : j ? d : $)) : ''));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
var resultado = r.join(e);
|
|
70
|
+
|
|
71
|
+
if (resultado === 'zero real') {
|
|
72
|
+
return 'zero reais';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return resultado;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
module.exports.dinheiroPorExtenso = dinheiroPorExtenso;
|
|
79
|
+
|
|
80
|
+
function numero(numero, args) {
|
|
81
|
+
if (isNaN(numero)) {
|
|
82
|
+
return numero;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
var casasDecimaisOriginais = numero.toString().split('.');
|
|
86
|
+
if (casasDecimaisOriginais.length > 1) {
|
|
87
|
+
casasDecimaisOriginais = casasDecimaisOriginais[1].length;
|
|
88
|
+
} else {
|
|
89
|
+
casasDecimaisOriginais = 0;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
args = merge({
|
|
93
|
+
separadorDecimal: ',',
|
|
94
|
+
separadorDeMilhar: '.',
|
|
95
|
+
casasDecimais: casasDecimaisOriginais
|
|
96
|
+
}, args);
|
|
97
|
+
|
|
98
|
+
function _format(n, x, s, c) {
|
|
99
|
+
//http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript
|
|
100
|
+
var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
|
|
101
|
+
num = numero.toString().replace(new RegExp('\\.([\\d]{' + args.casasDecimais + '})[\\d]*?$'), '.$1');
|
|
102
|
+
|
|
103
|
+
num = parseFloat(num).toFixed(args.casasDecimais);
|
|
104
|
+
return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return _format(args.casasDecimais, 3, args.separadorDeMilhar, args.separadorDecimal);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
module.exports.numero = numero;
|
|
111
|
+
|
|
112
|
+
function removerMascara(texto) {
|
|
113
|
+
if (typeof texto !== 'string') {
|
|
114
|
+
return texto;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return texto
|
|
118
|
+
.trim()
|
|
119
|
+
.replace(/\(/g, '')
|
|
120
|
+
.replace(/\)/g, '')
|
|
121
|
+
.replace(/\./g, '')
|
|
122
|
+
.replace(/\//g, '')
|
|
123
|
+
.replace(/-/g, '')
|
|
124
|
+
.replace(/\s/g, '')
|
|
125
|
+
.replace(/R\$/g, '')
|
|
126
|
+
.replace(/%/g, '')
|
|
127
|
+
.trim();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
module.exports.removerMascara = removerMascara;
|
|
131
|
+
|
|
132
|
+
function linhaDigitavel(valor) {
|
|
133
|
+
valor = removerMascara(valor);
|
|
134
|
+
|
|
135
|
+
if (valor.length !== 47) {
|
|
136
|
+
return valor;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
valor = insert(valor, 5, '.');
|
|
140
|
+
valor = insert(valor, 11, ' ');
|
|
141
|
+
valor = insert(valor, 17, '.');
|
|
142
|
+
valor = insert(valor, 24, ' ');
|
|
143
|
+
valor = insert(valor, 30, '.');
|
|
144
|
+
valor = insert(valor, 37, ' ');
|
|
145
|
+
valor = insert(valor, 39, ' ');
|
|
146
|
+
|
|
147
|
+
return valor;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
module.exports.linhaDigitavel = linhaDigitavel;
|
|
151
|
+
module.exports.boletoBancario = linhaDigitavel;
|
|
152
|
+
|
|
153
|
+
function hora(data, args) {
|
|
154
|
+
if (!isValidDate(data)) {
|
|
155
|
+
return data;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
args = merge({
|
|
159
|
+
comSegundos: true
|
|
160
|
+
}, args);
|
|
161
|
+
|
|
162
|
+
var componentes = [
|
|
163
|
+
pad(data.getHours(), 2, '0'),
|
|
164
|
+
pad(data.getMinutes(), 2, '0'),
|
|
165
|
+
];
|
|
166
|
+
|
|
167
|
+
if (args.comSegundos) {
|
|
168
|
+
componentes.push(pad(data.getSeconds(), 2, '0'));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return componentes.join(':');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
module.exports.hora = hora;
|
|
175
|
+
|
|
176
|
+
function data(data) {
|
|
177
|
+
if (!isValidDate(data)) {
|
|
178
|
+
return data;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return [
|
|
182
|
+
pad(data.getDate(), 2, '0'),
|
|
183
|
+
pad(data.getMonth() + 1, 2, '0'),
|
|
184
|
+
data.getFullYear()
|
|
185
|
+
].join('/');
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
module.exports.data = data;
|
|
189
|
+
|
|
190
|
+
function dataEHora(_data, args) {
|
|
191
|
+
if (!isValidDate(_data)) {
|
|
192
|
+
return _data;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return [
|
|
196
|
+
data(_data),
|
|
197
|
+
hora(_data, args)
|
|
198
|
+
].join(' ');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
module.exports.dataEHora = dataEHora;
|
|
202
|
+
|
|
203
|
+
function tituloDeEleitor(texto) {
|
|
204
|
+
if (!validacoes.eTituloDeEleitor(texto)) {
|
|
205
|
+
return texto;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return removerMascara(texto).match(/.{4}/g).join(' ');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
module.exports.tituloDeEleitor = tituloDeEleitor;
|
|
212
|
+
|
|
213
|
+
function cnpj(texto) {
|
|
214
|
+
if (!validacoes.eCnpj(texto)) return texto;
|
|
215
|
+
if (texto.trim().length > 14) return texto;
|
|
216
|
+
|
|
217
|
+
texto = texto.trim();
|
|
218
|
+
|
|
219
|
+
return texto.substr(0, 2) + '.' +
|
|
220
|
+
texto.substr(2, 3) + '.' +
|
|
221
|
+
texto.substr(5, 3) + '/' +
|
|
222
|
+
texto.substr(8, 4) + '-' +
|
|
223
|
+
texto.substr(12, 2);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
module.exports.cnpj = cnpj;
|
|
227
|
+
|
|
228
|
+
function cpf(texto) {
|
|
229
|
+
if (!validacoes.eCpf(texto)) return texto;
|
|
230
|
+
if (texto.trim().length > 11) return texto;
|
|
231
|
+
|
|
232
|
+
texto = texto.trim();
|
|
233
|
+
|
|
234
|
+
return texto.substr(0, 3) + '.' +
|
|
235
|
+
texto.substr(3, 3) + '.' +
|
|
236
|
+
texto.substr(6, 3) + '-' +
|
|
237
|
+
texto.substr(9, 2);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
module.exports.cpf = cpf;
|
|
241
|
+
|
|
242
|
+
function pisPasep(texto) {
|
|
243
|
+
if (!validacoes.ePisPasep(texto)) return texto;
|
|
244
|
+
if (texto.trim().length > 11) return texto;
|
|
245
|
+
|
|
246
|
+
texto = texto.trim();
|
|
247
|
+
|
|
248
|
+
return texto.substr(0, 3) + '.' +
|
|
249
|
+
texto.substr(3, 4) + '.' +
|
|
250
|
+
texto.substr(7, 3) + '-' +
|
|
251
|
+
texto.substr(10, 1);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
module.exports.nit = pisPasep;
|
|
255
|
+
module.exports.pisPasep = pisPasep;
|
|
256
|
+
|
|
257
|
+
module.exports.registroNacional = registroNacional;
|
|
258
|
+
|
|
259
|
+
function registroNacional(texto) {
|
|
260
|
+
var tipo = validacoes.eRegistroNacional(texto);
|
|
261
|
+
|
|
262
|
+
if (!tipo) {
|
|
263
|
+
return texto;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return this[tipo](texto);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
module.exports.placa = placa;
|
|
270
|
+
|
|
271
|
+
function placa(texto) {
|
|
272
|
+
if (!validacoes.ePlaca(texto)) return texto;
|
|
273
|
+
|
|
274
|
+
texto = texto.trim().replace(/-/g, '');
|
|
275
|
+
|
|
276
|
+
return texto.substr(0, 3).toUpperCase() + '-' + texto.substr(3, 4);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
module.exports.cep = cep;
|
|
280
|
+
|
|
281
|
+
function cep(texto) {
|
|
282
|
+
if (!validacoes.eCep(texto)) return texto;
|
|
283
|
+
|
|
284
|
+
texto = texto.trim().replace(/-/g, '').replace(/\./g, '');
|
|
285
|
+
|
|
286
|
+
return texto.substr(0, 2) + '.' + texto.substr(2, 3) + '-' + texto.substr(5, 3);
|
|
287
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
const formatacoes = require('../functions/formatacoesUtils');
|
|
2
|
+
const StringUtils = require('../string-utils');
|
|
3
|
+
const MathUtils = require('../math-utils');
|
|
4
|
+
const mod = MathUtils.mod;
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
function eTituloDeEleitor(tituloDeEleitor) {
|
|
8
|
+
if (typeof tituloDeEleitor !== 'string') {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
tituloDeEleitor = formatacoes.removerMascara(tituloDeEleitor);
|
|
13
|
+
|
|
14
|
+
if (tituloDeEleitor.length !== 12) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let base = tituloDeEleitor.substring(0, 10);
|
|
19
|
+
let primeiraBase = tituloDeEleitor.substring(0, 8);
|
|
20
|
+
let estado = tituloDeEleitor.substring(8, 10);
|
|
21
|
+
let segundaBase = tituloDeEleitor.substring(8, 10);
|
|
22
|
+
|
|
23
|
+
estado = parseInt(estado, 10);
|
|
24
|
+
|
|
25
|
+
if (estado < 1 || estado > 28) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var primeiroResto = mod(primeiraBase),
|
|
30
|
+
primeiroDigito;
|
|
31
|
+
|
|
32
|
+
if (primeiroResto === 0 && estado < 3) {
|
|
33
|
+
primeiroDigito = 0;
|
|
34
|
+
} else {
|
|
35
|
+
primeiroDigito = primeiroResto < 2 ? 0 : 11 - primeiroResto;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var segundoResto = mod(segundaBase + primeiroDigito, [2, 3, 4]),
|
|
39
|
+
segundoDigito;
|
|
40
|
+
|
|
41
|
+
if (segundoResto === 0 && estado < 3) {
|
|
42
|
+
segundoDigito = 0;
|
|
43
|
+
} else {
|
|
44
|
+
segundoDigito = segundoResto < 2 ? 0 : 11 - segundoResto;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (tituloDeEleitor === base + primeiroDigito + segundoDigito) {
|
|
48
|
+
return {
|
|
49
|
+
1: 'SP', 2: 'MG', 3: 'RJ', 4: 'RS',
|
|
50
|
+
5: 'BA', 6: 'PR', 7: 'CE', 8: 'PE',
|
|
51
|
+
9: 'SC', 10: 'GO', 11: 'MA', 12: 'PB',
|
|
52
|
+
13: 'PA', 14: 'ES', 15: 'PI', 16: 'RN',
|
|
53
|
+
17: 'AL', 18: 'MT', 19: 'MS', 20: 'DF',
|
|
54
|
+
21: 'SE', 22: 'AM', 23: 'RS', 24: 'AC',
|
|
55
|
+
25: 'AP', 26: 'RR', 27: 'TO', 28: 'ZZ',
|
|
56
|
+
}[estado];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
module.exports.eTituloDeEleitor = eTituloDeEleitor;
|
|
63
|
+
|
|
64
|
+
function eEan(ean) {
|
|
65
|
+
if (typeof ean !== 'string' || !/^(?:\d{8}|\d{12,14})$/.test(ean)) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
ean = ean.split('').map(Number);
|
|
70
|
+
|
|
71
|
+
var peso = 1,
|
|
72
|
+
digitoVerificador = ean.pop(),
|
|
73
|
+
soma = ean.reduceRight(function (anterior, atual) {
|
|
74
|
+
peso = 4 - peso;
|
|
75
|
+
return anterior + (atual * peso);
|
|
76
|
+
}, 0),
|
|
77
|
+
digitoVerificadorCalculado = (soma + ((10 - (soma % 10)) % 10)) - soma;
|
|
78
|
+
|
|
79
|
+
return digitoVerificador === digitoVerificadorCalculado;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
module.exports.eEan = eEan;
|
|
83
|
+
|
|
84
|
+
function eRegistroNacional(rn, tipo) {
|
|
85
|
+
if (typeof rn !== 'string') {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
rn = formatacoes.removerMascara(rn);
|
|
90
|
+
|
|
91
|
+
if (typeof tipo === 'undefined') {
|
|
92
|
+
if (rn.length === 14 && eCnpj(rn)) {
|
|
93
|
+
return 'cnpj';
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (rn.length === 11 && eCpf(rn)) {
|
|
97
|
+
return 'cpf';
|
|
98
|
+
}
|
|
99
|
+
} else if (['cpf', 'cnpj'].indexOf(tipo.toLowerCase()) > -1) {
|
|
100
|
+
var fn = module.exports['e' + StringUtils.capitalize(tipo)];
|
|
101
|
+
|
|
102
|
+
if (fn(rn)) {
|
|
103
|
+
return tipo;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
module.exports.eRegistroNacional = eRegistroNacional;
|
|
111
|
+
|
|
112
|
+
var casosTriviaisDeCnpjFalsos = [
|
|
113
|
+
'000000000000',
|
|
114
|
+
'111111111111',
|
|
115
|
+
'222222222222',
|
|
116
|
+
'333333333333',
|
|
117
|
+
'444444444444',
|
|
118
|
+
'555555555555',
|
|
119
|
+
'666666666666',
|
|
120
|
+
'777777777777',
|
|
121
|
+
'888888888888',
|
|
122
|
+
'999999999999'
|
|
123
|
+
];
|
|
124
|
+
|
|
125
|
+
function eCnpj(cnpj) {
|
|
126
|
+
if (typeof cnpj !== 'string') return false;
|
|
127
|
+
cnpj = formatacoes.removerMascara(cnpj);
|
|
128
|
+
if (cnpj.length !== 14) return false;
|
|
129
|
+
|
|
130
|
+
var base = cnpj.substring(0, 12);
|
|
131
|
+
|
|
132
|
+
if (casosTriviaisDeCnpjFalsos.indexOf(base) > -1) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
var primeiroResto = mod(base);
|
|
137
|
+
var primeiroDigito = primeiroResto < 2 ? 0 : 11 - primeiroResto;
|
|
138
|
+
|
|
139
|
+
var segundoResto = mod(base + primeiroDigito);
|
|
140
|
+
var segundoDigito = segundoResto < 2 ? 0 : 11 - segundoResto;
|
|
141
|
+
|
|
142
|
+
return cnpj === base + primeiroDigito + segundoDigito;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
module.exports.eCnpj = eCnpj;
|
|
146
|
+
|
|
147
|
+
var regexParaMatrizEFilial = /^[0-9]{8}([0-9]{4})[0-9]{2}$/;
|
|
148
|
+
|
|
149
|
+
function eMatriz(cnpj) {
|
|
150
|
+
//retorna null caso não seja passado um cnpj
|
|
151
|
+
//retorna true caso seja uma matriz
|
|
152
|
+
//retorna false caso não seja uma matriz
|
|
153
|
+
|
|
154
|
+
if (!eCnpj(cnpj)) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
var matches = regexParaMatrizEFilial.exec(formatacoes.removerMascara(cnpj));
|
|
159
|
+
|
|
160
|
+
return (matches !== null && matches.length === 2 && matches[1] === '0001');
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
module.exports.eMatriz = eMatriz;
|
|
164
|
+
|
|
165
|
+
function eFilial(cnpj) {
|
|
166
|
+
//retorna null caso não seja passado um cnpj
|
|
167
|
+
//retorna o número da filial caso seja passado um cnpj válido
|
|
168
|
+
//retorna false caso não seja uma filial
|
|
169
|
+
|
|
170
|
+
if (!eCnpj(cnpj)) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
var matches = regexParaMatrizEFilial.exec(formatacoes.removerMascara(cnpj));
|
|
175
|
+
|
|
176
|
+
if (matches !== null && matches.length === 2 && matches[1] !== '0001') {
|
|
177
|
+
return parseInt(matches[1], 10);
|
|
178
|
+
} else {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
module.exports.eFilial = eFilial;
|
|
184
|
+
|
|
185
|
+
function eCpf(cpf) {
|
|
186
|
+
if (typeof cpf !== 'string') return false;
|
|
187
|
+
cpf = formatacoes.removerMascara(cpf);
|
|
188
|
+
if (cpf.length !== 11) return false;
|
|
189
|
+
|
|
190
|
+
var base = cpf.substring(0, 9);
|
|
191
|
+
var multiplicadores = [2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
192
|
+
|
|
193
|
+
var primeiroResto = mod(base, multiplicadores);
|
|
194
|
+
var primeiroDigito = primeiroResto < 2 ? 0 : 11 - primeiroResto;
|
|
195
|
+
|
|
196
|
+
multiplicadores.push(11);
|
|
197
|
+
|
|
198
|
+
var segundoResto = mod(base + primeiroDigito, multiplicadores);
|
|
199
|
+
var segundoDigito = segundoResto < 2 ? 0 : 11 - segundoResto;
|
|
200
|
+
|
|
201
|
+
return cpf === base + primeiroDigito + segundoDigito;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
module.exports.eCpf = eCpf;
|
|
205
|
+
|
|
206
|
+
function ePisPasep(pisPasep) {
|
|
207
|
+
if (typeof pisPasep !== 'string') return false;
|
|
208
|
+
pisPasep = formatacoes.removerMascara(pisPasep);
|
|
209
|
+
if (pisPasep.length !== 11) return false;
|
|
210
|
+
|
|
211
|
+
var base = pisPasep.substring(0, 10);
|
|
212
|
+
var multiplicadores = [3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
213
|
+
|
|
214
|
+
var resto = mod(base, multiplicadores, 11, 'leftToRight');
|
|
215
|
+
var subtracao = 11 - resto;
|
|
216
|
+
var digito = subtracao == 10 || subtracao == 11 ? 0 : subtracao;
|
|
217
|
+
|
|
218
|
+
return pisPasep === base + digito;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
module.exports.ePisPasep = ePisPasep;
|
|
222
|
+
module.exports.eNit = ePisPasep;
|
|
223
|
+
|
|
224
|
+
var regexDePlacaValida = /^[a-zA-Z]{3}-?[0-9]{4}$/;
|
|
225
|
+
module.exports.ePlaca = function (placa) {
|
|
226
|
+
return regexDePlacaValida.test(placa);
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
var regexDeCepValido = /^\d{2}\.?\d{3}-?\d{3}$/;
|
|
231
|
+
module.exports.eCep = function (cep) {
|
|
232
|
+
return regexDeCepValido.test(cep);
|
|
233
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const arrayUtils = require('./array-utils');
|
|
2
|
+
|
|
3
|
+
module.exports.mod = function (value, factors, divider, direction) {
|
|
4
|
+
|
|
5
|
+
var reduceSummationTerms = false,
|
|
6
|
+
cumplimentaryToDivider = false;
|
|
7
|
+
|
|
8
|
+
if (arguments.length === 1 && typeof value === 'object') {
|
|
9
|
+
factors = value.factors;
|
|
10
|
+
divider = value.divider;
|
|
11
|
+
direction = value.direction;
|
|
12
|
+
reduceSummationTerms = value.reduceSummationTerms;
|
|
13
|
+
cumplimentaryToDivider = value.cumplimentaryToDivider;
|
|
14
|
+
value = value.value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (divider === undefined) {
|
|
18
|
+
divider = 11;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (factors === undefined) {
|
|
22
|
+
factors = arrayUtils.series(2, 9);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (direction === undefined) {
|
|
26
|
+
direction = 'rightToLeft';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var reduceMethod = direction === 'leftToRight' ? 'reduce' : 'reduceRight';
|
|
30
|
+
|
|
31
|
+
var i = 0;
|
|
32
|
+
var result = value.split('')[reduceMethod](function (last, current) {
|
|
33
|
+
if (i > factors.length - 1) {
|
|
34
|
+
i = 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
var total = factors[i++] * parseInt(current, 10);
|
|
38
|
+
|
|
39
|
+
const sum = (a, b) => a + b;
|
|
40
|
+
|
|
41
|
+
if (reduceSummationTerms) {
|
|
42
|
+
total = total.toString().split('').map(Number).reduce(sum, 0);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return total + last;
|
|
46
|
+
}, 0) % divider;
|
|
47
|
+
|
|
48
|
+
if (cumplimentaryToDivider) {
|
|
49
|
+
result = divider - result;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return result;
|
|
53
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
function pad(str, length, padStr, type) {
|
|
2
|
+
/* jshint ignore:start, unused:false */
|
|
3
|
+
str = str == null ? '' : String(str);
|
|
4
|
+
length = ~~length;
|
|
5
|
+
|
|
6
|
+
var padlen = 0;
|
|
7
|
+
|
|
8
|
+
if (!padStr)
|
|
9
|
+
padStr = ' ';
|
|
10
|
+
else if (padStr.length > 1)
|
|
11
|
+
padStr = padStr.charAt(0);
|
|
12
|
+
|
|
13
|
+
switch (type) {
|
|
14
|
+
case 'right':
|
|
15
|
+
padlen = length - str.length;
|
|
16
|
+
return str + strRepeat(padStr, padlen);
|
|
17
|
+
case 'both':
|
|
18
|
+
padlen = length - str.length;
|
|
19
|
+
return strRepeat(padStr, Math.ceil(padlen / 2)) + str +
|
|
20
|
+
strRepeat(padStr, Math.floor(padlen / 2));
|
|
21
|
+
default: // 'left'
|
|
22
|
+
padlen = length - str.length;
|
|
23
|
+
return strRepeat(padStr, padlen) + str;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function strRepeat(str, qty) {
|
|
27
|
+
if (qty < 1) return '';
|
|
28
|
+
var result = '';
|
|
29
|
+
while (qty > 0) {
|
|
30
|
+
if (qty & 1) result += str;
|
|
31
|
+
qty >>= 1, str += str;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/* jshint ignore:end */
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports.pad = pad;
|
|
41
|
+
|
|
42
|
+
function insert(string, index, value) {
|
|
43
|
+
return [
|
|
44
|
+
string.substring(0, index),
|
|
45
|
+
value,
|
|
46
|
+
string.substring(index, string.length)
|
|
47
|
+
].join('');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports.insert = insert;
|
|
51
|
+
|
|
52
|
+
function capitalize(value) {
|
|
53
|
+
if (typeof value !== 'string') {
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return value.substr(0, 1).toUpperCase() + value.substr(1);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
module.exports.capitalize = capitalize;
|