From f299b39879bff74dc288214a644521591585243b Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 1 Oct 2014 17:14:07 +0100 Subject: [PATCH] Latest dist --- dist/lightrouter.js | 122 +++++++++++++++++++++++++++++----------- dist/lightrouter.min.js | 2 +- 2 files changed, 91 insertions(+), 33 deletions(-) diff --git a/dist/lightrouter.js b/dist/lightrouter.js index a008ae3..b3217d2 100644 --- a/dist/lightrouter.js +++ b/dist/lightrouter.js @@ -64,8 +64,8 @@ */ var namedParam = '([\\w-]+)'; this.namedParam = { - replace: new RegExp(':' + namedParam, 'g'), - match: namedParam + match: new RegExp('{(' + namedParam + ')}', 'g'), + replace: namedParam }; options = options || {}; @@ -94,10 +94,10 @@ * @return self */ add: function(route, callback) { - this.routes.push({ + this.routes.push(new Route({ route: route, callback: callback - }); + }, this)); return this; }, @@ -150,19 +150,6 @@ return this; }, - /** - * Converts the given route string to a regex, suitable for matching against. - * @param string route - * @return RegExp - */ - regexRoute: function(route) { - if (typeof route === 'string') - { - return new RegExp('^' + route.replace(/\//g, '\\/').replace(this.namedParam.replace, this.namedParam.match) + '$'); - } - return route; - }, - /** * Gets the url to test the routes against * @return self @@ -170,11 +157,7 @@ getUrl: function(routeType) { var url; - - if (routeType === undefined) - { - routeType = this.type; - } + routeType = routeType || this.type; if (routeType == 'path') { @@ -195,23 +178,98 @@ * @return self */ run: function() { - var url = this.getUrl(), i, matched, routeOptions, routeRegex; + var url = this.getUrl(), route; - for (i in this.routes) + for (var i in this.routes) { - routeOptions = this.routes[i]; - routeRegex = this.regexRoute(routeOptions.route); - matched = url.match(routeRegex); - - if (matched) - { - routeOptions.callback.apply(undefined, matched.slice(1)); - } + // Get the route + route = this.routes[i]; + + // Test and run the route if it matches + route.test(url) && route.run(); } return this; } }; + + /** + * Route object + * @param {object} options Options passed to the route + * @param {LightRouter} router Instance of the light router the route belongs to. + */ + function Route(options, router) + { + this.options = options; + this.router = router; + this.values = []; + } + + Route.prototype = { + + /** + * Converts route to a regex (if required) so that it's suitable for matching against. + * @param string route + * @return RegExp + */ + regex: function() { + + var route = this.options.route; + + if (typeof route === 'string') + { + return new RegExp('^' + route.replace(/\//g, '\\/').replace(this.router.namedParam.match, this.router.namedParam.replace) + '$'); + } + return route; + }, + + /** + * Get the matching param keys + * @return object Object keyed with param name (or index) with the value. + */ + params: function() { + + var obj = {}, name, values = this.values, params = values, i, t = 0, route = this.options.route; + + if (typeof route === 'string') + { + t = 1; + params = route.match(this.router.namedParam.match); + } + + for (i in params) + { + name = t ? params[i].replace(this.router.namedParam.match, '$1') : i; + obj[name] = values[i]; + } + + return obj; + }, + + /** + * Test the route to see if it matches + * @param {string} url Url to match against + * @return {boolean} + */ + test: function(url) { + var matches; + if (matches = url.match(this.regex())) + { + this.values = matches.slice(1); + return true; + } + return false; + }, + + /** + * Run the route callback with the matched params + * @return {mixed} + */ + run: function() { + return this.options.callback.apply(undefined, [this.params()]); + } + }; + return LightRouter; })); \ No newline at end of file diff --git a/dist/lightrouter.min.js b/dist/lightrouter.min.js index 116b2de..4006476 100644 --- a/dist/lightrouter.min.js +++ b/dist/lightrouter.min.js @@ -1 +1 @@ -/* lightrouter.js - Copyright 2014 Gary Green. Licensed under the Apache License, Version 2.0 */!function(t){"undefined"!=typeof exports?module.exports=t():window.LightRouter=t(window)}(function(t){function e(t){this.pathRoot="",this.routes=[],this.type="path",this.path=null,this.hash=null;var e="([\\w-]+)";if(this.namedParam={replace:new RegExp(":"+e,"g"),match:e},t=t||{},t.type&&this.setType(t.type),t.path&&this.setPath(t.path),t.pathRoot&&this.setPathRoot(t.pathRoot),t.hash&&this.setHash(t.hash),t.routes){var h;for(h in t.routes)this.add(h,t.routes[h])}}return e.prototype={add:function(t,e){return this.routes.push({route:t,callback:e}),this},empty:function(){return this.routes=[],this},setType:function(t){return this.type=t,this},setPathRoot:function(t){return this.pathRoot=t,this},setPath:function(t){return this.path=t,this},setHash:function(t){return this.hash=t,this},regexRoute:function(t){return"string"==typeof t?new RegExp("^"+t.replace(/\//g,"\\/").replace(this.namedParam.replace,this.namedParam.match)+"$"):t},getUrl:function(e){var h;if(void 0===e&&(e=this.type),"path"==e){var s=new RegExp("^"+this.pathRoot+"/?");h=this.path||t.location.pathname.substring(1),h=h.replace(s,"")}else"hash"==e&&(h=this.hash||t.location.hash.substring(1));return decodeURI(h)},run:function(){var t,e,h,s,i=this.getUrl();for(t in this.routes)h=this.routes[t],s=this.regexRoute(h.route),e=i.match(s),e&&h.callback.apply(void 0,e.slice(1));return this}},e}); \ No newline at end of file +/* lightrouter.js - Copyright 2014 Gary Green. Licensed under the Apache License, Version 2.0 */!function(t){"undefined"!=typeof exports?module.exports=t():window.LightRouter=t(window)}(function(t){function e(t){this.pathRoot="",this.routes=[],this.type="path",this.path=null,this.hash=null;var e="([\\w-]+)";if(this.namedParam={match:new RegExp("{("+e+")}","g"),replace:e},t=t||{},t.type&&this.setType(t.type),t.path&&this.setPath(t.path),t.pathRoot&&this.setPathRoot(t.pathRoot),t.hash&&this.setHash(t.hash),t.routes){var s;for(s in t.routes)this.add(s,t.routes[s])}}function s(t,e){this.options=t,this.router=e,this.values=[]}return e.prototype={add:function(t,e){return this.routes.push(new s({route:t,callback:e},this)),this},empty:function(){return this.routes=[],this},setType:function(t){return this.type=t,this},setPathRoot:function(t){return this.pathRoot=t,this},setPath:function(t){return this.path=t,this},setHash:function(t){return this.hash=t,this},getUrl:function(e){var s;if(e=e||this.type,"path"==e){var r=new RegExp("^"+this.pathRoot+"/?");s=this.path||t.location.pathname.substring(1),s=s.replace(r,"")}else"hash"==e&&(s=this.hash||t.location.hash.substring(1));return decodeURI(s)},run:function(){var t,e=this.getUrl();for(var s in this.routes)t=this.routes[s],t.test(e)&&t.run();return this}},s.prototype={regex:function(){var t=this.options.route;return"string"==typeof t?new RegExp("^"+t.replace(/\//g,"\\/").replace(this.router.namedParam.match,this.router.namedParam.replace)+"$"):t},params:function(){var t,e,s={},r=this.values,h=r,i=0,a=this.options.route;"string"==typeof a&&(i=1,h=a.match(this.router.namedParam.match));for(e in h)t=i?h[e].replace(this.router.namedParam.match,"$1"):e,s[t]=r[e];return s},test:function(t){var e;return(e=t.match(this.regex()))?(this.values=e.slice(1),!0):!1},run:function(){return this.options.callback.apply(void 0,[this.params()])}},e}); \ No newline at end of file