diff --git a/modules/settings/Settings.js b/modules/settings/Settings.js index 0050645cb..a45c63d0d 100644 --- a/modules/settings/Settings.js +++ b/modules/settings/Settings.js @@ -1,4 +1,4 @@ -var UsernameGenerator = require('../statistics/UsernameGenerator'); +var UsernameGenerator = require('../util/UsernameGenerator'); var email = ''; var displayName = ''; diff --git a/modules/util/RandomUtil.js b/modules/util/RandomUtil.js index ba888b020..5c9992939 100644 --- a/modules/util/RandomUtil.js +++ b/modules/util/RandomUtil.js @@ -1,13 +1,51 @@ +/** + * @const + */ +var ALPHANUM = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + +/** + * Generates random int 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 int number + */ +function randomInt(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; +} + +/** + * Get random element from array or string. + * @param {Array|string} arr source + * @returns array element or string character + */ +function randomElement(arr) { + return arr[randomInt(0, arr.length -1)]; +} + +/** + * Generate random alphanumeric string. + * @param {number} length expected string length + * @returns {string} random string of specified length + */ +function randomAlphanumStr(length) { + var result = ''; + + for (var i = 0; i < length; i += 1) { + result += randomElement(ALPHANUM); + } + + return result; +} /** * Generates random hex number within the range [min, max] - * @param max the maximum value for the generated number * @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 Math.floor(Math.random() * (max - min) + min).toString(16); + return randomInt(min, max).toString(16); } /** @@ -31,7 +69,11 @@ var RandomUtil = { */ random12digitsHex: function() { return rangeRandomHex(17592186044416, 281474976710655); - } + }, + + randomElement: randomElement, + + randomAlphanumStr: randomAlphanumStr }; module.exports = RandomUtil; diff --git a/modules/statistics/UsernameGenerator.js b/modules/util/UsernameGenerator.js similarity index 97% rename from modules/statistics/UsernameGenerator.js rename to modules/util/UsernameGenerator.js index e2cd22258..06a09471d 100644 --- a/modules/statistics/UsernameGenerator.js +++ b/modules/util/UsernameGenerator.js @@ -1,5 +1,8 @@ +var RandomUtil = require('./RandomUtil'); + /** - * from faker.js - Copyright (c) 2014-2015 Matthew Bergman & Marak Squires, MIT License + * from faker.js - Copyright (c) 2014-2015 Matthew Bergman & Marak Squires + * MIT License * http://github.com/marak/faker.js/ * * @const @@ -410,55 +413,13 @@ var names = [ "Zora", "Zula" ]; -/** - * @const - */ -var suffixChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; - - -/** - * Returns a random integer between min (inclusive) and max (inclusive). - */ -function getRandomInt(min, max) { - return Math.floor(Math.random() * (max - min + 1)) + min; -} - - -/** - * Get random element from array or string. - * - * @param {Array|string} arr source - * - * @returns array element or string character - */ -function getRandomElement(arr) { - return arr[getRandomInt(0, arr.length -1)]; -} - -/** - * Generate random alphanumeric string. - * - * @param {number} length expected string length - * - * @returns {string} random string of specified length - */ -function generateAlphanumStr(length) { - var result = ''; - - for (var i = 0; i < length; i += 1) { - result += getRandomElement(suffixChars); - } - - return result; -} - /** * Generate random username. * @returns {string} random username */ function generateUsername () { - var name = getRandomElement(names); - var suffix = generateAlphanumStr(3); + var name = RandomUtil.randomElement(names); + var suffix = RandomUtil.randomAlphanumStr(3); return name + '-' + suffix; }