source: Dev/branches/rest-dojo-ui/client/dojox/uuid/generateRandomUuid.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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