From 33048b2d4d789bdede0914f46a965779da541899 Mon Sep 17 00:00:00 2001 From: Billouboq Date: Sat, 20 May 2017 13:55:02 +0200 Subject: [PATCH] optimize lookup add comment for charCode --- index.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index befd77e7..963349a3 100644 --- a/index.js +++ b/index.js @@ -132,9 +132,7 @@ Router.prototype._insert = function (method, path, kind, params, handler, store) } Router.prototype.lookup = function (req, res) { - var i = 0 - while (i < req.url.length && req.url.charCodeAt(i) !== 63 /* ? */ && req.url.charCodeAt(i) !== 35 /* # */) i++ - var handle = this.find(req.method, req.url.slice(0, i)) + var handle = this.find(req.method, sanitizeUrl(req.url)) if (!handle) return this._defaultRoute(req, res) return handle.handler(req, res, handle.params, handle.store) } @@ -231,3 +229,13 @@ Router.prototype._defaultRoute = function (req, res) { } module.exports = Router + +function sanitizeUrl (url) { + for (var i = 0; i < url.length; i++) { + var charCode = url.charCodeAt(i) + if (charCode === 63 /* ? */ || charCode === 35 /* # */) { + return url.slice(0, i) + } + } + return url +}