瀏覽代碼

Simplifies code.

master
Boris Grozev 9 年之前
父節點
當前提交
005cc4b27a
共有 2 個檔案被更改,包括 21 行新增25 行删除
  1. 18
    23
      modules/util/RandomUtil.js
  2. 3
    2
      modules/xmpp/xmpp.js

+ 18
- 23
modules/util/RandomUtil.js 查看文件

@@ -3,6 +3,12 @@
3 3
  */
4 4
 var ALPHANUM = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
5 5
 
6
+/**
7
+ * Hexadecimal digits.
8
+ * @const
9
+ */
10
+var HEX_DIGITS = '0123456789abcdef';
11
+
6 12
 /**
7 13
  * Generates random int within the range [min, max]
8 14
  * @param min the minimum value for the generated number
@@ -37,38 +43,27 @@ function randomAlphanumStr(length) {
37 43
     return result;
38 44
 }
39 45
 
40
-/**
41
- * Generates random hex number within the range [min, max]
42
- * @param min the minimum value for the generated number
43
- * @param max the maximum value for the generated number
44
- * @returns random hex number
45
- */
46
-function rangeRandomHex(min, max)
47
-{
48
-    return randomInt(min, max).toString(16);
49
-}
50
-
51 46
 /**
52 47
  * Exported interface.
53 48
  */
54 49
 var RandomUtil = {
55 50
     /**
56
-     * Generates hex number with length 4
57
-     */
58
-    random4digitsHex: function () {
59
-        return rangeRandomHex(0x1000, 0xFFFF);
60
-    },
61
-    /**
62
-     * Generates hex number with length 8
51
+     * Returns a random hex digit.
52
+     * @returns {*}
63 53
      */
64
-    random8digitsHex: function () {
65
-        return rangeRandomHex(0x10000000, 0xFFFFFFFF);
54
+    randomHexDigit: function() {
55
+        return randomElement(HEX_DIGITS);
66 56
     },
67 57
     /**
68
-     * Generates hex number with length 12
58
+     * Returns a random string of hex digits with length 'len'.
59
+     * @param len the length.
69 60
      */
70
-    random12digitsHex: function () {
71
-        return rangeRandomHex(0x100000000000, 0xFFFFFFFFFFFF);
61
+    randomHexString: function (len) {
62
+        var ret = '';
63
+        while (len--) {
64
+            ret += this.randomHexDigit();
65
+        }
66
+        return ret;
72 67
     }
73 68
 };
74 69
 

+ 3
- 2
modules/xmpp/xmpp.js 查看文件

@@ -23,8 +23,9 @@ var authenticatedUser = false;
23 23
  * @returns {string}
24 24
  */
25 25
 function generateUserName() {
26
-    return RandomUtil.random8digitsHex() + "-" + RandomUtil.random4digitsHex() + "-" +
27
-        RandomUtil.random4digitsHex() + "-" + RandomUtil.random8digitsHex();
26
+    return RandomUtil.randomHexString(8) + "-" + RandomUtil.randomHexString(4)
27
+        + "-" + RandomUtil.randomHexString(4) + "-"
28
+        + RandomUtil.randomHexString(8);
28 29
 }
29 30
 
30 31
 function connect(jid, password) {

Loading…
取消
儲存