@typespec/ts-http-runtime
Version:
Isomorphic client library for making HTTP requests in node.js and browser.
45 lines • 1.47 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Generated Universally Unique Identifier
*
* @returns RFC4122 v4 UUID.
*/
function generateUUID() {
let uuid = "";
for (let i = 0; i < 32; i++) {
// Generate a random number between 0 and 15
const randomNumber = Math.floor(Math.random() * 16);
// Set the UUID version to 4 in the 13th position
if (i === 12) {
uuid += "4";
}
else if (i === 16) {
// Set the UUID variant to "10" in the 17th position
uuid += ((randomNumber & 0x3) | 0x8).toString(16);
}
else {
// Add a random hexadecimal digit to the UUID string
uuid += randomNumber.toString(16);
}
// Add hyphens to the UUID string at the appropriate positions
if (i === 7 || i === 11 || i === 15 || i === 19) {
uuid += "-";
}
}
return uuid;
}
// NOTE: globalThis.crypto.randomUUID is not available on React Native engines
// (e.g. Hermes/JSC), so fall back to a manual implementation when it is missing.
const uuidFunction = typeof globalThis?.crypto?.randomUUID === "function"
? globalThis.crypto.randomUUID.bind(globalThis.crypto)
: generateUUID;
/**
* Generated Universally Unique Identifier
*
* @returns RFC4122 v4 UUID.
*/
export function randomUUID() {
return uuidFunction();
}
//# sourceMappingURL=uuidUtils-react-native.mjs.map