Skip to content

Commit

Permalink
Removing logical NOT operator to remove needless ops, removing more t…
Browse files Browse the repository at this point in the history
…ruthy/falsy statements
  • Loading branch information
avoidwork committed Sep 4, 2017
1 parent ceaa39c commit dde9f53
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ function valid (req, res, next) {
outDir = !invalid ? (pathname.match(/\.{2}\//g) || []).length : 0,
inDir = !invalid ? (pathname.match(/\w+?(\.\w+|\/)+/g) || []).length : 0;

if (invalid) {
if (invalid === true) {
next(new Error(404));
} else if (outDir > 0 && outDir >= inDir) {
next(new Error(404));
} else {
next();
}
} else {
next(new Error(req.allow.length > 0 ? 405 : 404));
next(new Error(req.allow !== "" ? 405 : 404));
}
}

Expand Down
1 change: 0 additions & 1 deletion lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
module.exports = {
escape: arg => arg.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&"),
getArity: arg => arg.toString().replace(/(^.*\()|(\).*)|(\n.*)/g, "").split(",").length,
isEmpty: arg => arg.replace(/^(\s+|\t+|\n+)|(\s+|\t+|\n+)$/g, "").length === 0,
schemeless: arg => arg.replace(/^.*:\/\//, "")
};
58 changes: 29 additions & 29 deletions lib/woodland.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class Woodland {
}

allowed (method, uri, host, override = false) {
return this.routes(uri, host, method, override).middleware.filter(i => !this.blacklisted.has(i.hash)).length > 0;
return this.routes(uri, host, method, override).middleware.filter(i => this.blacklisted.has(i.hash) === false).length > 0;
}

allows (uri, host, override = false) {
const key = this.hash(host + delimiter + uri);
let result = !override ? this.permissions.get(key) : void 0;
let result = override === false ? this.permissions.get(key) : void 0;

if (override || result === void 0) {
if (override === true || result === void 0) {
const list = methods.filter(i => this.allowed(i, uri, host, override)),
odx = list.indexOf("OPTIONS");

Expand All @@ -51,7 +51,7 @@ class Woodland {
}

blacklist (fn) {
if (!this.blacklisted.has(fn.hash)) {
if (this.blacklisted.has(fn.hash) === false) {
this.blacklisted.add(fn.hash);
}

Expand All @@ -77,7 +77,7 @@ class Woodland {
req.cors = req.headers.origin !== void 0 && utility.schemeless(req.headers.host) !== utility.schemeless(req.headers.origin);

// CORS handling
if (req.cors) {
if (req.cors === true) {
let headers = req.headers["access-control-request-headers"];

res.header("access-control-allow-origin", req.headers.origin);
Expand All @@ -86,15 +86,15 @@ class Woodland {
res.header(regex.options.test(req.method) ? "access-control-allow-headers" : "access-control-expose-headers", headers);
}

if (!utility.isEmpty(req.allow)) {
if (req.allow !== "") {
res.header("access-control-allow-methods", req.allow);
}
}

// Setting headers
retsu.each(this.defaultHeaders, header => res.header(header[0], header[1]));

if (!utility.isEmpty(req.allow)) {
if (req.allow !== "") {
res.header("allow", req.allow);
}
}
Expand All @@ -109,7 +109,7 @@ class Woodland {
retsu.each(this.hosts, (reg, host) => {
let output = true;

if (reg.test(arg)) {
if (reg.test(arg) === true) {
result = host;
output = false;
}
Expand Down Expand Up @@ -141,8 +141,8 @@ class Woodland {
onconnect () {}

onerror (req, res, err) {
const numeric = !isNaN(err.message),
status = !isNaN(res.statusCode) && res.statusCode >= 400 ? res.statusCode : numeric ? Number(err.message) : 500,
const numeric = isNaN(err.message) === false,
status = isNaN(res.statusCode) === false && res.statusCode >= 400 ? res.statusCode : numeric ? Number(err.message) : 500,
output = numeric ? http.STATUS_CODES[status] : err.message;

res.writeHead(status, {"content-type": "text/plain", "content-length": Buffer.byteLength(output)});
Expand All @@ -167,27 +167,27 @@ class Woodland {
const last = err => {
if (err === void 0) {
deferred.reject(new Error(regex.hasGet.test(req.allow || "") ? 405 : 404));
} else if (!isNaN(res.statusCode) && res.statusCode >= 400) {
} else if (isNaN(res.statusCode) === false && res.statusCode >= 400) {
deferred.reject(err);
} else {
deferred.reject(new Error(res.statusCode >= 400 ? res.statusCode : !isNaN(err.message) ? err.message : http.STATUS_CODES[err.message || err] || 500));
deferred.reject(new Error(res.statusCode >= 400 ? res.statusCode : isNaN(err.message) === false ? err.message : http.STATUS_CODES[err.message || err] || 500));
}
};

const next = err => {
process.nextTick(() => {
if (!res.headersSent) {
if (res.headersSent === false) {
let iter = middleware.next(),
arity = !iter.done ? utility.getArity(iter.value) : 0;
arity = iter.done === false ? utility.getArity(iter.value) : 0;

if (!iter.done) {
if (iter.done === false) {
if (err !== void 0) {
do {
arity = utility.getArity(iter.value);
} while (arity < 4 && (iter = middleware.next()) && !iter.done);
} while (arity < 4 && (iter = middleware.next()) && iter.done === false);
}

if (!iter.done) {
if (iter.done === false) {
if (err !== void 0 && arity === 4) {
try {
iter.value(err, req, res, next);
Expand Down Expand Up @@ -223,13 +223,13 @@ class Woodland {
this.decorate(req, res);
this.onconnect(req, res);

if (regex.options.test(method) && !this.allowed(method, req.parsed.pathname, req.host)) {
if (regex.options.test(method) === true && this.allowed(method, req.parsed.pathname, req.host) === false) {
method = "GET"; // Changing an OPTIONS request to GET due to absent route
}

result = this.routes(req.parsed.pathname, req.host, method);

if (result.params) {
if (result.params !== void 0) {
this.params(req, result.pos);
}

Expand All @@ -244,7 +244,7 @@ class Woodland {

routes (uri, host, method, override = false) {
const key = this.hash(method + delimiter + host + delimiter + uri),
cached = !override ? this.cache.get(key) : void 0;
cached = override === false ? this.cache.get(key) : void 0;
let result;

if (cached !== void 0) {
Expand All @@ -262,12 +262,12 @@ class Woodland {
let now = false,
valid;

if (regex.hasParam.test(route) && !regex.leftBrace.test(route) && !params) {
if (regex.hasParam.test(route) === true && regex.leftBrace.test(route) === false && params === false) {
params = true;
now = true;

retsu.each(route.replace(regex.startSlash, "").replace(regex.endSlash, "").split("/"), (i, idx) => {
if (regex.isParam.test(i)) {
if (regex.isParam.test(i) === true) {
pos.push([idx, i.replace(regex.isParam, "")]);
}
});
Expand All @@ -281,7 +281,7 @@ class Woodland {
valid = new RegExp("^" + utility.escape(route) + "$", "i").test(uri);
}

if (now && !valid) {
if (now === true && valid === false) {
params = false;
pos = [];
}
Expand All @@ -301,7 +301,7 @@ class Woodland {
}

setHost (arg) {
if (!this.hosts.has(arg)) {
if (this.hosts.has(arg) === false) {
this.hosts.set(arg, new RegExp("^" + arg.replace(/\*/g, ".*") + "$"));
}

Expand All @@ -328,27 +328,27 @@ class Woodland {
throw new TypeError("Invalid middleware");
}

if (!new RegExp(all, "i").test(method) && !retsu.contains(methods, method)) {
if (new RegExp(all, "i").test(method) === false && methods.includes(method) === false) {
throw new TypeError("Invalid HTTP method");
}

if (regex.head.test(method)) {
if (regex.head.test(method) === true) {
throw new TypeError("Cannot set HEAD route, use GET");
}

if (!this.middleware.has(host)) {
if (this.middleware.has(host) === false) {
this.middleware.set(host, new Map());
}

mhost = this.middleware.get(host);

if (!mhost.has(method)) {
if (mhost.has(method) === false) {
mhost.set(method, new Map());
}

mmethod = mhost.get(method);

if (!mmethod.has(rpath)) {
if (mmethod.has(rpath) === false) {
mmethod.set(rpath, []);
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "woodland",
"version": "2.2.2",
"version": "2.2.3",
"description": "Lightweight HTTP/HTTPS router with virtual hosts, and automatic `Allow` & `CORS` headers",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit dde9f53

Please sign in to comment.