jcinfo-utils 1.0.44 → 1.0.45

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jcinfo-utils",
3
- "version": "1.0.44",
3
+ "version": "1.0.45",
4
4
  "description": "Pacote de funções utilitárias",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -0,0 +1,76 @@
1
+ export function validarCNPJ(aValue) {
2
+ let acumulador = 0
3
+ let contador = 5
4
+ let finalChar = 12
5
+ let strDigito = ''
6
+
7
+ // Função para remover caracteres não numéricos
8
+ function soNumeros(value) {
9
+ return value.replace(/\D/g, '')
10
+ }
11
+
12
+ aValue = soNumeros(aValue)
13
+
14
+ if (aValue === '') {
15
+ return false
16
+ }
17
+
18
+ if (aValue.length !== 14) {
19
+ return false
20
+ }
21
+
22
+ for (let i = 0; i < 2; i++) {
23
+ for (let j = 0; j < finalChar; j++) {
24
+ acumulador += parseInt(aValue[j]) * contador
25
+ contador = contador === 2 ? 9 : contador - 1
26
+ }
27
+
28
+ let digito = Math.abs(11 - (acumulador % 11))
29
+ digito = digito > 9 ? 0 : digito
30
+ strDigito += digito.toString()
31
+ acumulador = 0
32
+ contador = 6
33
+ finalChar = 13
34
+ }
35
+
36
+ return aValue.substr(12, 2) === strDigito
37
+ }
38
+
39
+ export function validarCPF(aValue) {
40
+ let acumulador = 0
41
+ let contador = 10
42
+ let finalChar = 9
43
+ let numerosCpf = ''
44
+ let strDigito = ''
45
+
46
+ // Função para remover caracteres não numéricos
47
+ function soNumeros(value) {
48
+ return value.replace(/\D/g, '')
49
+ }
50
+
51
+ numerosCpf = soNumeros(aValue)
52
+
53
+ if (numerosCpf.trim() === '') {
54
+ return false
55
+ }
56
+
57
+ if (numerosCpf.length !== 11) {
58
+ return false
59
+ }
60
+
61
+ for (let i = 0; i < 2; i++) {
62
+ for (let j = 0; j < finalChar; j++) {
63
+ acumulador += parseInt(numerosCpf[j]) * contador
64
+ contador--
65
+ }
66
+
67
+ let digito = Math.abs(11 - (acumulador % 11))
68
+ digito = digito > 9 ? 0 : digito
69
+ strDigito += digito.toString()
70
+ acumulador = 0
71
+ contador = 11
72
+ finalChar = 10
73
+ }
74
+
75
+ return numerosCpf.substr(9, 2) === strDigito
76
+ }
package/test/testar.js CHANGED
@@ -2,117 +2,123 @@ import * as jc from '../index.js'
2
2
  import fs from 'node:fs'
3
3
  import * as update from '../src/updateDotEnv.js'
4
4
  import { retirarAcentuacao } from '../src/retirarAcentuacao.js'
5
+ import { validarCNPJ, validarCPF } from '../src/validator.js'
5
6
 
6
- jc.jcinfoUtils.showLogs = true
7
+ {
8
+ jc.jcinfoUtils.showLogs = true
7
9
 
8
- function testFormatFileSize() {
9
- let content = fs.readFileSync('c:\\jcinfo\\fb3\\databases.conf')
10
- // let content = fs.readFileSync('testar.js', 'utf8')
11
- console.log('formatFileSize() :', jc.formatFileSize(content.length, 3))
12
- }
13
-
14
- console.clear()
15
- console.group('--- Testes ---')
16
- console.log(
17
- 'formatValor(123, 3, false) => ',
18
- jc.formatValor(123, 3, false),
19
- )
20
- console.log(
21
- 'formatValor(654654.65654, 2, false) => ',
22
- jc.formatValor(jc.trunc(654654.65654, 2), 2, false),
23
- ' (usando trunc())',
24
- )
25
-
26
- console.log()
27
- console.log("getHash('test') :", jc.getHash('test'))
28
-
29
- testFormatFileSize()
30
-
31
- console.log()
32
- console.group('--- trunc ---')
33
- console.log(
34
- 'Números somados em um único trunc() 248.08 =',
35
- jc.trunc(123.99889889 + 123.99889889 + 0.09),
36
- )
37
-
38
- console.log(
39
- 'Soma sem quebra (com 2 casas decimais) e SEM trunc() 248.07 =',
40
- 123.99 + 123.99 + 0.09,
41
- )
42
-
43
- console.log(
44
- 'Soma com cada numero quebrado dentro de um trunc() 248.07 =',
45
- jc.trunc(123.99889889) + jc.trunc(123.99889889) + jc.trunc(0.0999999),
46
- )
47
-
48
- console.groupEnd()
49
-
50
- // ==================================================
51
- console.group('Datas')
52
-
53
- const datas = [
54
- '01/01/2023 23:00:00',
55
- '01/01/2023 15:00:00',
56
- '2023-01-01T23:00:00',
57
- '2023-01-01T15:00:00',
58
- '',
59
- new Date('2023-01-01T23:00:00'),
60
- new Date('2023-01-01T00:00:00'),
61
- '',
62
- '2023-01-01',
63
- '2023-01-01T00:00:00',
64
- '',
65
- new Date('2023-01-01T23:00:00'),
66
- new Date('2023-01-01T00:00:00'),
67
- jc.getDateTimeStr(),
68
- '2023-10-08T23:40',
69
- ]
70
-
71
- datas.forEach((data, i) => {
72
- let value = data
73
- if (typeof value != 'string') {
74
- value = data.toString()
10
+ function testFormatFileSize() {
11
+ let content = fs.readFileSync('c:\\jcinfo\\fb3\\databases.conf')
12
+ // let content = fs.readFileSync('testar.js', 'utf8')
13
+ console.log('formatFileSize() :', jc.formatFileSize(content.length, 3))
75
14
  }
15
+
16
+ console.clear()
17
+ console.group('--- Testes ---')
18
+ console.log(
19
+ 'formatValor(123, 3, false) => ',
20
+ jc.formatValor(123, 3, false),
21
+ )
22
+ console.log(
23
+ 'formatValor(654654.65654, 2, false) => ',
24
+ jc.formatValor(jc.trunc(654654.65654, 2), 2, false),
25
+ ' (usando trunc())',
26
+ )
27
+
28
+ console.log()
29
+ console.log("getHash('test') :", jc.getHash('test'))
30
+
31
+ testFormatFileSize()
32
+
33
+ console.log()
34
+ console.group('--- trunc ---')
76
35
  console.log(
77
- String(i + 1).padStart(2, '0'),
78
- value.padEnd(65),
79
- '=>',
80
- `"${jc.formatDataHora(data)}"`,
36
+ 'Números somados em um único trunc() 248.08 =',
37
+ jc.trunc(123.99889889 + 123.99889889 + 0.09),
81
38
  )
82
- })
83
39
 
84
- console.groupEnd()
85
- console.groupEnd()
40
+ console.log(
41
+ 'Soma sem quebra (com 2 casas decimais) e SEM trunc() 248.07 =',
42
+ 123.99 + 123.99 + 0.09,
43
+ )
86
44
 
87
- console.log('')
88
- console.group('updateDotEnv, updateVersionDate, retirarAcentuação')
45
+ console.log(
46
+ 'Soma com cada numero quebrado dentro de um trunc() 248.07 =',
47
+ jc.trunc(123.99889889) + jc.trunc(123.99889889) + jc.trunc(0.0999999),
48
+ )
89
49
 
90
- update.updateDotEnv(fs, true)
91
- console.log('')
50
+ console.groupEnd()
51
+
52
+ // ==================================================
53
+ console.group('Datas')
54
+
55
+ const datas = [
56
+ '01/01/2023 23:00:00',
57
+ '01/01/2023 15:00:00',
58
+ '2023-01-01T23:00:00',
59
+ '2023-01-01T15:00:00',
60
+ '',
61
+ new Date('2023-01-01T23:00:00'),
62
+ new Date('2023-01-01T00:00:00'),
63
+ '',
64
+ '2023-01-01',
65
+ '2023-01-01T00:00:00',
66
+ '',
67
+ new Date('2023-01-01T23:00:00'),
68
+ new Date('2023-01-01T00:00:00'),
69
+ jc.getDateTimeStr(),
70
+ '2023-10-08T23:40',
71
+ ]
72
+
73
+ datas.forEach((data, i) => {
74
+ let value = data
75
+ if (typeof value != 'string') {
76
+ value = data.toString()
77
+ }
78
+ console.log(
79
+ String(i + 1).padStart(2, '0'),
80
+ value.padEnd(65),
81
+ '=>',
82
+ `"${jc.formatDataHora(data)}"`,
83
+ )
84
+ })
85
+
86
+ console.groupEnd()
87
+ console.groupEnd()
88
+
89
+ console.log('')
90
+ console.group('updateDotEnv, updateVersionDate, retirarAcentuação')
91
+
92
+ update.updateDotEnv(fs, true)
93
+ console.log('')
92
94
 
93
- console.log(
94
- 'retirarAcentuacao',
95
- retirarAcentuacao('Criança, mãe, João, MÃE, anão, ANÃO'),
96
- )
95
+ console.log(
96
+ 'retirarAcentuacao',
97
+ retirarAcentuacao('Criança, mãe, João, MÃE, anão, ANÃO'),
98
+ )
97
99
 
98
- let arq = '.env.production.local'
99
- console.log('')
100
- console.log('Arquivo existe?', arq, jc.fileExists(fs, arq))
100
+ let arq = '.env.production.local'
101
+ console.log('')
102
+ console.log('Arquivo existe?', arq, jc.fileExists(fs, arq))
101
103
 
102
- arq = '.env.local'
103
- console.log('Arquivo existe?', arq, jc.fileExists(fs, arq))
104
+ arq = '.env.local'
105
+ console.log('Arquivo existe?', arq, jc.fileExists(fs, arq))
104
106
 
105
- console.log('')
106
- update.updateDotEnvKey(fs, true)
107
+ console.log('')
108
+ update.updateDotEnvKey(fs, true)
107
109
 
108
- console.log('')
109
- update.updateVersionBuild(fs, true)
110
- update.updateVersionDate(fs)
110
+ console.log('')
111
+ update.updateVersionBuild(fs, true)
112
+ update.updateVersionDate(fs)
111
113
 
112
- console.log('')
113
- jc.scrollToTop()
114
+ console.log('')
115
+ jc.scrollToTop()
114
116
 
115
- console.log('jcinfoUtils.showLogs', jc.jcinfoUtils.showLogs)
117
+ console.log('jcinfoUtils.showLogs', jc.jcinfoUtils.showLogs)
118
+
119
+ console.log('')
120
+ console.groupEnd()
121
+ }
116
122
 
117
- console.log('')
118
- console.groupEnd()
123
+ console.log('Cnpj: 05438177000199', validarCNPJ('05438177000198'))
124
+ console.log('Cpf : 068816807-83', validarCPF('068816807-83'))