Browse Source

Simplifies code.

master
Boris Grozev 10 years ago
parent
commit
005cc4b27a
2 changed files with 21 additions and 25 deletions
  1. 18
    23
      modules/util/RandomUtil.js
  2. 3
    2
      modules/xmpp/xmpp.js

+ 18
- 23
modules/util/RandomUtil.js View File

3
  */
3
  */
4
 var ALPHANUM = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
4
 var ALPHANUM = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
5
 
5
 
6
+/**
7
+ * Hexadecimal digits.
8
+ * @const
9
+ */
10
+var HEX_DIGITS = '0123456789abcdef';
11
+
6
 /**
12
 /**
7
  * Generates random int within the range [min, max]
13
  * Generates random int within the range [min, max]
8
  * @param min the minimum value for the generated number
14
  * @param min the minimum value for the generated number
37
     return result;
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
  * Exported interface.
47
  * Exported interface.
53
  */
48
  */
54
 var RandomUtil = {
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 View File

23
  * @returns {string}
23
  * @returns {string}
24
  */
24
  */
25
 function generateUserName() {
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
 function connect(jid, password) {
31
 function connect(jid, password) {

Loading…
Cancel
Save