utils.ts
TLDR
This file contains a class Utils
that has a static method createGuid()
. The method generates a random GUID (Globally Unique Identifier) using both the crypto library and Math.random method.
Classes
Utils
The Utils
class provides utility functions for generating a GUID.
Methods
createGuid()
This method generates a random GUID (Globally Unique Identifier) using both the crypto library and Math.random method. It returns a string in the format xxxxxxxx-xxxx-4xxx-8xxx-xxxxxxxxxxxx
.
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
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 }