jcinfo-utils 1.0.24 → 1.0.26
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/index.d.ts +46 -2
- package/index.js +27 -15
- package/package.json +2 -2
- package/src/updateDotEnv.js +62 -0
- package/test/testar.js +48 -0
- package/dates.d.ts +0 -41
- package/testar.js +0 -28
- package/updateDotEnv.js +0 -23
- /package/{dates.js → src/dates.js} +0 -0
- /package/{formatFileSize.js → src/formatFileSize.js} +0 -0
package/index.d.ts
CHANGED
|
@@ -1,3 +1,47 @@
|
|
|
1
|
+
export function getDateStr(data: Date): string
|
|
2
|
+
|
|
3
|
+
export function getDateTimeStr(data?: Date): string
|
|
4
|
+
|
|
5
|
+
export function getFirstDay(data: Date): string
|
|
6
|
+
|
|
7
|
+
export function getLastDay(data: 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,
|
|
35
|
+
decimalScale: number,
|
|
36
|
+
money: boolean,
|
|
37
|
+
): string
|
|
38
|
+
|
|
39
|
+
export function formatData(data: Date | string): string
|
|
40
|
+
|
|
41
|
+
export function formatDataHora(data: Date | string): string
|
|
42
|
+
|
|
43
|
+
// ======================================================= outras funções
|
|
44
|
+
|
|
1
45
|
export function addLog({ ...args }: { [x: string]: any }): void
|
|
2
46
|
|
|
3
47
|
export function addUserSelectNoneInMobile(): void
|
|
@@ -10,7 +54,7 @@ export function isDarkMode(): boolean
|
|
|
10
54
|
|
|
11
55
|
export function colorGain(hex: string, gain: number): string
|
|
12
56
|
|
|
13
|
-
export function trunc(valor
|
|
57
|
+
export function trunc(valor: number, decimais?: number): number
|
|
14
58
|
|
|
15
59
|
export function random(max?: number): number
|
|
16
60
|
|
|
@@ -20,4 +64,4 @@ export function scrollToTop(smooth?: boolean): void
|
|
|
20
64
|
|
|
21
65
|
export function formatFileSize(bytes: number, decimalPoint: number): string
|
|
22
66
|
|
|
23
|
-
export function updateDotEnv(): void
|
|
67
|
+
export function updateDotEnv(incrementBuild?: boolean): void
|
package/index.js
CHANGED
|
@@ -8,9 +8,9 @@ import {
|
|
|
8
8
|
getFirstDay,
|
|
9
9
|
getLastDay,
|
|
10
10
|
getMonthName,
|
|
11
|
-
} from './dates.js'
|
|
12
|
-
import { updateDotEnv } from './updateDotEnv.js'
|
|
13
|
-
import { formatFileSize } from './formatFileSize.js'
|
|
11
|
+
} from './src/dates.js'
|
|
12
|
+
import { updateDotEnv } from './src/updateDotEnv.js'
|
|
13
|
+
import { formatFileSize } from './src/formatFileSize.js'
|
|
14
14
|
|
|
15
15
|
// ===================================================== LOGS
|
|
16
16
|
|
|
@@ -21,21 +21,29 @@ function addLog({ ...args }) {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
function addUserSelectNoneInMobile() {
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
try {
|
|
25
|
+
if (isMobile()) {
|
|
26
|
+
document.body.style.userSelect = 'none'
|
|
27
|
+
}
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.log('addUserSelectNoneInMobile():', error)
|
|
26
30
|
}
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
function isMobile() {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
try {
|
|
35
|
+
return (
|
|
36
|
+
navigator.userAgent.match(/Android/i) ||
|
|
37
|
+
navigator.userAgent.match(/webOS/i) ||
|
|
38
|
+
navigator.userAgent.match(/iPhone/i) ||
|
|
39
|
+
navigator.userAgent.match(/iPad/i) ||
|
|
40
|
+
navigator.userAgent.match(/iPod/i) ||
|
|
41
|
+
navigator.userAgent.match(/BlackBerry/i) ||
|
|
42
|
+
navigator.userAgent.match(/Windows Phone/i)
|
|
43
|
+
)
|
|
44
|
+
} catch (error) {
|
|
45
|
+
return false
|
|
46
|
+
}
|
|
39
47
|
}
|
|
40
48
|
|
|
41
49
|
function isDesktop() {
|
|
@@ -43,7 +51,11 @@ function isDesktop() {
|
|
|
43
51
|
}
|
|
44
52
|
|
|
45
53
|
function isDarkMode() {
|
|
46
|
-
|
|
54
|
+
try {
|
|
55
|
+
return window.matchMedia('(prefers-color-scheme:dark)').matches
|
|
56
|
+
} catch (error) {
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
47
59
|
}
|
|
48
60
|
|
|
49
61
|
// ========================================== clarear cores
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jcinfo-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.26",
|
|
4
4
|
"description": "Pacote de funções utilitárias",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"test": "nodemon testar.js",
|
|
8
|
+
"test": "nodemon test/testar.js",
|
|
9
9
|
"pub": "yarn publish"
|
|
10
10
|
},
|
|
11
11
|
"keywords": [],
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
//import fs from 'node:fs'
|
|
2
|
+
|
|
3
|
+
function generateBuildVersion() {
|
|
4
|
+
// const date = new Date('2023-02-08T12:00:00')
|
|
5
|
+
const date = new Date()
|
|
6
|
+
const mes = String(date.getMonth() + 1)
|
|
7
|
+
const dia = String(date.getDate()).padStart(2, '0')
|
|
8
|
+
return mes + dia
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function getContentFileInArray(fs) {
|
|
12
|
+
return fs.readFileSync('.env', 'utf8').split('\r\n')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function saveContentArrayInFile(fs, arr) {
|
|
16
|
+
arr = arr.join('\r\n')
|
|
17
|
+
fs.writeFileSync('.env', arr, 'utf8')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function incrementBuildNumber(build, finalBuild) {
|
|
21
|
+
const SEPARATOR = '.'
|
|
22
|
+
if (build && build.includes('(')) {
|
|
23
|
+
build = build.replaceAll('(', '').replaceAll(')', '')
|
|
24
|
+
let [min, max] = build.split('.')
|
|
25
|
+
// console.log(build, min, finalBuild)
|
|
26
|
+
if (min == finalBuild) {
|
|
27
|
+
if (max) {
|
|
28
|
+
max = Number(max) + 1
|
|
29
|
+
} else {
|
|
30
|
+
max = 1
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
min = finalBuild
|
|
34
|
+
max = 0
|
|
35
|
+
}
|
|
36
|
+
finalBuild = min
|
|
37
|
+
if (max > 0) {
|
|
38
|
+
finalBuild += SEPARATOR + max
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return finalBuild
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function updateDotEnv(fs, incrementBuild = false) {
|
|
45
|
+
const KEYS = ['VITE_VERSION', 'REACT_APP_VERSION']
|
|
46
|
+
let content = getContentFileInArray(fs)
|
|
47
|
+
content = content.map((line) => {
|
|
48
|
+
let [key, value] = line.split('=')
|
|
49
|
+
if (KEYS.includes(key) && value) {
|
|
50
|
+
let [version, build] = value.split(' ')
|
|
51
|
+
let finalBuild = generateBuildVersion()
|
|
52
|
+
if (incrementBuild) {
|
|
53
|
+
finalBuild = incrementBuildNumber(build, finalBuild)
|
|
54
|
+
}
|
|
55
|
+
version = `${version} (${finalBuild})`
|
|
56
|
+
line = `${key}=${version}`
|
|
57
|
+
console.log('Versão atualizada:', version)
|
|
58
|
+
}
|
|
59
|
+
return line
|
|
60
|
+
})
|
|
61
|
+
saveContentArrayInFile(fs, content)
|
|
62
|
+
}
|
package/test/testar.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as jc from '../index.js'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
|
|
4
|
+
function testFormatFileSize() {
|
|
5
|
+
let content = fs.readFileSync('c:\\jcinfo\\fb3\\databases.conf')
|
|
6
|
+
// let content = fs.readFileSync('testar.js', 'utf8')
|
|
7
|
+
console.log('formatFileSize() :', jc.formatFileSize(content.length, 3))
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
console.clear()
|
|
11
|
+
console.group('--- Testes ---')
|
|
12
|
+
console.log(
|
|
13
|
+
'formatValor(123, 3, false) => ',
|
|
14
|
+
jc.formatValor(123, 3, false),
|
|
15
|
+
)
|
|
16
|
+
console.log(
|
|
17
|
+
'formatValor(654654.65654, 2, false) => ',
|
|
18
|
+
jc.formatValor(jc.trunc(654654.65654, 2), 2, false),
|
|
19
|
+
' (usando trunc())',
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
console.log()
|
|
23
|
+
console.log("getHash('test') :", jc.getHash('test'))
|
|
24
|
+
|
|
25
|
+
testFormatFileSize()
|
|
26
|
+
|
|
27
|
+
console.log()
|
|
28
|
+
console.group('--- trunc ---')
|
|
29
|
+
console.log(
|
|
30
|
+
'Números somados em um único trunc() 248.08 =',
|
|
31
|
+
jc.trunc(123.99889889 + 123.99889889 + 0.09),
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
console.log(
|
|
35
|
+
'Soma sem quebra (com 2 casas decimais) e SEM trunc() 248.07 =',
|
|
36
|
+
123.99 + 123.99 + 0.09,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
console.log(
|
|
40
|
+
'Soma com cada numero quebrado dentro de um trunc() 248.07 =',
|
|
41
|
+
jc.trunc(123.99889889) + jc.trunc(123.99889889) + jc.trunc(0.0999999),
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
console.groupEnd()
|
|
45
|
+
|
|
46
|
+
jc.updateDotEnv(fs, true)
|
|
47
|
+
|
|
48
|
+
console.groupEnd()
|
package/dates.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
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/testar.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { getDateStr } from './dates.js'
|
|
2
|
-
import * as jc from './index.js'
|
|
3
|
-
import { scrollToTop } from './index.js'
|
|
4
|
-
|
|
5
|
-
console.log(
|
|
6
|
-
jc.formatValor(123, 3, false),
|
|
7
|
-
jc.formatData(new Date(1973, 8, 19)),
|
|
8
|
-
JSON.stringify(jc.calcularIdade('1973-09-19', getDateStr())),
|
|
9
|
-
jc.getDateStr(),
|
|
10
|
-
jc.getDateTimeStr(),
|
|
11
|
-
jc.getFirstDay(),
|
|
12
|
-
jc.getLastDay(),
|
|
13
|
-
jc.getMonthName(new Date().getMonth()),
|
|
14
|
-
jc.trunc(123.99889889),
|
|
15
|
-
)
|
|
16
|
-
|
|
17
|
-
console.log(jc.getHash('jose'))
|
|
18
|
-
|
|
19
|
-
scrollToTop()
|
|
20
|
-
|
|
21
|
-
const value = jc.formatFileSize(156789.88, 3)
|
|
22
|
-
console.log(value)
|
|
23
|
-
|
|
24
|
-
try {
|
|
25
|
-
jc.updateDotEnv()
|
|
26
|
-
} catch (error) {
|
|
27
|
-
console.log('updateDotEnv', error)
|
|
28
|
-
}
|
package/updateDotEnv.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
|
|
3
|
-
export function updateDotEnv() {
|
|
4
|
-
const keys = ['VITE_VERSION', 'REACT_APP_VERSION']
|
|
5
|
-
let content = fs.readFileSync('.env', 'utf8')
|
|
6
|
-
content = content
|
|
7
|
-
.split('\r\n')
|
|
8
|
-
.map((line) => {
|
|
9
|
-
let [key, value] = line.split('=')
|
|
10
|
-
if (keys.includes(key) && value) {
|
|
11
|
-
let [version] = value.split(' ')
|
|
12
|
-
let date = new Date()
|
|
13
|
-
let mes = String(date.getMonth() + 1)
|
|
14
|
-
let dia = String(date.getDate()).padStart(2, '0')
|
|
15
|
-
version = `${version} (${mes}${dia})`
|
|
16
|
-
line = key + '=' + version
|
|
17
|
-
console.log('Versão atualizada:', version)
|
|
18
|
-
}
|
|
19
|
-
return line
|
|
20
|
-
})
|
|
21
|
-
.join('\r\n')
|
|
22
|
-
fs.writeFileSync('.env', content, 'utf8')
|
|
23
|
-
}
|
|
File without changes
|
|
File without changes
|