Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/for each to forloops #50

Merged
merged 13 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,24 @@ function Layer(path, methods, middleware, opts) {
this.paramNames = [];
this.stack = Array.isArray(middleware) ? middleware : [middleware];

methods.forEach(function(method) {
var l = this.methods.push(method.toUpperCase());
for(var i = 0; i < methods.length; i++) {
var l = this.methods.push(methods[i].toUpperCase());
if (this.methods[l-1] === 'GET') {
this.methods.unshift('HEAD');
this.methods.unshift('HEAD');
}
}, this);
}

// ensure middleware is a function
this.stack.forEach(function(fn) {
for (var i = 0; i < this.stack.length; i++) {
var fn = this.stack[i];
var type = (typeof fn);
if (type !== 'function') {
throw new Error(
methods.toString() + " `" + (this.opts.name || path) +"`: `middleware` "
+ "must be a function, not `" + type + "`"
);
}
}, this);
}

this.path = path;
this.regexp = pathToRegExp(path, this.paramNames, this.opts);
Expand Down
115 changes: 68 additions & 47 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,25 +187,28 @@ function Router(opts) {
* @returns {Router}
*/

methods.forEach(function (method) {
Router.prototype[method] = function (name, path, middleware) {
var middleware;

if (typeof path === 'string' || path instanceof RegExp) {
middleware = Array.prototype.slice.call(arguments, 2);
} else {
middleware = Array.prototype.slice.call(arguments, 1);
path = name;
name = null;
}
for (var i = 0; i < methods.length; i++) {
function setMethodVerb(method) {
Router.prototype[method] = function(name, path, middleware) {
var middleware;

if (typeof path === "string" || path instanceof RegExp) {
middleware = Array.prototype.slice.call(arguments, 2);
} else {
middleware = Array.prototype.slice.call(arguments, 1);
path = name;
name = null;
}

this.register(path, [method], middleware, {
name: name
});
this.register(path, [method], middleware, {
name: name
});

return this;
};
});
return this;
};
}
setMethodVerb(methods[i]);
}

// Alias for `router.delete()` because delete is a reserved word
Router.prototype.del = Router.prototype['delete'];
Expand Down Expand Up @@ -247,10 +250,11 @@ Router.prototype.use = function () {

// support array of paths
if (Array.isArray(middleware[0]) && typeof middleware[0][0] === 'string') {
middleware[0].forEach(function (p) {
var arrPaths = middleware[0];
for (var i = 0; i < arrPaths.length; i++) {
var p = arrPaths[i];
router.use.apply(router, [p].concat(middleware.slice(1)));
});

}
return this;
}

Expand All @@ -259,30 +263,40 @@ Router.prototype.use = function () {
path = middleware.shift();
}

middleware.forEach(function (m) {
for (var i = 0; i < middleware.length; i++) {
var m = middleware[i];
if (m.router) {
const cloneRouter = Object.assign(Object.create(Router.prototype), m.router, {
var cloneRouter = Object.assign(Object.create(Router.prototype), m.router, {
stack: m.router.stack.slice(0)
});

cloneRouter.stack.forEach(function (nestedLayer, index) {
const cloneLayer = Object.assign(Object.create(Layer.prototype), nestedLayer);
for (var i = 0; i < cloneRouter.stack.length; i++) {
var nestedLayer = cloneRouter.stack[i];
var cloneLayer = Object.assign(
Object.create(Layer.prototype),
nestedLayer
);

if (path) cloneLayer.setPrefix(path);
if (router.opts.prefix) cloneLayer.setPrefix(router.opts.prefix);
router.stack.push(cloneLayer);
this[index] = cloneLayer;
}, cloneRouter.stack);
cloneRouter.stack[i] = cloneLayer;
}

if (router.params) {
Object.keys(router.params).forEach(function (key) {
cloneRouter.param(key, router.params[key]);
});
function setRouterParams(paramArr) {
var routerParams = paramArr;
for (var i = 0; i < routerParams.length; i++) {
var key = routerParams[i];
cloneRouter.param(key, router.params[key]);
}
}
setRouterParams(Object.keys(router.params));
}
} else {
router.register(path || '(.*)', [], m, { end: false, ignoreCaptures: !hasPath });
}
});
}

return this;
};
Expand All @@ -305,10 +319,11 @@ Router.prototype.prefix = function (prefix) {

this.opts.prefix = prefix;

this.stack.forEach(function (route) {
for (var i = 0; i < this.stack.length; i++) {
var route = this.stack[i];
route.setPrefix(prefix);
});

}
return this;
};

Expand Down Expand Up @@ -415,11 +430,13 @@ Router.prototype.allowedMethods = function (options) {
var allowed = {};

if (!ctx.status || ctx.status === 404) {
ctx.matched.forEach(function (route) {
route.methods.forEach(function (method) {
allowed[method] = method;
});
});
for (var i = 0; i < ctx.matched.length; i++) {
var route = ctx.matched[i];
for (var j = 0; j < route.methods.length; j++) {
var method = route.methods[j];
allowed[method] = method
}
}

var allowedArr = Object.keys(allowed);

Expand Down Expand Up @@ -549,9 +566,10 @@ Router.prototype.register = function (path, methods, middleware, opts) {

// support array of paths
if (Array.isArray(path)) {
path.forEach(function (p) {
router.register.call(router, p, methods, middleware, opts);
});
for (var i = 0; i < path.length; i++) {
var curPath = path[i];
router.register.call(router, curPath, methods, middleware, opts);
}

return this;
}
Expand All @@ -571,9 +589,10 @@ Router.prototype.register = function (path, methods, middleware, opts) {
}

// add parameter middleware
Object.keys(this.params).forEach(function (param) {
for (var i = 0; i < Object.keys(this.params).length; i++) {
var param = Object.keys(this.params)[i];
route.param(param, this.params[param]);
}, this);
}

stack.push(route);

Expand Down Expand Up @@ -712,11 +731,13 @@ Router.prototype.match = function (path, method) {
* @returns {Router}
*/

Router.prototype.param = function (param, middleware) {
Router.prototype.param = function(param, middleware) {
this.params[param] = middleware;
this.stack.forEach(function (route) {
for (var i = 0; i < this.stack.length; i++) {
var route = this.stack[i];
route.param(param, middleware);
});
}

return this;
};

Expand All @@ -734,7 +755,7 @@ Router.prototype.param = function (param, middleware) {
* @param {Object} params url parameters
* @returns {String}
*/
Router.url = function (path, params) {
Router.url = function (path) {
var args = Array.prototype.slice.call(arguments, 1);
return Layer.prototype.url.apply({ path: path }, args);
};