move username generator to utils

This commit is contained in:
isymchych 2015-11-04 16:39:56 +02:00 committed by yanas
parent 0a71c2e1d3
commit c89c9e78ff
3 changed files with 49 additions and 49 deletions

View File

@ -1,4 +1,4 @@
var UsernameGenerator = require('../statistics/UsernameGenerator');
var UsernameGenerator = require('../util/UsernameGenerator');
var email = '';
var displayName = '';
@ -31,7 +31,8 @@ if (supportsLocalStorage()) {
}
if (!window.localStorage.callStatsUID) {
window.localStorage.callStatsUID = UsernameGenerator.generateUsername();
console.log('generated callstats uid', window.localStorage.callStatsUID);
console.log('generated callstats uid',
window.localStorage.callStatsUID);
}
userId = window.localStorage.jitsiMeetId || '';
callStatsUID = window.localStorage.callStatsUID;

View File

@ -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);
}
/**

View File

@ -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;
}