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

View File

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