Skip to content

Commit

Permalink
Changing use() to use rest parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed May 24, 2017
1 parent fe2647e commit c93e3a4
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions lib/woodland.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,52 +294,52 @@ class Woodland {
return this;
}

use (rpath, fn, method, host = all) {
let lpath = rpath,
lfn = fn,
lmethod = method,
lhost = host,
use (...args) {
let rpath = args[0],
fn = args[1],
method = args[2],
host = args[3] || all,
mhost, mmethod;

if (typeof lpath !== "string") {
lhost = lmethod || lhost;
lmethod = lfn;
lfn = lpath;
lpath = "/.*";
if (typeof rpath !== "string") {
host = method || host;
method = fn;
fn = rpath;
rpath = "/.*";
}

lmethod = lmethod !== undefined ? lmethod.toUpperCase() : "GET";
method = method !== undefined ? method.toUpperCase() : "GET";

if (typeof lfn !== "function") {
if (typeof fn !== "function") {
throw new Error("Invalid middleware");
}

if (!new RegExp(all, "i").test(lmethod) && !retsu.contains(methods, lmethod)) {
if (!new RegExp(all, "i").test(method) && !retsu.contains(methods, method)) {
throw new Error("Invalid HTTP method");
}

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

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

mhost = this.middleware.get(lhost);
mhost = this.middleware.get(host);

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

mmethod = mhost.get(lmethod);
mmethod = mhost.get(method);

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

lfn.hash = this.hash(lfn.toString());
mmethod.get(lpath).push(lfn);
fn.hash = this.hash(fn.toString());
mmethod.get(rpath).push(fn);

return this;
}
Expand Down

0 comments on commit c93e3a4

Please sign in to comment.