2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.uuid.generateRandomUuid"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.uuid.generateRandomUuid"] = true;
|
|
|
3 |
dojo.provide("dojox.uuid.generateRandomUuid");
|
|
|
4 |
|
|
|
5 |
dojox.uuid.generateRandomUuid = function(){
|
|
|
6 |
// summary:
|
|
|
7 |
// This function generates random UUIDs, meaning "version 4" UUIDs.
|
|
|
8 |
// description:
|
|
|
9 |
// A typical generated value would be something like this:
|
|
|
10 |
// "3b12f1df-5232-4804-897e-917bf397618a"
|
|
|
11 |
//
|
|
|
12 |
// For more information about random UUIDs, see sections 4.4 and
|
|
|
13 |
// 4.5 of RFC 4122: http://tools.ietf.org/html/rfc4122#section-4.4
|
|
|
14 |
//
|
|
|
15 |
// This generator function is designed to be small and fast,
|
|
|
16 |
// but not necessarily good.
|
|
|
17 |
//
|
|
|
18 |
// Small: This generator has a small footprint. Once comments are
|
|
|
19 |
// stripped, it's only about 25 lines of code, and it doesn't
|
|
|
20 |
// dojo.require() any other modules.
|
|
|
21 |
//
|
|
|
22 |
// Fast: This generator can generate lots of new UUIDs fairly quickly
|
|
|
23 |
// (at least, more quickly than the other dojo UUID generators).
|
|
|
24 |
//
|
|
|
25 |
// Not necessarily good: We use Math.random() as our source
|
|
|
26 |
// of randomness, which may or may not provide much randomness.
|
|
|
27 |
// examples:
|
|
|
28 |
// var string = dojox.uuid.generateRandomUuid();
|
|
|
29 |
var HEX_RADIX = 16;
|
|
|
30 |
|
|
|
31 |
function _generateRandomEightCharacterHexString(){
|
|
|
32 |
// Make random32bitNumber be a randomly generated floating point number
|
|
|
33 |
// between 0 and (4,294,967,296 - 1), inclusive.
|
|
|
34 |
var random32bitNumber = Math.floor( (Math.random() % 1) * Math.pow(2, 32) );
|
|
|
35 |
var eightCharacterHexString = random32bitNumber.toString(HEX_RADIX);
|
|
|
36 |
while(eightCharacterHexString.length < 8){
|
|
|
37 |
eightCharacterHexString = "0" + eightCharacterHexString;
|
|
|
38 |
}
|
|
|
39 |
return eightCharacterHexString; // for example: "3B12F1DF"
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
var hyphen = "-";
|
|
|
43 |
var versionCodeForRandomlyGeneratedUuids = "4"; // 8 == binary2hex("0100")
|
|
|
44 |
var variantCodeForDCEUuids = "8"; // 8 == binary2hex("1000")
|
|
|
45 |
var a = _generateRandomEightCharacterHexString();
|
|
|
46 |
var b = _generateRandomEightCharacterHexString();
|
|
|
47 |
b = b.substring(0, 4) + hyphen + versionCodeForRandomlyGeneratedUuids + b.substring(5, 8);
|
|
|
48 |
var c = _generateRandomEightCharacterHexString();
|
|
|
49 |
c = variantCodeForDCEUuids + c.substring(1, 4) + hyphen + c.substring(4, 8);
|
|
|
50 |
var d = _generateRandomEightCharacterHexString();
|
|
|
51 |
var returnValue = a + hyphen + b + hyphen + c + d;
|
|
|
52 |
returnValue = returnValue.toLowerCase();
|
|
|
53 |
return returnValue; // String
|
|
|
54 |
};
|
|
|
55 |
|
|
|
56 |
}
|