_comuns/dist-back/uuid.js

62 lines
2.1 KiB
JavaScript

"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var uuid_exports = {};
__export(uuid_exports, {
erUuid: () => erUuid,
uuid: () => uuid,
uuidV3: () => uuidV3,
uuidV4: () => uuidV4,
validarUuid: () => validarUuid
});
module.exports = __toCommonJS(uuid_exports);
var import_node_crypto = require("node:crypto");
const erUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const validarUuid = (uuid2) => {
const retorno = erUuid.test(String(uuid2 || ""));
return retorno;
};
const NIL_UUID = "00000000-0000-0000-0000-000000000000";
const formatarUuidV3 = (hash) => {
const chars = hash.slice(0, 32);
return [
chars.slice(0, 8),
chars.slice(8, 12),
`3${chars.slice(13, 16)}`,
// version 3
`${(Number.parseInt(chars[16], 16) & 3 | 8).toString(16)}${chars.slice(17, 20)}`,
// variant
chars.slice(20, 32)
].join("-");
};
const uuidV3 = (chave, grupo) => {
const namespace = grupo ? uuidV3(grupo) : NIL_UUID;
const input = typeof chave === "string" ? chave : typeof chave === "number" ? String(chave) : JSON.stringify(chave);
const hash = (0, import_node_crypto.createHash)("md5").update(namespace + input).digest("hex");
return formatarUuidV3(hash);
};
const uuidV4 = () => (0, import_node_crypto.randomUUID)();
const uuid = uuidV4;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
erUuid,
uuid,
uuidV3,
uuidV4,
validarUuid
});