Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added 'use strict'

  • Loading branch information...
commit 197412c998ddef0a91d4460ea2bbb792a8a79493 1 parent 0ee2628
Vadim Baryshev authored
Showing with 94 additions and 91 deletions.
  1. +94 −91 lib/connect-route.js
185 lib/connect-route.js
View
@@ -1,111 +1,114 @@
-var
- methods = [ 'get', 'post', 'put', 'delete', 'connect', 'options', 'trace', 'copy', 'lock', 'mkcol', 'move', 'propfind', 'proppatch', 'unlock', 'report', 'mkactivity', 'checkout', 'merge' ],
- separator = /^[\s\/]+|[\s\/]+$/g,
- i, length,
+(function () {
+ 'use strict';
+ var
+ methods = [ 'get', 'post', 'put', 'delete', 'connect', 'options', 'trace', 'copy', 'lock', 'mkcol', 'move', 'propfind', 'proppatch', 'unlock', 'report', 'mkactivity', 'checkout', 'merge' ],
+ separator = /^[\s\/]+|[\s\/]+$/g,
+ i, length,
- createMethodHandler = function (method) {
- method = method.toUpperCase();
- return function () {
- var i;
- for (i = 0; i < arguments.length - 1; i++) {
- this.add(method, arguments[i], arguments[arguments.length - 1]);
- }
- };
- },
+ createMethodHandler = function (method) {
+ method = method.toUpperCase();
+ return function () {
+ var i;
+ for (i = 0; i < arguments.length - 1; i++) {
+ this.add(method, arguments[i], arguments[arguments.length - 1]);
+ }
+ };
+ },
- Router = function () {
- this.routes = {};
- };
+ Router = function () {
+ this.routes = {};
+ };
-Router.prototype.add = function (method, route, handler) {
- var parts, current, i, length, name;
+ Router.prototype.add = function (method, route, handler) {
+ var parts, current, i, length, name;
- if (typeof handler !== 'function') { return; }
- if (!this.routes[method]) { this.routes[method] = { childs: {}, handler: undefined, route: undefined }; }
+ if (typeof handler !== 'function') { return; }
+ if (!this.routes[method]) { this.routes[method] = { childs: {}, handler: undefined, route: undefined }; }
- parts = route.split('?', 1)[0].replace(separator, '').split('/');
+ parts = route.split('?', 1)[0].replace(separator, '').split('/');
- if (!parts[0].length) {
- this.routes[method].handler = handler;
- this.routes[method].route = route;
- } else {
- current = this.routes[method];
- for (i = 0, length = parts.length; i < length; i++) {
- name = undefined;
- if (parts[i].charAt(0) === ':') {
- name = parts[i].substr(1);
- parts[i] = '*';
- }
- if ((!current.childs[parts[i]] && (current.childs[parts[i]] = {})) || (parts[i] === '*' && !current.childs[parts[i]][length])) {
+ if (!parts[0].length) {
+ this.routes[method].handler = handler;
+ this.routes[method].route = route;
+ } else {
+ current = this.routes[method];
+ for (i = 0, length = parts.length; i < length; i++) {
+ name = undefined;
+ if (parts[i].charAt(0) === ':') {
+ name = parts[i].substr(1);
+ parts[i] = '*';
+ }
+ if ((!current.childs[parts[i]] && (current.childs[parts[i]] = {})) || (parts[i] === '*' && !current.childs[parts[i]][length])) {
+ if (parts[i] === '*') {
+ current.childs[parts[i]][length] = { handler: undefined, route: undefined, childs: {}, name: name };
+ } else {
+ current.childs[parts[i]] = { handler: undefined, route: undefined, childs: {}, name: name };
+ }
+ }
if (parts[i] === '*') {
- current.childs[parts[i]][length] = { handler: undefined, route: undefined, childs: {}, name: name };
+ current = current.childs[parts[i]][length];
} else {
- current.childs[parts[i]] = { handler: undefined, route: undefined, childs: {}, name: name };
+ current = current.childs[parts[i]];
}
}
- if (parts[i] === '*') {
- current = current.childs[parts[i]][length];
- } else {
- current = current.childs[parts[i]];
- }
+ current.handler = handler;
+ current.route = route;
}
- current.handler = handler;
- current.route = route;
- }
-};
+ };
-Router.prototype.match = function (method, url) {
- var
- parts = url.split('?', 1)[0].replace(separator, '').split('/'),
- result = { handler: undefined, route: undefined, params: {} },
- current, i, length;
+ Router.prototype.match = function (method, url) {
+ var
+ parts = url.split('?', 1)[0].replace(separator, '').split('/'),
+ result = { handler: undefined, route: undefined, params: {} },
+ current, i, length;
- if (this.routes[method]) {
- if (!parts[0].length) {
- result.handler = this.routes[method].handler;
- result.route = this.routes[method].route;
- } else {
- current = this.routes[method];
- for (i = 0, length = parts.length; i < length; i++) {
- if (current.childs[parts[i]]) {
- current = current.childs[parts[i]];
- result.handler = current.handler;
- result.route = current.route;
- } else if (current.childs['*'] && current.childs['*'][length]) {
- current = current.childs['*'][length];
- result.handler = current.handler;
- result.route = current.route;
- result.params[current.name] = parts[i];
- } else {
- result.handler = undefined;
- result.route = undefined;
- result.params = {};
- break;
+ if (this.routes[method]) {
+ if (!parts[0].length) {
+ result.handler = this.routes[method].handler;
+ result.route = this.routes[method].route;
+ } else {
+ current = this.routes[method];
+ for (i = 0, length = parts.length; i < length; i++) {
+ if (current.childs[parts[i]]) {
+ current = current.childs[parts[i]];
+ result.handler = current.handler;
+ result.route = current.route;
+ } else if (current.childs['*'] && current.childs['*'][length]) {
+ current = current.childs['*'][length];
+ result.handler = current.handler;
+ result.route = current.route;
+ result.params[current.name] = parts[i];
+ } else {
+ result.handler = undefined;
+ result.route = undefined;
+ result.params = {};
+ break;
+ }
}
}
}
- }
- return result;
-};
+ return result;
+ };
-for (i = 0, length = methods.length; i < length; i++) {
- Router.prototype[methods[i]] = createMethodHandler(methods[i]);
-}
+ for (i = 0, length = methods.length; i < length; i++) {
+ Router.prototype[methods[i]] = createMethodHandler(methods[i]);
+ }
-module.exports = function (cb) {
- var router = new Router();
- if (typeof cb === 'function') { cb(router); }
+ module.exports = function (cb) {
+ var router = new Router();
+ if (typeof cb === 'function') { cb(router); }
- return function (req, res, next) {
- var action = router.match(req.method, req.url);
- if (action.handler) {
- req.route = action.route;
- req.params = action.params;
- action.handler(req, res, next);
- } else {
- req.route = undefined;
- req.params = {};
- next();
- }
+ return function (req, res, next) {
+ var action = router.match(req.method, req.url);
+ if (action.handler) {
+ req.route = action.route;
+ req.params = action.params;
+ action.handler(req, res, next);
+ } else {
+ req.route = undefined;
+ req.params = {};
+ next();
+ }
+ };
};
-};
+}());
Please sign in to comment.
Something went wrong with that request. Please try again.