jcinfo-utils 1.0.4

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.
Files changed (2) hide show
  1. package/index.js +273 -0
  2. package/package.json +12 -0
package/index.js ADDED
@@ -0,0 +1,273 @@
1
+ // ===================================================== LOGS
2
+
3
+ function addLog({ ...args }) {
4
+ if (process.env.NODE_ENV === 'development') {
5
+ console.info(...args)
6
+ }
7
+ }
8
+
9
+ /**
10
+ *
11
+ * @returns {boolean}
12
+ */
13
+ function isMobile() {
14
+ return (
15
+ navigator.userAgent.match(/Android/i) ||
16
+ navigator.userAgent.match(/webOS/i) ||
17
+ navigator.userAgent.match(/iPhone/i) ||
18
+ navigator.userAgent.match(/iPad/i) ||
19
+ navigator.userAgent.match(/iPod/i) ||
20
+ navigator.userAgent.match(/BlackBerry/i) ||
21
+ navigator.userAgent.match(/Windows Phone/i)
22
+ )
23
+ }
24
+
25
+ /**
26
+ *
27
+ * @returns {boolean}
28
+ */
29
+ function isDesktop() {
30
+ return !isMobile()
31
+ }
32
+
33
+ /**
34
+ *
35
+ * @returns {boolean}
36
+ */
37
+ function isDarkMode() {
38
+ return window.matchMedia('(prefers-color-scheme:dark)').matches
39
+ }
40
+
41
+ // ========================================== clarear cores
42
+
43
+ /**
44
+ * @param {string} hex
45
+ * @param {number} gain
46
+ * @returns {string}
47
+ */
48
+ const colorGain = (hex, gain) => {
49
+ if (hex.startsWith('#')) {
50
+ hex = hex.substring(1, 7)
51
+ }
52
+ var r = Number.parseInt(
53
+ Number.parseInt(hex.substring(0, 2), 16) * (gain / 100 + 1),
54
+ )
55
+ .toString(16)
56
+ .padStart(2, '0')
57
+ .substring(0, 2)
58
+
59
+ var g = Number.parseInt(
60
+ Number.parseInt(hex.substring(2, 4), 16) * (gain / 100 + 1),
61
+ )
62
+ .toString(16)
63
+ .padStart(2, '0')
64
+ .substring(0, 2)
65
+
66
+ var b = Number.parseInt(
67
+ Number.parseInt(hex.substring(4, 6), 16) * (gain / 100 + 1),
68
+ )
69
+ .toString(16)
70
+ .padStart(2, '0')
71
+ .substring(0, 2)
72
+ return `#${r}${g}${b}`
73
+ }
74
+
75
+ // =======================================================
76
+ // Manipulação de numeros //
77
+ // =======================================================
78
+
79
+ function decimalAdjust(type, value, exp) {
80
+ // Se exp é indefinido ou zero...
81
+ if (typeof exp === 'undefined' || +exp === 0) {
82
+ return Math[type](value)
83
+ }
84
+ value = +value
85
+ exp = +exp
86
+ // Se o valor não é um número ou o exp não é inteiro...
87
+ if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
88
+ return NaN
89
+ }
90
+ // Transformando para string
91
+ value = value.toString().split('e')
92
+ value = Math[type](+(value[0] + 'e' + (value[1] ? +value[1] - exp : -exp)))
93
+ // Transformando de volta
94
+ value = value.toString().split('e')
95
+ return +(value[0] + 'e' + (value[1] ? +value[1] + exp : exp))
96
+ }
97
+
98
+ // Arredondamento decimal
99
+ if (!Math.round) {
100
+ Math.round = function (value, exp) {
101
+ return decimalAdjust('round', value, exp)
102
+ }
103
+ }
104
+
105
+ // Decimal arredondado para baixo
106
+ if (!Math.floor) {
107
+ Math.floor = function (value, exp) {
108
+ return decimalAdjust('floor', value, exp)
109
+ }
110
+ }
111
+
112
+ // Decimal arredondado para cima
113
+ if (!Math.ceil) {
114
+ Math.ceil = function (value, exp) {
115
+ return decimalAdjust('ceil', value, exp)
116
+ }
117
+ }
118
+
119
+ /**
120
+ *
121
+ * @param {number} valor
122
+ * @param {number} dec
123
+ * @returns {number}
124
+ */
125
+ function trunc(valor = 0, dec = 2) {
126
+ let result = decimalAdjust('round', valor, dec * -1)
127
+ return result
128
+ }
129
+
130
+ /**
131
+ *
132
+ * @param {number} max
133
+ * @returns {number}
134
+ */
135
+ function random(max = 99999999) {
136
+ return Math.floor(Math.random() * max + 1)
137
+ }
138
+
139
+ // =======================================================
140
+ // Manipulação de datas //
141
+ // =======================================================
142
+ /**
143
+ *
144
+ * @param {Date} data
145
+ * @returns {string}
146
+ */
147
+ function getDateStr(data = new Date()) {
148
+ if (typeof data === 'string') {
149
+ data = new Date(data)
150
+ }
151
+ let result = data.toLocaleDateString('pt-br')
152
+ result = result.split('/').reverse().join('-')
153
+ return result
154
+ }
155
+
156
+ /**
157
+ *
158
+ * @param {Date} data
159
+ * @returns {string}
160
+ */
161
+ function getDateTimeStr(data = new Date()) {
162
+ let result = getDateStr(data)
163
+ result += 'T' + data.toLocaleTimeString('pt-br').substring(0, 5)
164
+ return result
165
+ }
166
+
167
+ /**
168
+ *
169
+ * @param {Date} data
170
+ * @returns {string}
171
+ */
172
+ function getFirstDay(data = new Date()) {
173
+ data.setDate(1)
174
+ return getDateStr(data)
175
+ }
176
+
177
+ /**
178
+ *
179
+ * @param {Date} data
180
+ * @returns {string}
181
+ */
182
+ function getLastDay(data = new Date()) {
183
+ data.setDate(1)
184
+ data.setMonth(data.getMonth() + 1)
185
+ data.setDate(data.getDate() - 1)
186
+ return getDateStr(data)
187
+ }
188
+
189
+ /**
190
+ *
191
+ * @param {number} mes
192
+ * @returns {string}
193
+ */
194
+ function getMonthName(mes) {
195
+ let data = new Date(2023, mes, 1)
196
+ return data.toLocaleString('pt-br', { month: 'long' })
197
+ }
198
+
199
+ /**
200
+ *
201
+ * @param {string} dataNascimento string de data no formato JS (yyyy-mm-dd)
202
+ * @param {string} dataFalecimento string de data no formato JS (yyyy-mm-dd)
203
+ * @returns {string}
204
+ */
205
+ function calcularIdade(dataNascimento, dataFalecimento) {
206
+ if (!dataNascimento || !dataFalecimento) {
207
+ return ''
208
+ }
209
+ const dn = new Date(dataNascimento)
210
+ const df = new Date(dataFalecimento)
211
+ if (df < dn) {
212
+ return ''
213
+ }
214
+ let dias = 0
215
+ let meses = 0
216
+ //anos
217
+ let anos = df.getFullYear() - dn.getFullYear()
218
+ if (df.getMonth() < dn.getMonth()) {
219
+ anos--
220
+ } else if (df.getMonth() === dn.getMonth() && df.getDate() < dn.getDate()) {
221
+ anos--
222
+ }
223
+ //meses
224
+ if (df.getMonth() === dn.getMonth()) {
225
+ if (df.getDate() < dn.getDate()) {
226
+ meses = 12
227
+ }
228
+ } else {
229
+ if (df.getMonth() > dn.getMonth()) {
230
+ meses = df.getMonth() - dn.getMonth()
231
+ } else {
232
+ meses = df.getMonth() + (12 - dn.getMonth())
233
+ }
234
+ }
235
+ //dias
236
+ if (df.getDate() !== dn.getDate()) {
237
+ if (df.getDate() > dn.getDate()) {
238
+ dias = df.getDate() - dn.getDate()
239
+ } else {
240
+ if (meses > 0) {
241
+ meses--
242
+ }
243
+ dias = 30 - (dn.getDate() - df.getDate())
244
+ }
245
+ }
246
+ let result = ''
247
+ if (anos > 0) {
248
+ result += `${anos} ano${anos > 1 ? 's' : ''}`
249
+ }
250
+ if (meses > 0) {
251
+ result += `${result ? ', ' : ''}${meses} ${meses > 1 ? 'meses' : 'mês'}`
252
+ }
253
+ if (dias > 0) {
254
+ result += `${result ? ', ' : ''}${dias} dia${dias ? 's' : ''}`
255
+ }
256
+ return result
257
+ }
258
+
259
+ module.exports = {
260
+ addLog,
261
+ calcularIdade,
262
+ colorGain,
263
+ getDateStr,
264
+ getDateTimeStr,
265
+ getFirstDay,
266
+ getLastDay,
267
+ getMonthName,
268
+ isDarkMode,
269
+ isDesktop,
270
+ isMobile,
271
+ random,
272
+ trunc,
273
+ }
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "jcinfo-utils",
3
+ "version": "1.0.4",
4
+ "description": "Pacote de funções utilitárias",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [],
10
+ "author": "JC",
11
+ "license": "ISC"
12
+ }