Source: common/util/id_generator.js

/**
 * Generates IDs.
 * @name IDGenerator
 * @namespace
 * @private
 */
define(["lib/underscore"], function(_) {
    "use strict";

    var uuidFormat = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";

    var traceIdFormat = "xxxxxxxxxxxxxxxxxxxxxy==";

    // return true iff the given character is a valid lowercase hex caracter
    var isHexC = function(c) {
        return (/^[0-9a-f]$/).test(c);
    };

    var base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var base64TwoBits = "AQgw";

    var IDGenerator = /** @lends IDGenerator */ {
        /**
         * Returns a random UUID.
         * @return {String}
         */
        uuid: function() {
            // from http://stackoverflow.com/a/2117523
            return uuidFormat.replace(/[xy]/g, function(c) {
                var r = Math.random()*16|0, v = c === "x" ? r : (r&0x3|0x8);
                return v.toString(16);
            });
        },

        /**
         * Return true iff the given string is a uuid.
         */
        isUUID: function(str) {
            if(str.length !== uuidFormat.length) {
                return false;
            }

            return _.every(uuidFormat, function(c, i) {
                var testC = str[i];
                if(c === "x" || c === "y") {
                    return isHexC(testC);
                }
                else if(c === "-") {
                    return (testC === "-");
                }
                else if(c === "4") {
                    return (testC === "4");
                }
            });
        },

        /**
         * Generates a random trace id.
         * @return {String} the trace id.
         */
        traceId: function() {
            return traceIdFormat.replace(/x/g, function() {
                return base64[Math.random()*64|0];
            }).replace(/y/g, function() {
                return base64TwoBits[Math.random()*4|0];
            });
        }
    };

    return IDGenerator;
});