This commit is contained in:
marcio 2025-11-10 16:03:15 -03:00
commit e6fa9640bc
83 changed files with 5480 additions and 969 deletions

53
src/cacheMemoria.ts Normal file
View file

@ -0,0 +1,53 @@
/** gerar uma função de cache para uso em memoria */
const _cache: {
[k: string]:
| {
/** new Date().getTime() */
validade?: number | undefined
valor: any
}
| undefined
} = {}
;(globalThis as any).cacheMemoria_cache = _cache
export const cacheM = <T>(
chave: any,
valor?: T,
validadeSeg?: number,
): T | undefined => {
// converte a chave e string
const txChave: string =
typeof chave == "string"
? chave
: typeof chave == "number"
? String(chave)
: encodeURIComponent(JSON.stringify(chave))
const validade = validadeSeg && new Date().getTime() + validadeSeg * 1000
if (valor !== undefined) {
_cache[txChave] = {
valor,
validade,
}
}
const busca = _cache[txChave]
if (busca?.validade && busca.validade < new Date().getTime()) {
return undefined
}
return busca?.valor
}
export const verCacheM = () => _cache
export const cacheMemoria = cacheM
/** para uso fixo dentro de uma função, trava a chave */
export const cacheMFixo =
(chave: any) =>
<T>(valor?: T) =>
cacheM(chave, valor)

View file

@ -8,7 +8,7 @@ export enum camposComuns {
data_hora_atualizacao = "data_hora_atualizacao",
codigo_usuario_criacao = "codigo_usuario_criacao",
codigo_usuario_atualizacao = "codigo_usuario_atualizacao",
versao = "__versao",
versao = "versao",
}
/** Palavras comumente usadas */

View file

@ -1,3 +1,5 @@
import z from "zod"
export enum operadores {
"=" = "=",
"!=" = "!=",
@ -26,3 +28,21 @@ export type interfaceConsulta = {
apenasConsulta?: boolean
apenasContagem?: boolean
}
export const zOperadores = z.enum([
"=",
"!=",
">",
">=",
"<",
"<=",
"like",
"in",
])
export const zFiltro = z.object({
coluna: z.string(),
valor: z.any(),
operador: zOperadores,
ou: z.boolean().optional(),
})

25
src/dayjs.ts Normal file
View file

@ -0,0 +1,25 @@
import dayjs from "dayjs"
export type { ManipulateType } from "dayjs"
import duration from "dayjs/plugin/duration.js"
import isSameOrAfter from "dayjs/plugin/isSameOrAfter.js"
import isSameOrBefore from "dayjs/plugin/isSameOrBefore.js"
import minMax from "dayjs/plugin/minMax.js"
import relativeTime from "dayjs/plugin/relativeTime.js"
import timezone from "dayjs/plugin/timezone.js"
import utc from "dayjs/plugin/utc.js"
import weekOfYear from "dayjs/plugin/weekOfYear.js"
import "dayjs/locale/pt-br.js"
dayjs.locale("pt-br")
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(weekOfYear)
dayjs.extend(isSameOrBefore)
dayjs.extend(isSameOrAfter)
dayjs.extend(minMax)
dayjs.extend(relativeTime)
dayjs.extend(duration)
export const dayjsbr = dayjs

12
src/graficosPilao.ts Normal file
View file

@ -0,0 +1,12 @@
export const graficos_pilao: {
[k: string]: { grafico: string; titulo: string }
} = {
Condicionantes: {
grafico: "condicionantes-criadas",
titulo: "Condicionantes Criadas",
},
Licenças: {
grafico: "licencas-criadas",
titulo: "Licenças Criadas",
},
}

View file

@ -1,12 +1,21 @@
export * from "./aleatorio"
export * from "./cacheMemoria"
export * from "./constantes"
export * from "./consulta"
export * from "./aleatorio"
export * from "./dayjs"
export * from "./ecosistema"
export * from "./extensoes"
export * from "./graficosPilao"
export * from "./local"
export * from "./logger"
export * from "./postgres"
export * from "./testes-de-variaveis"
export * from "./texto_busca"
export * from "./tipagemRotas"
export * from "./unidades_medida"
export * from "./uuid"
export * from "./ecosistema"
export * from "./variaveisComuns"
export * from "./tipagemRotas"
export * from "./extensoes"
export * from "./logger"
export * from "./situacoes"
export * from "./situacoes"

57
src/instalarAmbiente.ts Normal file
View file

@ -0,0 +1,57 @@
import fs from "node:fs"
import path from "node:path"
/**
* Mescla objetos recursivamente.
* - Adiciona chaves novas
* - Sobrescreve valores primitivos
* - Mescla objetos aninhados
*/
const mesclar = (entrada: any, novo: any): any => {
const saida = { ...(entrada || {}) }
for (const [k, v] of Object.entries(novo)) {
if (v && typeof v === "object" && !Array.isArray(v)) {
saida[k] = mesclar(saida[k], v)
} else {
saida[k] = v
}
}
return saida
}
/** Lê JSON ou retorna objeto vazio */
const abrirJson = (caminho: string) => {
try {
return JSON.parse(fs.readFileSync(caminho, "utf-8"))
} catch {
return {}
}
}
const settings_json = {
"editor.defaultFormatter": "biomejs.biome",
"[javascript]": { "editor.defaultFormatter": "biomejs.biome" },
"[javascriptreact]": { "editor.defaultFormatter": "biomejs.biome" },
"[typescript]": { "editor.defaultFormatter": "biomejs.biome" },
"[typescriptreact]": { "editor.defaultFormatter": "biomejs.biome" },
"[json]": { "editor.defaultFormatter": "biomejs.biome" },
"[jsonc]": { "editor.defaultFormatter": "biomejs.biome" },
"[vue]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
"editor.codeActionsOnSave": {
"source.organizeImports.biome": "always",
"source.fixAll.biome": "always",
},
}
const caminhoSeting = path.join(process.cwd(), ".vscode/settings.json")
// Garante a pasta .vscode
fs.mkdirSync(path.dirname(caminhoSeting), { recursive: true })
// Mescla e grava
const atual = abrirJson(caminhoSeting)
const final = mesclar(atual, settings_json)
fs.writeFileSync(caminhoSeting, JSON.stringify(final, null, 2), "utf8")
console.log(`✅ Configurações salvas em ${caminhoSeting}`)

38
src/local/index.ts Normal file
View file

@ -0,0 +1,38 @@
/**
* LocalStorage Tipado
* ou grava um valor no localStorage, mantendo o tipo genérico <T>.
*/
export const localValor = <T>(
chave_: string | any,
valor?: T | null,
): T | null => {
const localStorage = globalThis.localStorage
if (typeof localStorage == "undefined") return null
const chave =
typeof chave_ === "string"
? chave_
: encodeURIComponent(JSON.stringify(chave_))
try {
// Grava valor se fornecido
if (valor !== undefined) {
localStorage.setItem(chave, JSON.stringify(valor))
}
// Lê valor
const v2 = localStorage.getItem(chave)
if (v2 === null) return null
try {
return JSON.parse(v2) as T
} catch {
// Caso o valor não seja JSON válido
return v2 as unknown as T
}
} catch {
// Em caso de erro de acesso ao localStorage
return null
}
}

View file

@ -14,9 +14,9 @@ export type tipoLokiObjeto = {
export const postLogger = async ({
objeto,
}: { objeto: tipoLokiObjeto }): Promise<
[objeto: tipoLokiObjeto, erro?: string]
> => {
}: {
objeto: tipoLokiObjeto
}): Promise<[objeto: tipoLokiObjeto, erro?: string]> => {
const response = await crossFetch(`${LOKI_BASE_URL}${LOKI_ENDPOINT}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
@ -24,9 +24,8 @@ export const postLogger = async ({
}).catch((a) => a)
if (!response.ok) {
return [objeto, `Erro ${response.status}: ${await response?.text?.()}`]
} else {
return [objeto]
}
return [objeto]
}
let cwd = ""

32
src/postgres.ts Normal file
View file

@ -0,0 +1,32 @@
/**
* Trata um objeto para ser imput para postgres
* @param entrada
* @returns
*/
export const paraObjetoRegistroPg = (entrada: {
[k: string]: any
}): {
[k: string]: string | boolean | null | undefined | number
} => {
try {
return Object.fromEntries(
Object.entries(entrada).map(([k, v]) => [
k,
v === undefined || v == null
? v
: typeof v == "string" ||
typeof v == "number" ||
typeof v == "boolean"
? v
: JSON.stringify(v, null, 2),
]),
)
} catch (error) {
throw new Error(
`Erro na função paraObjetoRegistroPg: ${(error as Error).message} ${(error as Error).stack}`,
)
}
}
export const pgObjeto = paraObjetoRegistroPg
export const objetoPg = paraObjetoRegistroPg

View file

@ -1,13 +1,8 @@
import { logger } from "./logger"
import { cacheM } from "./cacheMemoria"
import { texto_busca } from "./texto_busca"
const l = logger({ app: "teste", eProducao: true })
// node dist-back/teste.mjs
console.log("Variáveis funcionando", texto_busca)
const lg = l({
inquilino: "conta_1",
usuario: "pedrinho",
})
lg("error", "Deu Ruim").then((a) => console.log(JSON.stringify(a)))
lg("info", "Deu Bom", { __filename }).then((a) =>
console.log(JSON.stringify(a)),
)
cacheM(1, { Jaca: Promise.resolve() })
console.log("cache:", cacheM(1))

View file

@ -0,0 +1,2 @@
export * from "./umaFuncao"
export * from "./umaVariavel"

View file

@ -0,0 +1 @@
export const umaFuncao = () => "Olá Mundo! (função)"

View file

@ -0,0 +1 @@
export const umaVariavel = "Olá Mundo! (variável)"

View file

@ -0,0 +1,29 @@
import { describe, expect, it } from "vitest"
import { TipagemRotas } from "../tipagemRotas"
describe("TipagemRotas", () => {
it("deve montar _partesCaminho a partir de string ou array, normalizando barras", () => {
const r1 = new TipagemRotas<{ q: string }>({ caminho: "aplicacao/func" })
expect(r1.caminho).toBe("/aplicacao/func")
const r2 = new TipagemRotas<{ q: string }>({
caminho: ["aplicacao", "func"],
})
expect(r2.caminho).toBe("/aplicacao/func")
const r3 = new TipagemRotas<{ q: string }>({ caminho: "/a//b///c/" })
expect(r3.caminho).toBe("/a/b/c")
})
it("Valores de entrada com mesmo valor dos valores de saída", () => {
type tipagem = { nome: string; idade: number }
const r1 = new TipagemRotas<tipagem>({ caminho: "aplicacao/func" })
const objetoEntrada: tipagem = { idade: 21, nome: "João" }
const rota = r1.endereco(objetoEntrada)
const parametros = r1.parametros(rota)
expect(parametros.nome).toBe(objetoEntrada.nome)
})
})

View file

@ -7,7 +7,6 @@ export const texto_busca = (...texto: any[]): string =>
? ""
: String(txt)
.normalize("NFD")
// biome-ignore lint/suspicious/noMisleadingCharacterClass: <explanation>
.replace(/[\u0300-\u036f]/g, "")
.replace(/\s+/g, " ")
.toLowerCase(),

View file

@ -9,22 +9,35 @@
* parametros = {nome:"José"}
*/
export class TipagemRotas<T extends { [q: string]: string | undefined }> {
export class TipagemRotas<T extends { [q: string]: any }> {
_partesCaminho: string[] = []
_acaoIr?: (endereco: string) => undefined
rotulo: string | undefined
/** Ao criar novo obijeto de tipagem de rota é necessário passar o caminho parcial
** export const mCaminho = new TipagemRotas<{q:string}>("aplicacao","funcionalidade")
*/
constructor(...caminhos: string[]) {
caminhos.forEach((caminho) => {
String(caminho)
.split("/")
.forEach((parte) => {
if (parte) {
this._partesCaminho.push(parte)
}
})
})
constructor({
caminho,
acaoIr,
rotulo,
}: {
caminho: (string | null | undefined)[] | string
acaoIr?: undefined | ((endereco: string) => undefined)
/** Rotulo da página
* Inicio | Controle
*/
rotulo?: string
}) {
this._acaoIr = acaoIr
this._partesCaminho = (Array.isArray(caminho) ? caminho : [caminho])
.filter(Boolean)
.map((a) => String(a))
.flatMap((a) => a.split("/"))
.filter(Boolean)
this.rotulo = rotulo
}
/** Retorna o caminho completo da rota
@ -32,11 +45,7 @@ export class TipagemRotas<T extends { [q: string]: string | undefined }> {
** "/caminho"
*/
get caminho() {
let ret = `/${this._partesCaminho.join("/")}`
ret = ret.replace(/\/+/g, "/")
if (ret.length > 1 && ret.endsWith("/")) {
ret = ret.slice(0, -1)
}
const ret = `/${this._partesCaminho.join("/")}`
return ret
}
/** Define o caminho completo da rota
@ -65,10 +74,7 @@ export class TipagemRotas<T extends { [q: string]: string | undefined }> {
const queryKeys = Object.entries(query)
for (const [key, value] of queryKeys) {
url.searchParams.set(
String(key),
value === undefined || value === null ? "" : value,
)
url.searchParams.set(String(key), JSON.stringify(value))
}
url.hash = ""
@ -86,8 +92,12 @@ export class TipagemRotas<T extends { [q: string]: string | undefined }> {
** window.location.href = "http://localhost:3000/caminho?q=query"
*/
ir(query: T) {
if (typeof window != "undefined") {
window.location.href = this.endereco(query)
if (this._acaoIr) {
this._acaoIr(this.endereco({ ...query }))
} else {
if (typeof window != "undefined") {
window.location.href = this.endereco({ ...query })
}
}
}
@ -96,12 +106,16 @@ export class TipagemRotas<T extends { [q: string]: string | undefined }> {
** {q:"query"}
*/
get parametros() {
const url = new URL(
typeof window !== "undefined" ? window.location.href : "http://localhost",
)
parametros(urlEntrada?: string) {
const url = urlEntrada
? new URL(urlEntrada)
: new URL(
typeof window !== "undefined"
? window.location.href
: "http://localhost",
)
const query = url.searchParams
const queryObj = Object.fromEntries(query.entries())
let queryObj = Object.fromEntries(query.entries())
// pegar hash
const hash = url.hash
@ -109,7 +123,15 @@ export class TipagemRotas<T extends { [q: string]: string | undefined }> {
const hashObj = Object.fromEntries(
new URLSearchParams(hash.slice(1)).entries(),
)
return { ...queryObj, ...hashObj } as T
queryObj = { ...queryObj, ...hashObj } as T
}
for (const chave in queryObj) {
try {
queryObj[chave] = JSON.parse(queryObj[chave])
} catch {
console.log(`[${chave}|${queryObj[chave]}] não é um json válido.`)
}
}
return queryObj as Partial<T>

View file

@ -1,15 +1,53 @@
//Gerar uma uuid V4
const letras = "0123456789abcdef".split("")
export const uuid = () => {
letras.sort(() => Math.random() - 0.5)
const modelo = "xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx".split("")
const retorno = modelo
.map((letra) =>
letra === "x"
? letras[((1000 * Math.random()) | 0) % letras.length]
: letra,
)
.join("")
import { NIL, v3, v4 } from "uuid"
/**
* Valida se uma string é um UUID válido (qualquer versão).
*
* @param valor - A string que será validada.
* @returns booleano indicando se é um UUID válido.
*/
export const erUuid =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
export const validarUuid = (uuid: string | number | undefined | null) => {
const retorno = erUuid.test(String(uuid || ""))
return retorno
}
/**
* Gera um UUID determinístico (versão 3) com base em uma chave e um grupo (namespace).
*
* - Usa o algoritmo MD5 (RFC 4122).
* - Sempre retorna o mesmo UUID para a mesma combinação chave + grupo.
* - Caso o grupo não seja informado, usa o UUID "nil" como namespace.
*
* @param chave - Qualquer valor que será convertido em string para gerar o UUID (ex: número, string ou objeto).
* @param grupo - Opcional. Namespace para separar domínios diferentes de UUIDs.
* @returns UUID v3 (determinístico)
*/
export const uuidV3 = (chave: any, grupo?: any): string => {
return v3(
// Converte a chave para string (de forma segura)
typeof chave === "string"
? chave
: typeof chave === "number"
? String(chave)
: JSON.stringify(chave),
// Se um grupo foi fornecido, gera um UUID v3 recursivamente com base nele, senão usa NIL
grupo ? uuidV3(grupo) : NIL,
)
}
/**
* Gera um UUID v4 (aleatório, não determinístico).
*
* - Usado quando unicidade é necessária, mas não se exige que seja previsível.
*/
export const uuidV4 = v4
/**
* @deprecated Esta variável será descontinuada em versões futuras.
* Use a função `uuidV4()` diretamente.
*/
export const uuid = uuidV4