Moves the roomname generator to util, reuses a function.

This commit is contained in:
Boris Grozev 2015-11-06 16:08:20 -06:00
parent fb6367d687
commit 4009fed35d
3 changed files with 9 additions and 16 deletions

View File

@ -16,7 +16,7 @@ var EventEmitter = require("events");
var SettingsMenu = require("./side_pannels/settings/SettingsMenu");
var Settings = require("./../settings/Settings");
var PanelToggler = require("./side_pannels/SidePanelToggler");
var RoomNameGenerator = require("./welcome_page/RoomnameGenerator");
var RoomnameGenerator = require("../util/RoomnameGenerator");
UI.messageHandler = require("./util/MessageHandler");
var messageHandler = UI.messageHandler;
var Authentication = require("./authentication/Authentication");
@ -718,7 +718,7 @@ UI.getRoomNode = function () {
if (path.length > 1) {
roomNode = path.substr(1).toLowerCase();
} else {
var word = RoomNameGenerator.generateRoomWithoutSeparator();
var word = RoomnameGenerator.generateRoomWithoutSeparator();
roomNode = word.toLowerCase();
window.history.pushState('VideoChat',
'Room: ' + word, window.location.pathname + word);

View File

@ -1,7 +1,7 @@
/* global $, interfaceConfig */
var animateTimeout, updateTimeout;
var RoomNameGenerator = require("./RoomnameGenerator");
var RoomnameGenerator = require("../../util/RoomnameGenerator");
function enter_room() {
var val = $("#enter_room_field").val();
@ -22,7 +22,7 @@ function animate(word) {
}
function update_roomname() {
var word = RoomNameGenerator.generateRoomWithoutSeparator();
var word = RoomnameGenerator.generateRoomWithoutSeparator();
$("#enter_room_field").attr("room_name", word);
$("#enter_room_field").attr("placeholder", "");
clearTimeout(animateTimeout);

View File

@ -1,3 +1,4 @@
var RandomUtil = require('./RandomUtil');
//var nouns = [
//];
var pluralNouns = [
@ -160,14 +161,6 @@ var PATTERNS = [
//"_ADJECTIVE__PLURALNOUN_AtThe_PLACE_",
];
/*
* Returns a random element from the array 'arr'
*/
function randomElement(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
/*
* Returns true if the string 's' contains one of the
* template strings.
@ -183,17 +176,17 @@ function hasTemplate(s) {
/**
* Generates new room name.
*/
var RoomNameGenerator = {
var RoomnameGenerator = {
generateRoomWithoutSeparator: function() {
// Note that if more than one pattern is available, the choice of
// 'name' won't have a uniform distribution amongst all patterns (names
// from patterns with fewer options will have higher probability of
// being chosen that names from patterns with more options).
var name = randomElement(PATTERNS);
var name = RandomUtil.randomElement(PATTERNS);
var word;
while (hasTemplate(name)) {
for (var template in CATEGORIES) {
word = randomElement(CATEGORIES[template]);
word = RandomUtil.randomElement(CATEGORIES[template]);
name = name.replace(template, word);
}
}
@ -202,4 +195,4 @@ var RoomNameGenerator = {
}
};
module.exports = RoomNameGenerator;
module.exports = RoomnameGenerator;