jcinfo-utils 1.0.16 → 1.0.17

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/dates.d.ts ADDED
@@ -0,0 +1,41 @@
1
+ export function getDateStr(data: Date = new Date()): string
2
+
3
+ export function getDateTimeStr(data?: Date = new Date()): string
4
+
5
+ export function getFirstDay(data: Date = new Date()): string
6
+
7
+ export function getLastDay(data: Date = new Date()): string
8
+
9
+ /**
10
+ * Retorna o nome do mês em português
11
+ * @param mes Número do mês, baseado em 0 = jan, 1 = fev, ...
12
+ * @returns Nome do mês
13
+ */
14
+ export function getMonthName(mes: number): string
15
+
16
+ /**
17
+ * Calcular a idade em anos, meses e dias
18
+ * @param dataNascimento Data em formato JSON
19
+ * @param dataFalecimento Data em formato JSON
20
+ */
21
+ export function calcularIdade(
22
+ dataNascimento: string,
23
+ dataFalecimento: string,
24
+ ): string
25
+
26
+ /**
27
+ * Formatar um valor decimal
28
+ * @param valor Valor decimal, default 0
29
+ * @param decimalScale Numero de casas decimais, default 2
30
+ * @param money Retornar com simbolo R$, default true
31
+ * @returns Valor formatado em reais ou não
32
+ */
33
+ export function formatValor(
34
+ valor: number = 0,
35
+ decimalScale: number = 2,
36
+ money: boolean = true,
37
+ ): string
38
+
39
+ export function formatData(data: Date | string): string
40
+
41
+ export function formatDataHora(data: Date | string): string
package/dates.js ADDED
@@ -0,0 +1,116 @@
1
+ // =======================================================
2
+ // Manipulação de datas //
3
+ // =======================================================
4
+ export function getDateStr(data = new Date()) {
5
+ if (typeof data === 'string') {
6
+ data = new Date(data)
7
+ }
8
+ let result = data.toLocaleDateString('pt-br')
9
+ result = result.split('/').reverse().join('-')
10
+ return result
11
+ }
12
+
13
+ export function getDateTimeStr(data = new Date()) {
14
+ let result = getDateStr(data)
15
+ result += 'T' + data.toLocaleTimeString('pt-br').substring(0, 5)
16
+ return result
17
+ }
18
+
19
+ export function getFirstDay(data = new Date()) {
20
+ data.setDate(1)
21
+ return getDateStr(data)
22
+ }
23
+
24
+ export function getLastDay(data = new Date()) {
25
+ data.setDate(1)
26
+ data.setMonth(data.getMonth() + 1)
27
+ data.setDate(data.getDate() - 1)
28
+ return getDateStr(data)
29
+ }
30
+
31
+ export function getMonthName(mes) {
32
+ let data = new Date(2023, mes, 1)
33
+ return data.toLocaleString('pt-br', { month: 'long' })
34
+ }
35
+
36
+ export function calcularIdade(dataNascimento, dataHoje) {
37
+ if (!dataNascimento || !dataHoje) {
38
+ return ''
39
+ }
40
+ const dn = new Date(dataNascimento)
41
+ const df = new Date(dataHoje)
42
+ if (df < dn) {
43
+ return ''
44
+ }
45
+ let dias = 0
46
+ let meses = 0
47
+ //anos
48
+ let anos = df.getFullYear() - dn.getFullYear()
49
+ if (df.getMonth() < dn.getMonth()) {
50
+ anos--
51
+ } else if (df.getMonth() === dn.getMonth() && df.getDate() < dn.getDate()) {
52
+ anos--
53
+ }
54
+ //meses
55
+ if (df.getMonth() === dn.getMonth()) {
56
+ if (df.getDate() < dn.getDate()) {
57
+ meses = 12
58
+ }
59
+ } else {
60
+ if (df.getMonth() > dn.getMonth()) {
61
+ meses = df.getMonth() - dn.getMonth()
62
+ } else {
63
+ meses = df.getMonth() + (12 - dn.getMonth())
64
+ }
65
+ }
66
+ //dias
67
+ if (df.getDate() !== dn.getDate()) {
68
+ if (df.getDate() > dn.getDate()) {
69
+ dias = df.getDate() - dn.getDate()
70
+ } else {
71
+ if (meses > 0) {
72
+ meses--
73
+ }
74
+ dias = 30 - (dn.getDate() - df.getDate())
75
+ }
76
+ }
77
+ let result = ''
78
+ if (anos > 0) {
79
+ result += `${anos} ano${anos > 1 ? 's' : ''}`
80
+ }
81
+ if (meses > 0) {
82
+ result += `${result ? ', ' : ''}${meses} ${meses > 1 ? 'meses' : 'mês'}`
83
+ }
84
+ if (dias > 0) {
85
+ result += `${result ? ', ' : ''}${dias} dia${dias ? 's' : ''}`
86
+ }
87
+ return result
88
+ }
89
+
90
+ export function formatValor(valor = 0, decimalScale = 2, money = true) {
91
+ return Intl.NumberFormat('pt-br', {
92
+ maximumFractionDigits: decimalScale,
93
+ minimumFractionDigits: decimalScale,
94
+ style: money ? 'currency' : 'decimal',
95
+ currency: 'BRL',
96
+ }).format(valor)
97
+ }
98
+
99
+ export function formatData(data) {
100
+ if (typeof data == 'string') {
101
+ data = new Date(data)
102
+ }
103
+ return Intl.DateTimeFormat('pt-br', { dateStyle: 'short' }).format(data)
104
+ }
105
+
106
+ export function formatDataHora(data) {
107
+ if (typeof data == 'string') {
108
+ data = new Date(data)
109
+ }
110
+ let result = Intl.DateTimeFormat('pt-br', {
111
+ dateStyle: 'short',
112
+ timeStyle: 'short',
113
+ }).format(data)
114
+ result = result.split(', ').join(' ')
115
+ return result
116
+ }
package/index.d.ts CHANGED
@@ -13,45 +13,3 @@ export function colorGain(hex: string, gain: number): string
13
13
  export function trunc(valor: number = 0, decimais: number = 2): number
14
14
 
15
15
  export function random(max: number = 99999999): number
16
-
17
- export function getDateStr(data: Date = new Date()): string
18
-
19
- export function getDateTimeStr(data?: Date = new Date()): string
20
-
21
- export function getFirstDay(data: Date = new Date()): string
22
-
23
- export function getLastDay(data: Date = new Date()): string
24
-
25
- /**
26
- * Retorna o nome do mês em português
27
- * @param mes Número do mês, baseado em 0 = jan, 1 = fev, ...
28
- * @returns Nome do mês
29
- */
30
- export function getMonthName(mes: number): string
31
-
32
- /**
33
- * Calcular a idade em anos, meses e dias
34
- * @param dataNascimento Data em formato JSON
35
- * @param dataFalecimento Data em formato JSON
36
- */
37
- export function calcularIdade(
38
- dataNascimento: string,
39
- dataFalecimento: string,
40
- ): string
41
-
42
- /**
43
- * Formatar um valor decimal
44
- * @param valor Valor decimal, default 0
45
- * @param decimalScale Numero de casas decimais, default 2
46
- * @param money Retornar com simbolo R$, default true
47
- * @returns Valor formatado em reais ou não
48
- */
49
- export function formatValor(
50
- valor: number = 0,
51
- decimalScale: number = 2,
52
- money: boolean = true,
53
- ): string
54
-
55
- export function formatData(data: Date | string): string
56
-
57
- export function formatDataHora(data: Date | string): string
package/index.js CHANGED
@@ -1,4 +1,14 @@
1
- import checkUpdate, { showMessage } from './update.js'
1
+ import {
2
+ calcularIdade,
3
+ formatData,
4
+ formatDataHora,
5
+ formatValor,
6
+ getDateStr,
7
+ getDateTimeStr,
8
+ getFirstDay,
9
+ getLastDay,
10
+ getMonthName,
11
+ } from './dates.js'
2
12
 
3
13
  // ===================================================== LOGS
4
14
 
@@ -121,131 +131,14 @@ function random(max = 99999999) {
121
131
  return Math.floor(Math.random() * max + 1)
122
132
  }
123
133
 
124
- // =======================================================
125
- // Manipulação de datas //
126
- // =======================================================
127
- function getDateStr(data = new Date()) {
128
- if (typeof data === 'string') {
129
- data = new Date(data)
130
- }
131
- let result = data.toLocaleDateString('pt-br')
132
- result = result.split('/').reverse().join('-')
133
- return result
134
- }
135
-
136
- function getDateTimeStr(data = new Date()) {
137
- let result = getDateStr(data)
138
- result += 'T' + data.toLocaleTimeString('pt-br').substring(0, 5)
139
- return result
140
- }
141
-
142
- function getFirstDay(data = new Date()) {
143
- data.setDate(1)
144
- return getDateStr(data)
145
- }
146
-
147
- function getLastDay(data = new Date()) {
148
- data.setDate(1)
149
- data.setMonth(data.getMonth() + 1)
150
- data.setDate(data.getDate() - 1)
151
- return getDateStr(data)
152
- }
153
-
154
- function getMonthName(mes) {
155
- let data = new Date(2023, mes, 1)
156
- return data.toLocaleString('pt-br', { month: 'long' })
157
- }
158
-
159
- function calcularIdade(dataNascimento, dataFalecimento) {
160
- if (!dataNascimento || !dataFalecimento) {
161
- return ''
162
- }
163
- const dn = new Date(dataNascimento)
164
- const df = new Date(dataFalecimento)
165
- if (df < dn) {
166
- return ''
167
- }
168
- let dias = 0
169
- let meses = 0
170
- //anos
171
- let anos = df.getFullYear() - dn.getFullYear()
172
- if (df.getMonth() < dn.getMonth()) {
173
- anos--
174
- } else if (df.getMonth() === dn.getMonth() && df.getDate() < dn.getDate()) {
175
- anos--
176
- }
177
- //meses
178
- if (df.getMonth() === dn.getMonth()) {
179
- if (df.getDate() < dn.getDate()) {
180
- meses = 12
181
- }
182
- } else {
183
- if (df.getMonth() > dn.getMonth()) {
184
- meses = df.getMonth() - dn.getMonth()
185
- } else {
186
- meses = df.getMonth() + (12 - dn.getMonth())
187
- }
188
- }
189
- //dias
190
- if (df.getDate() !== dn.getDate()) {
191
- if (df.getDate() > dn.getDate()) {
192
- dias = df.getDate() - dn.getDate()
193
- } else {
194
- if (meses > 0) {
195
- meses--
196
- }
197
- dias = 30 - (dn.getDate() - df.getDate())
198
- }
199
- }
200
- let result = ''
201
- if (anos > 0) {
202
- result += `${anos} ano${anos > 1 ? 's' : ''}`
203
- }
204
- if (meses > 0) {
205
- result += `${result ? ', ' : ''}${meses} ${meses > 1 ? 'meses' : 'mês'}`
206
- }
207
- if (dias > 0) {
208
- result += `${result ? ', ' : ''}${dias} dia${dias ? 's' : ''}`
209
- }
210
- return result
211
- }
212
-
213
- function formatValor(valor = 0, decimalScale = 2, money = true) {
214
- return Intl.NumberFormat('pt-br', {
215
- maximumFractionDigits: decimalScale,
216
- minimumFractionDigits: decimalScale,
217
- style: money ? 'currency' : 'decimal',
218
- currency: 'BRL',
219
- }).format(valor)
220
- }
221
-
222
- function formatData(data) {
223
- if (typeof data == 'string') {
224
- data = new Date(data)
225
- }
226
- return Intl.DateTimeFormat('pt-br', { dateStyle: 'short' }).format(data)
227
- }
228
-
229
- function formatDataHora(data) {
230
- if (typeof data == 'string') {
231
- data = new Date(data)
232
- }
233
- let result = Intl.DateTimeFormat('pt-br', {
234
- dateStyle: 'short',
235
- timeStyle: 'short',
236
- }).format(data)
237
- result = result.split(', ').join(' ')
238
- return result
239
- }
240
-
241
134
  export {
242
135
  addLog,
243
136
  addUserSelectNoneInMobile,
244
137
  calcularIdade,
245
138
  colorGain,
246
- formatValor,
247
139
  formatData,
248
140
  formatDataHora,
141
+ formatValor,
249
142
  getDateStr,
250
143
  getDateTimeStr,
251
144
  getFirstDay,
@@ -256,7 +149,4 @@ export {
256
149
  isMobile,
257
150
  random,
258
151
  trunc,
259
- // funções externas
260
- checkUpdate,
261
- showMessage,
262
152
  }
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
- {
2
- "name": "jcinfo-utils",
3
- "version": "1.0.16",
4
- "description": "Pacote de funções utilitárias",
5
- "main": "index.js",
6
- "type": "module",
7
- "scripts": {
8
- "test": "echo \"Error: no test specified\" && exit 1"
9
- },
10
- "keywords": [],
11
- "author": "JC",
12
- "license": "ISC",
13
- "devDependencies": {
14
- "nodemon": "^3.0.1"
15
- }
16
- }
1
+ {
2
+ "name": "jcinfo-utils",
3
+ "version": "1.0.17",
4
+ "description": "Pacote de funções utilitárias",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "nodemon testar.js"
9
+ },
10
+ "keywords": [],
11
+ "author": "JC",
12
+ "license": "ISC",
13
+ "devDependencies": {
14
+ "nodemon": "^3.0.1"
15
+ }
16
+ }
package/testar.js CHANGED
@@ -1,16 +1,18 @@
1
- // import * as jc from './index.js'
1
+ import * as jc from './index.js'
2
+
3
+ console.log(
4
+ jc.formatValor(123, 3, false),
5
+ jc.formatData(new Date(1973, 8, 19)),
6
+ JSON.stringify(jc.calcularIdade('2023-07-24', '1973-09-19')),
7
+ jc.calcularIdade('2023-07-24', '1973-09-19'),
8
+ jc.getDateStr(),
9
+ jc.getDateTimeStr(),
10
+ jc.getFirstDay(),
11
+ jc.getLastDay(),
12
+ jc.getMonthName(new Date().getMonth()),
13
+ jc.trunc(123.99889889),
14
+ )
2
15
 
3
- // console.log(
4
- // jc.formatValor(123, 3, false),
5
- // jc.formatData(new Date(1973, 8, 19)),
6
- // jc.calcularIdade('2023-07-24', '1973-09-19'),
7
- // jc.getDateStr(),
8
- // jc.getDateTimeStr(),
9
- // jc.getFirstDay(),
10
- // jc.getLastDay,
11
- // jc.getMonthName(new Date().getMonth()),
12
- // jc.trunc(123.99889889),
13
- // )
14
16
  function formatData(data) {
15
17
  if (typeof data == 'string') {
16
18
  data = new Date(data)
package/update.css DELETED
@@ -1,67 +0,0 @@
1
- :root {
2
- --margin-up: 15px;
3
- --margin-down: -120px;
4
- }
5
-
6
- @keyframes entrada {
7
- 0% {
8
- opacity: 0;
9
- bottom: var(--margin-down);
10
- }
11
- 100% {
12
- opacity: 1;
13
- bottom: var(--margin-up);
14
- }
15
- }
16
-
17
- .c-update {
18
- position: fixed;
19
- right: 20px;
20
- bottom: var(--margin-up);
21
-
22
- display: flex;
23
- align-items: center;
24
- gap: 8px;
25
-
26
- background-color: #d6f5ff;
27
- color: #333;
28
- padding: 15px 20px;
29
- border: 1px solid var(--bs-info);
30
- border-radius: 4px;
31
- box-shadow: var(--sombra-login);
32
-
33
- font-family: 'Segoe UI', Geneva, Verdana, sans-serif;
34
- font-size: 0.9rem;
35
- width: auto;
36
- white-space: nowrap;
37
- user-select: none;
38
- animation: entrada 1s both;
39
- z-index: 9999991;
40
- }
41
- .c-update i {
42
- color: var(--bs-info);
43
- font-size: 1.3rem;
44
- }
45
-
46
- .c-line {
47
- position: absolute;
48
- min-height: 85%;
49
- border-left: 3px solid var(--bs-info);
50
- left: 4px;
51
- }
52
-
53
- @media (max-width: 575px) {
54
- :root {
55
- --margin-up: 5px;
56
- }
57
-
58
- .c-update {
59
- max-width: 95vw;
60
- width: 95vw;
61
- white-space: unset;
62
- right: initial;
63
- left: 50%;
64
- transform: translateX(-50%);
65
- border-radius: 0;
66
- }
67
- }
package/update.js DELETED
@@ -1,54 +0,0 @@
1
- import './update.css'
2
-
3
- const cMessagUpdating =
4
- '<div class="c-line"></div>' +
5
- '<i class="fas fa-info-circle"></i>' +
6
- 'Nova versão disponível, atualizando...'
7
-
8
- let divUpdate = document.querySelector('#update')
9
-
10
- if (!divUpdate) {
11
- divUpdate = document.createElement('div')
12
- divUpdate.id = 'update'
13
- document.body.appendChild(divUpdate)
14
- }
15
-
16
- export function showMessage() {
17
- divUpdate.innerHTML = cMessagUpdating
18
- divUpdate.classList.add('c-update')
19
- console.info('Mostrando msg...')
20
- }
21
-
22
- export default function checkUpdate() {
23
- console.info('Versão:', process.env.REACT_APP_VERSION)
24
- console.info('Server:', process.env.REACT_APP_BASEURL)
25
- console.info('App:', window.location.origin)
26
- if ('serviceWorker' in navigator && window.location.origin.startsWith('http')) {
27
- console.log('Procurando atualizações...')
28
- navigator.serviceWorker
29
- .getRegistration()
30
- .then((registration) => {
31
- if (!registration) {
32
- console.info('Sem Service Worker!')
33
- return
34
- }
35
- console.info('=> Service Worker encontrado:', registration)
36
- registration.addEventListener('updatefound', () => {
37
- showMessage()
38
- const installingWorker = registration.installing
39
- installingWorker.addEventListener('statechange', () => {
40
- if (installingWorker.state === 'installed') {
41
- if (navigator.serviceWorker.controller) {
42
- window.location.reload(true)
43
- }
44
- }
45
- })
46
- })
47
- })
48
- .catch((error) => {
49
- console.error('=> Service Worker não encontrado:', error)
50
- })
51
- }
52
- }
53
-
54
- //showMessage()