From 31eb1e172da3280d08d8a6c5602dbeba323dbe79 Mon Sep 17 00:00:00 2001 From: Andrei Sevostianov Date: Fri, 27 Dec 2019 10:46:59 -0800 Subject: [PATCH] style: make code more readable --- server.js | 39 ++++++++++++++++++++------------------ test.js | 56 +++++++++++++++++++++++++++---------------------------- 2 files changed, 49 insertions(+), 46 deletions(-) diff --git a/server.js b/server.js index de0fea0..014956d 100644 --- a/server.js +++ b/server.js @@ -1,28 +1,31 @@ -const http = require("http"); +const http = require('http') const _ = require('lodash') -const PORT = process.env.PORT || 3000; +const PORT = process.env.PORT || 3000 const server = http.createServer((req, res) => { - if (req.url === "/") return respondHello(req, res); - if (req.url === "/user-agent") return respondU(req, res); - if (req.url.match(/^\/b64\//)) return respondB(req, res); + if (req.url === '/') return respondHello(req, res) + if (req.url === '/user-agent') return respondUserAgent(req, res) + if (req.url.match(/^\/b64\//)) return respondBase64(req, res) if (req.url === '/repetitive-word') return respondRepetitiveWord(req, res) - res.end(); -}); + res.end() +}) -function respondHello(req, res) { - res.end(JSON.stringify({ msg: "hello" })); +function respondHello (req, res) { + res.end(JSON.stringify({ msg: 'hello' })) } -function respondU(req, res) { - const ua = req.headers["user-agent"]; - res.end(JSON.stringify({ ua })); +function respondUserAgent (req, res) { + const ua = req.headers['user-agent'] + res.end(JSON.stringify({ ua })) } -function respondB(req, res) { - res.end(JSON.stringify({b64: Buffer.from(req.url.replace(/^\/b64\//, "")).toString("base64")})); +function respondBase64 (req, res) { + const base64 = req.url.replace(/^\/b64\//, '') + res.end( + JSON.stringify({ + b64: Buffer.from(base64).toString('base64') })) } function respondRepetitiveWord (req, res) { @@ -37,10 +40,10 @@ function respondRepetitiveWord (req, res) { }) } -server.listen(PORT); -console.log(`Server listening on port ${PORT}`); +server.listen(PORT) +console.log(`Server listening on port ${PORT}`) -if (require.main !== module) module.exports = server; +if (require.main !== module) module.exports = server function countWords (text) { const parsedText = {} @@ -63,4 +66,4 @@ function countWords (text) { repetitiveWords.push(word) }) return _.orderBy(repetitiveWords, ['count'], ['desc']) -} \ No newline at end of file +} diff --git a/test.js b/test.js index 2b0d3c5..0a1e88b 100644 --- a/test.js +++ b/test.js @@ -1,39 +1,39 @@ -const tape = require("tape"); -const jsonist = require("jsonist"); +const tape = require('tape') +const jsonist = require('jsonist') const PORT = (process.env.PORT = - process.env.PORT || require("get-PORT-sync")()); -const server = require("./server"); + process.env.PORT || require('get-PORT-sync')()) +const server = require('./server') -const urlBase = `http://localhost:${PORT}`; +const urlBase = `http://localhost:${PORT}` -tape("should respond hello", t => { +tape('should respond hello', t => { jsonist.get(urlBase, (err, body) => { - if (err) t.error(err); + if (err) t.error(err) - t.equal(body.msg, "hello"); - t.end(); - }); -}); + t.equal(body.msg, 'hello') + t.end() + }) +}) -tape("should respond user-agent", t => { - const opts = { headers: { "User-Agent": "tape" } }; +tape('should respond user-agent', t => { + const opts = { headers: { 'User-Agent': 'tape' } } jsonist.get(`${urlBase}/user-agent`, opts, (err, body) => { - if (err) t.error(err); + if (err) t.error(err) - t.equal(body.ua, "tape"); - t.end(); - }); -}); + t.equal(body.ua, 'tape') + t.end() + }) +}) -tape("should respond b64", t => { +tape('should respond b64', t => { jsonist.get(`${urlBase}/b64/hello`, (err, body) => { - if (err) t.error(err); + if (err) t.error(err) - t.equal(body.b64, "aGVsbG8="); - t.end(); - }); -}); + t.equal(body.b64, 'aGVsbG8=') + t.end() + }) +}) tape('should respond repetitive-word', (t) => { const data = { @@ -48,7 +48,7 @@ tape('should respond repetitive-word', (t) => { }) }) -tape("cleanup", function(t) { - server.close(); - t.end(); -}); +tape('cleanup', function (t) { + server.close() + t.end() +})