UNPKG

@polgubau/utils

Version:

A collection of utility functions for TypeScript

17 lines 682 B
// src/functions/generate-uuid/generate-uuid.ts var generateUUID = (limit) => { if (limit === 0) { return ""; } const randomBytes = new Uint8Array(16); crypto.getRandomValues(randomBytes); randomBytes[6] = randomBytes[6] & 15 | 64; randomBytes[8] = randomBytes[8] & 63 | 128; const hexBytes = Array.from(randomBytes, (byte) => byte.toString(16).padStart(2, "0")); const uuid = `${hexBytes.slice(0, 4).join("")}-${hexBytes.slice(4, 6).join("")}-${hexBytes.slice(6, 8).join("")}-${hexBytes.slice(8, 10).join("")}-${hexBytes.slice(10).join("")}`; return limit ? uuid.slice(0, limit) : uuid; }; export { generateUUID }; //# sourceMappingURL=generate-uuid.mjs.map