Uuid.js 8.1 KB

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