Skip to content

Commit

Permalink
Add ESLint Config
Browse files Browse the repository at this point in the history
  • Loading branch information
killercup committed May 26, 2016
1 parent f4656c1 commit 6e2ef25
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 54 deletions.
12 changes: 12 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"env": {
"node": true,
"commonjs": true,
"es6": true
},
"extends": "eslint:recommended",
"rules": {
"comma-dangle": ["error", "always-multiline"],
"no-unused-vars": ["error", { "vars": "all", "args": "none" }]
}
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ const routes = require("./server");
server.use(routes);

server.listen(3000, () => {
console.log("Server started!");
process.stdout.write("Server started!");
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"start": "node server/index.js",
"test": "ava"
"test": "eslint 'server/**/*.js' 'test.js' 'index.js' && ava"
},
"author": "Pascal Hertleif <[email protected]> (http://pascalhertleif.de/)",
"license": "MIT",
Expand Down
21 changes: 10 additions & 11 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const path = require("path");
const fs = require("fs");
const express = require("express");
const hfstospell = require("hfst-ospell-js");

const H = require('./helpers');
Expand Down Expand Up @@ -34,9 +33,9 @@ const spellcheckers = langFiles.reduce((result, lang) => {
* @param {express.Response} res
* @param {express.RequestHandler} next
*/
module.exports.getBanner = function getBanner(req, res, next) {
module.exports.getBanner = function getBanner(req, res) {
res.status(200).jsonp({
"banner": false
"banner": false,
});
}

Expand All @@ -47,13 +46,13 @@ module.exports.getBanner = function getBanner(req, res, next) {
* @param {express.Response} res
* @param {express.RequestHandler} next
*/
module.exports.getLangList = function getLangList(req, res, next) {
module.exports.getLangList = function getLangList(req, res) {
res.status(200).jsonp({
"langList": {
"ltr": availableLanguages,
"rtl": {}
"rtl": {},
},
"verLang": 6
"verLang": 6,
});
}

Expand All @@ -68,16 +67,16 @@ module.exports.checkSpelling = function checkSpelling(req, res, next) {
// Quick n dirty validation of query params
const checks = [
H.checkQueryParams("out_type", {
status: 400, message: "`out_type` needs to be `words`"
status: 400, message: "`out_type` needs to be `words`",
}, "words"),
H.checkQueryParams("version", {
status: 400, message: "No language code given"
status: 400, message: "No language code given",
}),
H.checkQueryParams("slang", {
status: 400, message: "No language code given"
status: 400, message: "No language code given",
}),
H.checkQueryParams("text", {
status: 400, message: "No text to check given"
status: 400, message: "No text to check given",
}),
];

Expand All @@ -95,7 +94,7 @@ module.exports.checkSpelling = function checkSpelling(req, res, next) {

if (!spellchecker) {
return next({
status: 400, message: "I can't spellcheck `" + lang + "`, sorry."
status: 400, message: "I can't spellcheck `" + lang + "`, sorry.",
});
}

Expand Down
9 changes: 3 additions & 6 deletions server/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const express = require("express");

/**
* Check Query Params
*
Expand Down Expand Up @@ -55,16 +53,15 @@ module.exports.matchCommand = function matchCommand(cmd, fn) {
* @param {express.RequestHandler} next
*/
module.exports.errorHandler = function errorHandler(err, req, res, next) {
console.log(err);
var status = err.status || err.statusCode || 500;
if (status < 400) {
status = 500
};
}

res.status(status);

var body = {
status: status
status: status,
};

if (process.env.NODE_ENV !== 'production') {
Expand All @@ -73,7 +70,7 @@ module.exports.errorHandler = function errorHandler(err, req, res, next) {

// internal server errors
if (status >= 500) {
console.error(err.stack);
process.stderr.write(JSON.stringify(err.stack, null, 2));
res.json(body);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ let server = express.Router();

server.get(new RegExp("/spellcheck(31|)/script/ssrv.cgi"),
H.asMiddleware(H.checkQueryParams("cmd", {
status: 400, message: "No command given"
status: 400, message: "No command given",
})),
H.asMiddleware(H.checkQueryParams("run_mode", {
status: 400, message: "Invalid `run mode` param"
status: 400, message: "Invalid `run mode` param",
}, "web_service")),
// H.asMiddleware(H.checkQueryParams("format", {
// status: 400, message: "Invalid `format` param"
Expand Down
69 changes: 36 additions & 33 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {test} from "ava";
const test = require("ava").test;
const request = require("supertest-as-promised");
const express = require("express");

Expand All @@ -8,50 +8,53 @@ app.use(require("./server"));
const API = "/spellcheck/script/ssrv.cgi";

const cmd = (name) =>
({ cmd: name, run_mode: "web_service", format: "json" });
({ 'cmd': name, 'run_mode': "web_service", 'format': "json" });

const spellCmd = () => ({
cmd: "check_spelling", run_mode: "web_service", format: "json",
version: "1.0", out_type: "words"
'cmd': "check_spelling",
'run_mode': "web_service",
'format': "json",
'version': "1.0",
'out_type': "words",
});

test("Get Banner", async (t) => {
await request(app)
test("Get Banner", () =>
request(app)
.get(API)
.query(cmd("getbanner"))
.expect(200)
.expect({ banner: false });
});
.expect({ banner: false })
);

test("Get Lang List", async (t) => {
await request(app)
test("Get Lang List", () =>
request(app)
.get(API)
.query(cmd("get_lang_list"))
.expect(200)
.expect({
langList: {
ltr: {
sma_NO: 'South Sámi',
sme_NO: 'Northern Sámi',
smj_NO: 'Lule Sámi'
'sma_NO': 'South Sámi',
'sme_NO': 'Northern Sámi',
'smj_NO': 'Lule Sámi',
},
rtl: {}
rtl: {},
},
verLang: 6
});
});
verLang: 6,
})
);

test("Check Spelling: Correct Word", async (t) => {
await request(app)
test("Check Spelling: Correct Word", () =>
request(app)
.get(API)
.query(spellCmd())
.query({ slang: "sma_NO", text: "akkusatijvh" })
.expect(200)
.expect([]);
});
.expect([])
);

test("Check Spelling: Suggestions", async (t) => {
await request(app)
test("Check Spelling: Suggestions", () =>
request(app)
.get(API)
.query(spellCmd())
.query({ slang: "sma_NO", text: "akkusativa" })
Expand All @@ -60,13 +63,13 @@ test("Check Spelling: Suggestions", async (t) => {
{
word: 'akkusativa',
suggestions: ['akkusatijvh', 'akkusatijvi', 'akkusativ-D', 'akkusativ-V', 'akkusativ-M', 'akkusativ-L', 'akkusativ-I', 'akkusativ-C'],
ud: false
}
]);
});
ud: false,
},
])
);

test("Check Spelling: Empty Suggestions", async (t) => {
await request(app)
test("Check Spelling: Empty Suggestions", () =>
request(app)
.get(API)
.query(spellCmd())
.query({ slang: "sma_NO", text: "apfelkuchen" })
Expand All @@ -75,8 +78,8 @@ test("Check Spelling: Empty Suggestions", async (t) => {
{
word: 'apfelkuchen',
suggestions: [],
ud: false
}
]);
});
ud: false,
},
])
);

0 comments on commit 6e2ef25

Please sign in to comment.