utils.ts
TLDR
This file contains a class named Utils
with a static method createGuid()
. The createGuid()
method generates a random GUID (Globally Unique Identifier).
Classes
Utils
The Utils
class provides utility methods. Currently, it contains one static method:
createGuid()
This static method generates a random GUID (Globally Unique Identifier) string. It uses the crypto
module to generate random bytes and then replaces placeholders in a UUID template with the random digits. The generated GUID follows the pattern xxxxxxxx-xxxx-4xxx-8xxx-xxxxxxxxxxxx
, where x
represents a random hexadecimal digit.
import * as crypto from 'crypto'
class Utils {
static createGuid(): string {
function randomDigit() {
if (crypto && crypto.randomBytes) {
const rands = crypto.randomBytes(1)
return (rands[0] % 16).toString(16)
}
return (Math.floor((Math.random() * 16))).toString(16)
}
return 'xxxxxxxx-xxxx-4xxx-8xxx-xxxxxxxxxxxx'.replace(/x/g, randomDigit)
}
}
export { Utils }