uuidUtils.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. var Uuid = (typeof CC_EDITOR !== 'undefined' ? CC_EDITOR : true) && require('node-uuid');
  2. var Base64KeyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  3. var AsciiTo64 = new Array(128);
  4. for (var i = 0; i < 128; ++i) { AsciiTo64[i] = 0; }
  5. for (i = 0; i < 64; ++i) { AsciiTo64[Base64KeyChars.charCodeAt(i)] = i; }
  6. var Reg_Dash = /-/g;
  7. var Reg_Uuid = /^[0-9a-fA-F-]{36}$/;
  8. var Reg_NormalizedUuid = /^[0-9a-fA-F]{32}$/;
  9. var Reg_CompressedUuid = /^[0-9a-zA-Z+/]{22,23}$/;
  10. var UuidUtils = {
  11. // 加了这个标记后,字符串就不可能会是 uuid 了。
  12. NonUuidMark: '.',
  13. // 压缩后的 uuid 可以减小保存时的尺寸,但不能做为文件名(因为无法区分大小写并且包含非法字符)。
  14. // 默认将 uuid 的后面 27 位压缩成 18 位,前 5 位保留下来,方便调试。
  15. // fc991dd7-0033-4b80-9d41-c8a86a702e59 -> fc9913XADNLgJ1ByKhqcC5Z
  16. // 如果启用 min 则将 uuid 的后面 30 位压缩成 20 位,前 2 位保留不变。
  17. // fc991dd7-0033-4b80-9d41-c8a86a702e59 -> fcmR3XADNLgJ1ByKhqcC5Z
  18. /*
  19. * @param {Boolean} [min=false]
  20. */
  21. compressUuid: function (uuid, min) {
  22. if (Reg_Uuid.test(uuid)) {
  23. uuid = uuid.replace(Reg_Dash, '');
  24. }
  25. else if (!Reg_NormalizedUuid.test(uuid)) {
  26. return uuid;
  27. }
  28. var reserved = (min === true) ? 2 : 5;
  29. return UuidUtils.compressHex(uuid, reserved);
  30. },
  31. compressHex: function (hexString, reservedHeadLength) {
  32. var length = hexString.length;
  33. var i;
  34. if (typeof reservedHeadLength !== 'undefined') {
  35. i = reservedHeadLength;
  36. }
  37. else {
  38. i = length % 3;
  39. }
  40. var head = hexString.slice(0, i);
  41. var base64Chars = [];
  42. while (i < length) {
  43. var hexVal1 = parseInt(hexString[i], 16);
  44. var hexVal2 = parseInt(hexString[i + 1], 16);
  45. var hexVal3 = parseInt(hexString[i + 2], 16);
  46. base64Chars.push(Base64KeyChars[(hexVal1 << 2) | (hexVal2 >> 2)]);
  47. base64Chars.push(Base64KeyChars[((hexVal2 & 3) << 4) | hexVal3]);
  48. i += 3;
  49. }
  50. return head + base64Chars.join('');
  51. },
  52. //解压缩
  53. decompressUuid: function (str) {
  54. if (str.length === 23) {
  55. // decode base64
  56. var hexChars = [];
  57. for (var i = 5; i < 23; i += 2) {
  58. var lhs = AsciiTo64[str.charCodeAt(i)];
  59. var rhs = AsciiTo64[str.charCodeAt(i + 1)];
  60. hexChars.push((lhs >> 2).toString(16));
  61. hexChars.push((((lhs & 3) << 2) | rhs >> 4).toString(16));
  62. hexChars.push((rhs & 0xF).toString(16));
  63. }
  64. //
  65. str = str.slice(0, 5) + hexChars.join('');
  66. }
  67. else if (str.length === 22) {
  68. // decode base64
  69. var hexChars = [];
  70. for (var i = 2; i < 22; i += 2) {
  71. var lhs = AsciiTo64[str.charCodeAt(i)];
  72. var rhs = AsciiTo64[str.charCodeAt(i + 1)];
  73. hexChars.push((lhs >> 2).toString(16));
  74. hexChars.push((((lhs & 3) << 2) | rhs >> 4).toString(16));
  75. hexChars.push((rhs & 0xF).toString(16));
  76. }
  77. //
  78. str = str.slice(0, 2) + hexChars.join('');
  79. }
  80. return [str.slice(0, 8), str.slice(8, 12), str.slice(12, 16), str.slice(16, 20), str.slice(20)].join('-');
  81. },
  82. isUuid: function (str) {
  83. if (typeof str == 'string') {
  84. return Reg_CompressedUuid.test(str) || Reg_NormalizedUuid.test(str) || Reg_Uuid.test(str);
  85. } else {
  86. return false;
  87. }
  88. },
  89. uuid: function () {
  90. var uuid = Uuid.v4();
  91. return UuidUtils.compressUuid(uuid, true);
  92. },
  93. /**
  94. * 生成激活码
  95. * UUID.v1 基于时间戳生成(time based)
  96. * UUID.v4 随机生成(random), 有一定几率重复
  97. * cocoscreator 使用的是v4 uuid生成器
  98. */
  99. uuidv4: function () {
  100. var uuid = Uuid.v4();
  101. return uuid;
  102. },
  103. uuidv1: function () {
  104. var uuid = Uuid.v1();
  105. return uuid;
  106. }
  107. };
  108. module.exports = UuidUtils;