Uuid.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. define("dojox/uuid/Uuid", ['dojo/_base/lang', './_base'], function(dojo, uuid){
  2. dojox.uuid.Uuid = function(/*String?*/ input){
  3. // summary:
  4. // This is the constructor for the Uuid class. The Uuid class offers
  5. // methods for inspecting existing UUIDs.
  6. // input: A 36-character string that conforms to the UUID spec.
  7. // examples:
  8. // var uuid;
  9. // uuid = new dojox.uuid.Uuid("3b12f1df-5232-4804-897e-917bf397618a");
  10. // uuid = new dojox.uuid.Uuid(); // "00000000-0000-0000-0000-000000000000"
  11. // uuid = new dojox.uuid.Uuid(dojox.uuid.generateRandomUuid());
  12. // uuid = new dojox.uuid.Uuid(dojox.uuid.generateTimeBasedUuid());
  13. // dojox.uuid.Uuid.setGenerator(dojox.uuid.generateRandomUuid);
  14. // uuid = new dojox.uuid.Uuid();
  15. // dojox.uuid.assert(!uuid.isEqual(dojox.uuid.NIL_UUID));
  16. this._uuidString = dojox.uuid.NIL_UUID;
  17. if(input){
  18. dojox.uuid.assert(dojo.isString(input));
  19. this._uuidString = input.toLowerCase();
  20. dojox.uuid.assert(this.isValid());
  21. }else{
  22. var ourGenerator = dojox.uuid.Uuid.getGenerator();
  23. if(ourGenerator){
  24. this._uuidString = ourGenerator();
  25. dojox.uuid.assert(this.isValid());
  26. }
  27. }
  28. };
  29. dojox.uuid.Uuid.compare = function(/*dojox.uuid.Uuid*/ uuidOne, /*dojox.uuid.Uuid*/ uuidTwo){
  30. // summary:
  31. // Given two UUIDs to compare, this method returns 0, 1, or -1.
  32. // description:
  33. // This method is designed to be used by sorting routines, like the
  34. // JavaScript built-in Array sort() method. This implementation is
  35. // intended to match the sample implementation in IETF RFC 4122:
  36. // http://www.ietf.org/rfc/rfc4122.txt
  37. // uuidOne: Any object that has toString() method that returns a 36-character string that conforms to the UUID spec.
  38. // uuidTwo: Any object that has toString() method that returns a 36-character string that conforms to the UUID spec.
  39. // examples:
  40. // var uuid;
  41. // var generator = dojox.uuid.TimeBasedGenerator;
  42. // var a = new dojox.uuid.Uuid(generator);
  43. // var b = new dojox.uuid.Uuid(generator);
  44. // var c = new dojox.uuid.Uuid(generator);
  45. // var array = new Array(a, b, c);
  46. // array.sort(dojox.uuid.Uuid.compare);
  47. var uuidStringOne = uuidOne.toString();
  48. var uuidStringTwo = uuidTwo.toString();
  49. if (uuidStringOne > uuidStringTwo) return 1; // integer
  50. if (uuidStringOne < uuidStringTwo) return -1; // integer
  51. return 0; // integer (either 0, 1, or -1)
  52. };
  53. dojox.uuid.Uuid.setGenerator = function(/*Function?*/ generator){
  54. // summary:
  55. // Sets the default generator, which will be used by the
  56. // "new dojox.uuid.Uuid()" constructor if no parameters
  57. // are passed in.
  58. // generator: A UUID generator function, such as dojox.uuid.generateTimeBasedUuid.
  59. dojox.uuid.assert(!generator || dojo.isFunction(generator));
  60. dojox.uuid.Uuid._ourGenerator = generator;
  61. };
  62. dojox.uuid.Uuid.getGenerator = function(){
  63. // summary:
  64. // Returns the default generator. See setGenerator().
  65. return dojox.uuid.Uuid._ourGenerator; // generator (A UUID generator, such as dojox.uuid.TimeBasedGenerator).
  66. };
  67. dojox.uuid.Uuid.prototype.toString = function(){
  68. // summary:
  69. // This method returns a standard 36-character string representing
  70. // the UUID, such as "3b12f1df-5232-4804-897e-917bf397618a".
  71. return this._uuidString; // string
  72. };
  73. dojox.uuid.Uuid.prototype.compare = function(/*dojox.uuid.Uuid*/ otherUuid){
  74. // summary:
  75. // Compares this UUID to another UUID, and returns 0, 1, or -1.
  76. // description:
  77. // This implementation is intended to match the sample implementation
  78. // in IETF RFC 4122: http://www.ietf.org/rfc/rfc4122.txt
  79. // otherUuid: Any object that has toString() method that returns a 36-character string that conforms to the UUID spec.
  80. return dojox.uuid.Uuid.compare(this, otherUuid); // integer (either 0, 1, or -1)
  81. };
  82. dojox.uuid.Uuid.prototype.isEqual = function(/*dojox.uuid.Uuid*/ otherUuid){
  83. // summary:
  84. // Returns true if this UUID is equal to the otherUuid, or false otherwise.
  85. // otherUuid: Any object that has toString() method that returns a 36-character string that conforms to the UUID spec.
  86. return (this.compare(otherUuid) == 0); // boolean
  87. };
  88. dojox.uuid.Uuid.prototype.isValid = function(){
  89. // summary:
  90. // Returns true if the UUID was initialized with a valid value.
  91. return dojox.uuid.isValid(this);
  92. };
  93. dojox.uuid.Uuid.prototype.getVariant = function(){
  94. // summary:
  95. // Returns a variant code that indicates what type of UUID this is.
  96. // Returns one of the enumerated dojox.uuid.variant values.
  97. // example:
  98. // var uuid = new dojox.uuid.Uuid("3b12f1df-5232-4804-897e-917bf397618a");
  99. // var variant = uuid.getVariant();
  100. // dojox.uuid.assert(variant == dojox.uuid.variant.DCE);
  101. // example:
  102. // "3b12f1df-5232-4804-897e-917bf397618a"
  103. // ^
  104. // |
  105. // (variant "10__" == DCE)
  106. return dojox.uuid.getVariant(this);
  107. };
  108. dojox.uuid.Uuid.prototype.getVersion = function(){
  109. // summary:
  110. // Returns a version number that indicates what type of UUID this is.
  111. // Returns one of the enumerated dojox.uuid.version values.
  112. // example:
  113. // var uuid = new dojox.uuid.Uuid("b4308fb0-86cd-11da-a72b-0800200c9a66");
  114. // var version = uuid.getVersion();
  115. // dojox.uuid.assert(version == dojox.uuid.version.TIME_BASED);
  116. // exceptions:
  117. // Throws an Error if this is not a DCE Variant UUID.
  118. if(!this._versionNumber){
  119. this._versionNumber = dojox.uuid.getVersion(this);
  120. }
  121. return this._versionNumber; // dojox.uuid.version
  122. };
  123. dojox.uuid.Uuid.prototype.getNode = function(){
  124. // summary:
  125. // If this is a version 1 UUID (a time-based UUID), getNode() returns a
  126. // 12-character string with the "node" or "pseudonode" portion of the UUID,
  127. // which is the rightmost 12 characters.
  128. // exceptions:
  129. // Throws an Error if this is not a version 1 UUID.
  130. if (!this._nodeString) {
  131. this._nodeString = dojox.uuid.getNode(this);
  132. }
  133. return this._nodeString; // String (a 12-character string, which will look something like "917bf397618a")
  134. };
  135. dojox.uuid.Uuid.prototype.getTimestamp = function(/*String?*/ returnType){
  136. // summary:
  137. // If this is a version 1 UUID (a time-based UUID), this method returns
  138. // the timestamp value encoded in the UUID. The caller can ask for the
  139. // timestamp to be returned either as a JavaScript Date object or as a
  140. // 15-character string of hex digits.
  141. // returnType: Any of these five values: "string", String, "hex", "date", Date
  142. // returns:
  143. // Returns the timestamp value as a JavaScript Date object or a 15-character string of hex digits.
  144. // examples:
  145. // var uuid = new dojox.uuid.Uuid("b4308fb0-86cd-11da-a72b-0800200c9a66");
  146. // var date, string, hexString;
  147. // date = uuid.getTimestamp(); // returns a JavaScript Date
  148. // date = uuid.getTimestamp(Date); //
  149. // string = uuid.getTimestamp(String); // "Mon, 16 Jan 2006 20:21:41 GMT"
  150. // hexString = uuid.getTimestamp("hex"); // "1da86cdb4308fb0"
  151. // exceptions:
  152. // Throws an Error if this is not a version 1 UUID.
  153. if(!returnType){returnType = null};
  154. switch(returnType){
  155. case "string":
  156. case String:
  157. return this.getTimestamp(Date).toUTCString(); // String (e.g. "Mon, 16 Jan 2006 20:21:41 GMT")
  158. break;
  159. case "hex":
  160. // Return a 15-character string of hex digits containing the
  161. // timestamp for this UUID, with the high-order bits first.
  162. if (!this._timestampAsHexString) {
  163. this._timestampAsHexString = dojox.uuid.getTimestamp(this, "hex");
  164. }
  165. return this._timestampAsHexString; // String (e.g. "1da86cdb4308fb0")
  166. break;
  167. case null: // no returnType was specified, so default to Date
  168. case "date":
  169. case Date:
  170. // Return a JavaScript Date object.
  171. if (!this._timestampAsDate) {
  172. this._timestampAsDate = dojox.uuid.getTimestamp(this, Date);
  173. }
  174. return this._timestampAsDate; // Date
  175. break;
  176. default:
  177. // we got passed something other than a valid returnType
  178. dojox.uuid.assert(false, "The getTimestamp() method dojox.uuid.Uuid was passed a bogus returnType: " + returnType);
  179. break;
  180. }
  181. };
  182. return dojox.uuid.Uuid;
  183. });