From f89bc5510569284875f4ae10e02044b8b8e91d33 Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Sun, 7 Feb 2016 21:24:50 -0800 Subject: [PATCH] =?UTF-8?q?Refactor=20RouteNode=20=E2=86=92=20flatten=20fo?= =?UTF-8?q?r=20better=20readability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/route-node.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/route-node.js b/lib/route-node.js index 6c67816..957685e 100644 --- a/lib/route-node.js +++ b/lib/route-node.js @@ -48,27 +48,30 @@ assign(RouteNode.prototype, { get: function get(parts, args, done) { parts = parts || [] args = args || [] - if (parts.length === 0) { return done(null, this.handler, this.context, args) } + var part = parts.shift() - var childNode = this._children[part] - if (childNode) { - return childNode.get(parts, args, done) - } else { - var results = this._getRegexChild(part) - if (results && results.node) { - return results.node.get(parts, args.concat(results.args), done) - } else { - results = this._getFunctionChild(part) - if (results && results.node) { - return results.node.get(parts, args.concat(results.args), done) - } else { - return done(new Error('not found')) - } - } + + // Check exact matches + if (this._children[part]) { + return this._children[part].get(parts, args, done) } + + // Check regex matches + var results = this._getRegexChild(part) + if (results && results.node) { + return results.node.get(parts, args.concat(results.args), done) + } + + // Check function matches + results = this._getFunctionChild(part) + if (results && results.node) { + return results.node.get(parts, args.concat(results.args), done) + } + + return done(new Error('not found')) }, _getRegexChild: function _getRegexChild(part) {