All Downloads are FREE. Search and download functionalities are using the official Maven repository.

utils.id.ts Maven / Gradle / Ivy

The newest version!
// Generate unique IDs for use as pseudo-private/protected names.
// Similar in concept to
// .
//
// The goals of this function are twofold:
//
// * Provide a way to generate a string guaranteed to be unique when compared
//   to other strings generated by this function.
// * Make the string complex enough that it is highly unlikely to be
//   accidentally duplicated by hand (this is key if you're using `ID`
//   as a private/protected name on an object).
//
// Use:
//
//     var privateName = ID();
//     var o = { 'public': 'foo' };
//     o[privateName] = 'bar';
export function id(): string {
    // Math.random should be unique because of its seeding algorithm.
    // Convert it to base 36 (numbers + letters), and grab the first 9 characters
    // after the decimal.
    return (
        `_${
            Math.random()
                .toString(36)
                .substr(2, 9)}`
    )
}

export function generateKey(containerKey: string, columnId: string) {
    return `${containerKey}.${columnId}`
}

export function guid(): string {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1)
    }

    return (
        `${s4() + s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy