From e982951281cd1a73e6cd8aabf154552fb4d77844 Mon Sep 17 00:00:00 2001 From: Ryan Feline Date: Wed, 23 Aug 2017 16:11:05 +0100 Subject: [PATCH] Use let and const over var. --- .../server-unit/core/botLocator.js | 8 +++---- .../server-unit/core/clientType.js | 2 +- .../server-unit/core/disconnector.js | 12 +++++----- TelepresenceBot/server-unit/core/mover.js | 4 ++-- .../server-unit/core/public/js/connect.js | 8 +++---- TelepresenceBot/server-unit/core/router.js | 10 ++++---- TelepresenceBot/server-unit/core/server.js | 4 ++-- .../server-unit/core/serverCreator.js | 7 +++--- TelepresenceBot/server-unit/test/apiTest.js | 20 ++++++++-------- .../server-unit/test/botLocatorTest.js | 24 +++++++++---------- .../server-unit/test/disconnectorTest.js | 14 +++++------ TelepresenceBot/server-unit/test/moverTest.js | 14 +++++------ .../server-unit/test/routerTest.js | 12 +++++----- .../server-unit/test/serverCreatorTest.js | 14 +++++------ 14 files changed, 77 insertions(+), 76 deletions(-) diff --git a/TelepresenceBot/server-unit/core/botLocator.js b/TelepresenceBot/server-unit/core/botLocator.js index a5d9c6940..03f7b2c7c 100644 --- a/TelepresenceBot/server-unit/core/botLocator.js +++ b/TelepresenceBot/server-unit/core/botLocator.js @@ -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')) diff --git a/TelepresenceBot/server-unit/core/clientType.js b/TelepresenceBot/server-unit/core/clientType.js index 9b495d44d..0355a6cba 100644 --- a/TelepresenceBot/server-unit/core/clientType.js +++ b/TelepresenceBot/server-unit/core/clientType.js @@ -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]; } diff --git a/TelepresenceBot/server-unit/core/disconnector.js b/TelepresenceBot/server-unit/core/disconnector.js index 080ebaf08..ac989cf53 100644 --- a/TelepresenceBot/server-unit/core/disconnector.js +++ b/TelepresenceBot/server-unit/core/disconnector.js @@ -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(); } diff --git a/TelepresenceBot/server-unit/core/mover.js b/TelepresenceBot/server-unit/core/mover.js index 1b340a5ed..70246d66b 100644 --- a/TelepresenceBot/server-unit/core/mover.js +++ b/TelepresenceBot/server-unit/core/mover.js @@ -1,6 +1,6 @@ module.exports = function Mover(clientsAndRooms, emitter) { - var emitToRoom = function (direction) { + const emitToRoom = function (direction) { return function (room) { emitter.to(room).emit('direction', direction); } @@ -8,7 +8,7 @@ module.exports = function Mover(clientsAndRooms, emitter) { return { moveIn: function (clientId, direction) { - var rooms = clientsAndRooms[clientId]; + const rooms = clientsAndRooms[clientId]; Object.keys(rooms || {}) .every(emitToRoom(direction)); diff --git a/TelepresenceBot/server-unit/core/public/js/connect.js b/TelepresenceBot/server-unit/core/public/js/connect.js index 0dabe9604..152f7e465 100644 --- a/TelepresenceBot/server-unit/core/public/js/connect.js +++ b/TelepresenceBot/server-unit/core/public/js/connect.js @@ -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); diff --git a/TelepresenceBot/server-unit/core/router.js b/TelepresenceBot/server-unit/core/router.js index 1dbdf5b9c..cef266ce2 100644 --- a/TelepresenceBot/server-unit/core/router.js +++ b/TelepresenceBot/server-unit/core/router.js @@ -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; diff --git a/TelepresenceBot/server-unit/core/server.js b/TelepresenceBot/server-unit/core/server.js index 02d4fdd2e..cd06fadee 100644 --- a/TelepresenceBot/server-unit/core/server.js +++ b/TelepresenceBot/server-unit/core/server.js @@ -1,4 +1,4 @@ -var ServerCreator = require('./serverCreator'); +const ServerCreator = require('./serverCreator'); -var serverCreator = new ServerCreator(); +const serverCreator = new ServerCreator(); serverCreator.create(); diff --git a/TelepresenceBot/server-unit/core/serverCreator.js b/TelepresenceBot/server-unit/core/serverCreator.js index 097748681..36c7e2212 100644 --- a/TelepresenceBot/server-unit/core/serverCreator.js +++ b/TelepresenceBot/server-unit/core/serverCreator.js @@ -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), diff --git a/TelepresenceBot/server-unit/test/apiTest.js b/TelepresenceBot/server-unit/test/apiTest.js index 9eb6b5d97..89efbdf91 100644 --- a/TelepresenceBot/server-unit/test/apiTest.js +++ b/TelepresenceBot/server-unit/test/apiTest.js @@ -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 () { @@ -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(); }); @@ -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(); }); diff --git a/TelepresenceBot/server-unit/test/botLocatorTest.js b/TelepresenceBot/server-unit/test/botLocatorTest.js index e18a609e5..7c570f25d 100644 --- a/TelepresenceBot/server-unit/test/botLocatorTest.js +++ b/TelepresenceBot/server-unit/test/botLocatorTest.js @@ -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 @@ -12,7 +12,7 @@ var roomWithASingleBot = { } }; -var roomWhereBotIsConnectedToHuman = { +const roomWhereBotIsConnectedToHuman = { 'botId': { sockets: { 'botId': true, 'humanId': true }, length: 2 @@ -23,7 +23,7 @@ var roomWhereBotIsConnectedToHuman = { } }; -var roomContainingMultipleBotsWhereOneIsConnectedToHuman = { +const roomContainingMultipleBotsWhereOneIsConnectedToHuman = { 'botId01': { sockets: { 'botId01': true, 'human01': true }, length: 2 @@ -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(); diff --git a/TelepresenceBot/server-unit/test/disconnectorTest.js b/TelepresenceBot/server-unit/test/disconnectorTest.js index ff62da92a..62f2651e5 100644 --- a/TelepresenceBot/server-unit/test/disconnectorTest.js +++ b/TelepresenceBot/server-unit/test/disconnectorTest.js @@ -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 @@ -16,7 +16,7 @@ var rooms = { } }; -var connectedClients = { +let connectedClients = { called: false, 'botId': { disconnect: function () { @@ -25,7 +25,7 @@ var connectedClients = { } }; -var noConnectedClients = { +let noConnectedClients = { called: false, disconnect: function () { noConnectedClients.called = true; @@ -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'); @@ -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'); @@ -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'); diff --git a/TelepresenceBot/server-unit/test/moverTest.js b/TelepresenceBot/server-unit/test/moverTest.js index c6e2ca676..2146dcf2a 100644 --- a/TelepresenceBot/server-unit/test/moverTest.js +++ b/TelepresenceBot/server-unit/test/moverTest.js @@ -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 }, @@ -10,7 +10,7 @@ var clientsAndRooms = { } } -var emitter = { +let emitter = { called: false, to: function (room) { return { @@ -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'); @@ -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'); @@ -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'); @@ -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'); diff --git a/TelepresenceBot/server-unit/test/routerTest.js b/TelepresenceBot/server-unit/test/routerTest.js index e29f4a123..7f06bcfd7 100644 --- a/TelepresenceBot/server-unit/test/routerTest.js +++ b/TelepresenceBot/server-unit/test/routerTest.js @@ -1,19 +1,19 @@ -var sinon = require('sinon'), +const sinon = require('sinon'), expect = require('chai').expect, botLocator = require('../core/BotLocator.js')(), router = require('../core/Router.js')(botLocator); -var queryWithHuman = { +const queryWithHuman = { clientType: 'human', room: 'London', }; -var queryWithBot = { +const queryWithBot = { clientType: 'bot', room: 'London', }; -var queryWithUnhandled = { +const queryWithUnhandled = { clientType: 'unhandled', room: 'London', }; @@ -21,7 +21,7 @@ var queryWithUnhandled = { describe('Router Tests.', function () { it('Should replace query.room with first available bot when clientType is Human.', function (done) { - var firstAvailableBotId = 'ABCDEFGH123'; + const firstAvailableBotId = 'ABCDEFGH123'; mockBotLocator = sinon.stub(botLocator, 'locateFirstAvailableBotIn') .callsFake(function () { return firstAvailableBotId; }); @@ -46,7 +46,7 @@ describe('Router Tests.', function () { }); it('Should return Error when the are no available Bots.', function (done) { - var noAvailableBots = undefined; + const noAvailableBots = undefined; mockBotLocator = sinon.stub(botLocator, 'locateFirstAvailableBotIn') .callsFake(function () { return noAvailableBots; }); diff --git a/TelepresenceBot/server-unit/test/serverCreatorTest.js b/TelepresenceBot/server-unit/test/serverCreatorTest.js index 11aa5f6b2..46310e7d8 100644 --- a/TelepresenceBot/server-unit/test/serverCreatorTest.js +++ b/TelepresenceBot/server-unit/test/serverCreatorTest.js @@ -1,4 +1,4 @@ -var sinon = require('sinon'), +const sinon = require('sinon'), expect = require('chai').expect, router = require('../core/Router.js')(), disconnector = require('../core/Disconnector.js')(), @@ -6,9 +6,9 @@ var sinon = require('sinon'), mover = require('../core/mover.js')(), ServerCreator = require('../core/serverCreator.js'); -var io = require('socket.io-client'); +const io = require('socket.io-client'); -var socketUrl = 'http://localhost:4200'; +const socketUrl = 'http://localhost:4200'; options = { transports: ['websocket'], @@ -42,7 +42,7 @@ describe('ServerCreator Tests.', function () { }); it('Should delegate to Router when client is connected.', function (done) { - var client = io.connect(socketUrl, options); + const client = io.connect(socketUrl, options); client.once('connect', function () { client.once('joined_room', function (room) { @@ -53,7 +53,7 @@ describe('ServerCreator Tests.', function () { }); it('Should emit joined_room when client is connected.', function (done) { - var client = io.connect(socketUrl, options); + const client = io.connect(socketUrl, options); client.once('connect', function () { client.once('joined_room', function (room) { @@ -64,7 +64,7 @@ describe('ServerCreator Tests.', function () { }); it('Should delegate to Mover when moving in given direction.', function (done) { - var client = io.connect(socketUrl, options); + const client = io.connect(socketUrl, options); client.once('connect', function () { client.emit('move_in', 'forward'); @@ -79,7 +79,7 @@ describe('ServerCreator Tests.', function () { }); it('Should delegate to Disconnector when disconnecting an already connected client.', function (done) { - var client = io.connect(socketUrl, options); + const client = io.connect(socketUrl, options); client.once('connect', function () { client.disconnect();