Simplifies code.

This commit is contained in:
Boris Grozev 2015-11-05 18:51:23 -06:00
parent a2c37fa3f6
commit 005cc4b27a
2 changed files with 21 additions and 25 deletions

View File

@ -3,6 +3,12 @@
*/
var ALPHANUM = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
/**
* Hexadecimal digits.
* @const
*/
var HEX_DIGITS = '0123456789abcdef';
/**
* Generates random int within the range [min, max]
* @param min the minimum value for the generated number
@ -37,38 +43,27 @@ function randomAlphanumStr(length) {
return result;
}
/**
* Generates random hex number within the range [min, max]
* @param min the minimum value for the generated number
* @param max the maximum value for the generated number
* @returns random hex number
*/
function rangeRandomHex(min, max)
{
return randomInt(min, max).toString(16);
}
/**
* Exported interface.
*/
var RandomUtil = {
/**
* Generates hex number with length 4
* Returns a random hex digit.
* @returns {*}
*/
random4digitsHex: function () {
return rangeRandomHex(0x1000, 0xFFFF);
randomHexDigit: function() {
return randomElement(HEX_DIGITS);
},
/**
* Generates hex number with length 8
* Returns a random string of hex digits with length 'len'.
* @param len the length.
*/
random8digitsHex: function () {
return rangeRandomHex(0x10000000, 0xFFFFFFFF);
},
/**
* Generates hex number with length 12
*/
random12digitsHex: function () {
return rangeRandomHex(0x100000000000, 0xFFFFFFFFFFFF);
randomHexString: function (len) {
var ret = '';
while (len--) {
ret += this.randomHexDigit();
}
return ret;
}
};

View File

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