js generated guid

A globally unique identifier (GUID, Globally Unique Identifier), also referred to UUID (Universally Unique IDentifier).

It is a GUID generated by the algorithm is 128 bits binary numeric identifier. GUID format is "xxxxxxxxxxxx-xxxxxxxx-xxxxxxxxxxxx", where x is a 32-bit hexadecimal number within the range 0-9 or af. In the ideal case, any computers and computer clusters will not generate two identical GUID.

The following is a method of generating the guid:

/**
 * 生成guid
 */
export const createguid = () => {
    var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
    var chars = CHARS,
        uuid = [],
        i
    // rfc4122, version 4 form
    var r
    // rfc4122 requires these characters
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
    uuid[14] = '4'
    for (i = 0; i < 36; i++) {
        if (!uuid[i]) {
            r = 0 | (Math.random() * 16)
            uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r]
        }
    }

    var ret = uuid.join('')
    return ret
}

 

Guess you like

Origin www.cnblogs.com/wxcbg/p/10945141.html