Skip to content

Commit

Permalink
Use let and const over var.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Feline committed Aug 23, 2017
1 parent f541733 commit e982951
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 76 deletions.
8 changes: 4 additions & 4 deletions TelepresenceBot/server-unit/core/botLocator.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
module.exports = function (locationRooms) {

var roomsThatMatch = function (toMatch) {
const roomsThatMatch = function (toMatch) {
return function (room) {
return toMatch == room;
};
}

var roomsThatContainProperty = function (property) {
const roomsThatContainProperty = function (property) {
return function (roomName) {
return locationRooms[roomName].hasOwnProperty(property);
};
}

var roomsThatAreNotAlreadyConnectedToOtherSockets = function () {
const roomsThatAreNotAlreadyConnectedToOtherSockets = function () {
return function (roomName) {
return locationRooms[roomName].length == 1;
};
}

var asEmptyBotRoom = function () {
const asEmptyBotRoom = function () {
return function (locationRoom) {
return Object.keys(locationRooms[locationRoom].sockets)
.filter(roomsThatContainProperty('length'))
Expand Down
2 changes: 1 addition & 1 deletion TelepresenceBot/server-unit/core/clientType.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = ClientType = {
HUMAN: 'human',
TEST: 'test',
from: function (rawClientType) {
for (var key in this) {
for (let key in this) {
if (ClientType[key] == rawClientType) {
return ClientType[key];
}
Expand Down
12 changes: 6 additions & 6 deletions TelepresenceBot/server-unit/core/disconnector.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
module.exports = function Disconnector(rooms, connectedClients) {

var roomsThatMatch = function (toMatch) {
const roomsThatMatch = function (toMatch) {
return function (room) {
return toMatch == room;
};
}

var roomsThatContainProperty = function (property) {
const roomsThatContainProperty = function (property) {
return function (roomName) {
return rooms[roomName].hasOwnProperty(property);
};
}

var thosePresentIn = function (objectToSearch) {
const thosePresentIn = function (objectToSearch) {
return function (key) {
return objectToSearch[key]
};
}

var asConnectedClient = function () {
const asConnectedClient = function () {
return function (key) {
return connectedClients[key];
};
}

var connectedClientsThatContainProperty = function (property) {
const connectedClientsThatContainProperty = function (property) {
return function (connectedClient) {
return connectedClient.hasOwnProperty(property);
}
}

var disconnectClient = function () {
const disconnectClient = function () {
return function (connectedClient) {
connectedClient.disconnect();
}
Expand Down
4 changes: 2 additions & 2 deletions TelepresenceBot/server-unit/core/mover.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module.exports = function Mover(clientsAndRooms, emitter) {

var emitToRoom = function (direction) {
const emitToRoom = function (direction) {
return function (room) {
emitter.to(room).emit('direction', direction);
}
}

return {
moveIn: function (clientId, direction) {
var rooms = clientsAndRooms[clientId];
const rooms = clientsAndRooms[clientId];

Object.keys(rooms || {})
.every(emitToRoom(direction));
Expand Down
8 changes: 4 additions & 4 deletions TelepresenceBot/server-unit/core/public/js/connect.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
var intervalId;
const intervalId;

var socketOptions ={
const socketOptions ={
transports: ['websocket'],
'force new connection': true,
query: 'clientType=human&room=London'
};

var socket = io(socketOptions);
const socket = io(socketOptions);

$(document).ready(function(){

$("input").mousedown(function(){
console.log("mouseDown");
var message = $(this).val();
const message = $(this).val();
intervalId = setInterval(function() {
sendMessage(message);
}, 100);
Expand Down
10 changes: 5 additions & 5 deletions TelepresenceBot/server-unit/core/router.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
var ClientType = require('./clientType.js')
const ClientType = require('./clientType.js')

module.exports = function Router(botLocator) {
return {
route: function (query, next) {
var roomName = query.room;
var rawClientType = query.clientType;
var clientType = ClientType.from(rawClientType);
const roomName = query.room;
const rawClientType = query.clientType;
const clientType = ClientType.from(rawClientType);

switch (clientType) {
case ClientType.BOT:
return next();
case ClientType.HUMAN:
var availableBot = botLocator.locateFirstAvailableBotIn(roomName);
const availableBot = botLocator.locateFirstAvailableBotIn(roomName);

if (availableBot) {
query.room = availableBot;
Expand Down
4 changes: 2 additions & 2 deletions TelepresenceBot/server-unit/core/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var ServerCreator = require('./serverCreator');
const ServerCreator = require('./serverCreator');

var serverCreator = new ServerCreator();
const serverCreator = new ServerCreator();
serverCreator.create();
7 changes: 4 additions & 3 deletions TelepresenceBot/server-unit/core/serverCreator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var express = require('express'),
const express = require('express'),
app = express(),
httpServer = require('http').createServer(app),
io = require('socket.io')(httpServer),
path = require('path'),
debug = require('debug')('server'),
botLocator = require('./botLocator.js')(io.sockets.adapter.rooms),
debug = require('debug')('server');

let botLocator = require('./botLocator.js')(io.sockets.adapter.rooms),
router = require('./router.js')(botLocator),
disconnector = require('./disconnector.js')(io.sockets.adapter.rooms, io.sockets.connected),
mover = require('./mover.js')(io.sockets.adapter.sids, io),
Expand Down
20 changes: 10 additions & 10 deletions TelepresenceBot/server-unit/test/apiTest.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var mocha = require('mocha'),
const mocha = require('mocha'),
request = require('supertest'),
expect = require('chai').expect,
fs = require('fs'),
ServerCreator = require('../core/serverCreator.js');
ServerCreator = require('../core/serverCreator.js'),
io = require('socket.io-client'),
options = {
transports: ['websocket'],
'force new connection': true
};

var io = require('socket.io-client');

var server, options = {
transports: ['websocket'],
'force new connection': true
};
let server;

describe('API Tests - Performing GET requests.', function () {

Expand All @@ -28,7 +28,7 @@ describe('API Tests - Performing GET requests.', function () {
.type('html')
.expect(200)
.end(function (error, response) {
var file = fs.readFileSync('../core/html/index.html', 'utf8');
const file = fs.readFileSync('../core/html/index.html', 'utf8');
expect(response.text).to.equal(file);
done();
});
Expand All @@ -40,7 +40,7 @@ describe('API Tests - Performing GET requests.', function () {
.type('json')
.expect(200)
.end(function (error, response) {
var file = fs.readFileSync('../core/json/rooms.json', 'utf8');
const file = fs.readFileSync('../core/json/rooms.json', 'utf8');
expect(response.text).to.equal(file);
done();
});
Expand Down
24 changes: 12 additions & 12 deletions TelepresenceBot/server-unit/test/botLocatorTest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var expect = require('chai').expect
const expect = require('chai').expect
BotLocator = require('../core/botLocator.js');

var roomWithASingleBot = {
const roomWithASingleBot = {
'botId': {
sockets: { 'botId': true },
length: 1
Expand All @@ -12,7 +12,7 @@ var roomWithASingleBot = {
}
};

var roomWhereBotIsConnectedToHuman = {
const roomWhereBotIsConnectedToHuman = {
'botId': {
sockets: { 'botId': true, 'humanId': true },
length: 2
Expand All @@ -23,7 +23,7 @@ var roomWhereBotIsConnectedToHuman = {
}
};

var roomContainingMultipleBotsWhereOneIsConnectedToHuman = {
const roomContainingMultipleBotsWhereOneIsConnectedToHuman = {
'botId01': {
sockets: { 'botId01': true, 'human01': true },
length: 2
Expand All @@ -41,36 +41,36 @@ var roomContainingMultipleBotsWhereOneIsConnectedToHuman = {
describe('BotLocator Tests.', function () {

it('Should give undefined when bot is not found in given room.', function (done) {
var botLocator = new BotLocator(roomWithASingleBot);
const botLocator = new BotLocator(roomWithASingleBot);

var bot = botLocator.locateFirstAvailableBotIn('Unexpected Room');
const bot = botLocator.locateFirstAvailableBotIn('Unexpected Room');

expect(bot).to.be.undefined;
done();
});

it('Should give bot id when bot room does not contain other sockets.', function (done) {
var botLocator = new BotLocator(roomWithASingleBot);
const botLocator = new BotLocator(roomWithASingleBot);

var bot = botLocator.locateFirstAvailableBotIn('London');
const bot = botLocator.locateFirstAvailableBotIn('London');

expect(bot).to.equal('botId');
done();
});

it('Should give undefined when bot room contains other sockets.', function (done) {
var botLocator = new BotLocator(roomWhereBotIsConnectedToHuman);
const botLocator = new BotLocator(roomWhereBotIsConnectedToHuman);

var bot = botLocator.locateFirstAvailableBotIn('London');
const bot = botLocator.locateFirstAvailableBotIn('London');

expect(bot).to.be.undefined;
done();
});

it('Should give first bot in room that contains multiple bots.', function (done) {
var botLocator = new BotLocator(roomContainingMultipleBotsWhereOneIsConnectedToHuman);
const botLocator = new BotLocator(roomContainingMultipleBotsWhereOneIsConnectedToHuman);

var bot = botLocator.locateFirstAvailableBotIn('London');
const bot = botLocator.locateFirstAvailableBotIn('London');

expect(bot).to.equal('botId02');
done();
Expand Down
14 changes: 7 additions & 7 deletions TelepresenceBot/server-unit/test/disconnectorTest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var expect = require('chai').expect,
const expect = require('chai').expect,
Disconnector = require('../core/disconnector.js');

var rooms = {
const rooms = {
'botId': {
sockets: { 'botId': true },
length: 1
Expand All @@ -16,7 +16,7 @@ var rooms = {
}
};

var connectedClients = {
let connectedClients = {
called: false,
'botId': {
disconnect: function () {
Expand All @@ -25,7 +25,7 @@ var connectedClients = {
}
};

var noConnectedClients = {
let noConnectedClients = {
called: false,
disconnect: function () {
noConnectedClients.called = true;
Expand All @@ -41,7 +41,7 @@ afterEach(function (done) {
describe('Disconnector Tests.', function () {

it('Should do nothing when cannot locate room in list of rooms.', function (done) {
var disconnector = new Disconnector(rooms, connectedClients);
const disconnector = new Disconnector(rooms, connectedClients);

disconnector.disconnectRoom('Room not present');

Expand All @@ -50,7 +50,7 @@ describe('Disconnector Tests.', function () {
});

it('Should do nothing when connected clients does contain any clients.', function (done) {
var disconnector = new Disconnector(rooms, noConnectedClients);
const disconnector = new Disconnector(rooms, noConnectedClients);

disconnector.disconnectRoom('London');

Expand All @@ -59,7 +59,7 @@ describe('Disconnector Tests.', function () {
});

it('Should call disconnect when disconnecting all clients in room.', function (done) {
var disconnector = new Disconnector(rooms, connectedClients);
const disconnector = new Disconnector(rooms, connectedClients);

disconnector.disconnectRoom('London');

Expand Down
14 changes: 7 additions & 7 deletions TelepresenceBot/server-unit/test/moverTest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var expect = require('chai').expect,
const expect = require('chai').expect,
Mover = require('../core/mover.js');

var clientsAndRooms = {
const clientsAndRooms = {
'humanId': {
'botId': true
},
Expand All @@ -10,7 +10,7 @@ var clientsAndRooms = {
}
}

var emitter = {
let emitter = {
called: false,
to: function (room) {
return {
Expand All @@ -32,7 +32,7 @@ afterEach(function (done) {
describe('Mover Tests.', function () {

it('Should emit to botId when moving humanId in any direction.', function (done) {
var mover = new Mover(clientsAndRooms, emitter);
const mover = new Mover(clientsAndRooms, emitter);

mover.moveIn('humanId', 'forward');

Expand All @@ -42,7 +42,7 @@ describe('Mover Tests.', function () {
});

it('Should emit event of direction when moving humanId in any direction.', function (done) {
var mover = new Mover(clientsAndRooms, emitter);
const mover = new Mover(clientsAndRooms, emitter);

mover.moveIn('humanId', 'forward');

Expand All @@ -52,7 +52,7 @@ describe('Mover Tests.', function () {
});

it('Should emit value of forward when moving humanId in a forward direction.', function (done) {
var mover = new Mover(clientsAndRooms, emitter);
const mover = new Mover(clientsAndRooms, emitter);

mover.moveIn('humanId', 'forward');

Expand All @@ -62,7 +62,7 @@ describe('Mover Tests.', function () {
});

it('Should do nothing when a given clientId does not belong to any rooms.', function (done) {
var mover = new Mover(clientsAndRooms, emitter);
const mover = new Mover(clientsAndRooms, emitter);

mover.moveIn('clientId', 'forward');

Expand Down
Loading

0 comments on commit e982951

Please sign in to comment.