main

mattermost/focalboard

Last updated at: 29/12/2023 09:47

utils.ts

TLDR

The provided file is a TypeScript module that exports a class called Utils, which has one static method called createGuid(). The createGuid() method generates a random GUID in the format "xxxxxxxx-xxxx-4xxx-8xxx-xxxxxxxxxxxx".

Classes

Utils

The Utils class provides utility methods for generating random GUIDs.

Methods

createGuid()

The createGuid() method generates a random GUID. It uses the crypto module to generate random bytes if available, otherwise falls back to using Math.random(). The generated GUID follows 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 }