6464 */
6565 var namedParam = '([\\w-]+)' ;
6666 this . namedParam = {
67- replace : new RegExp ( ': ' + namedParam , 'g' ) ,
68- match : namedParam
67+ match : new RegExp ( '{( ' + namedParam + ')}' , 'g' ) ,
68+ replace : namedParam
6969 } ;
7070
7171 options = options || { } ;
9494 * @return self
9595 */
9696 add : function ( route , callback ) {
97- this . routes . push ( {
97+ this . routes . push ( new Route ( {
9898 route : route ,
9999 callback : callback
100- } ) ;
100+ } , this ) ) ;
101101 return this ;
102102 } ,
103103
150150 return this ;
151151 } ,
152152
153- /**
154- * Converts the given route string to a regex, suitable for matching against.
155- * @param string route
156- * @return RegExp
157- */
158- regexRoute : function ( route ) {
159- if ( typeof route === 'string' )
160- {
161- return new RegExp ( '^' + route . replace ( / \/ / g, '\\/' ) . replace ( this . namedParam . replace , this . namedParam . match ) + '$' ) ;
162- }
163- return route ;
164- } ,
165-
166153 /**
167154 * Gets the url to test the routes against
168155 * @return self
169156 */
170157 getUrl : function ( routeType ) {
171158
172159 var url ;
173-
174- if ( routeType === undefined )
175- {
176- routeType = this . type ;
177- }
160+ routeType = routeType || this . type ;
178161
179162 if ( routeType == 'path' )
180163 {
195178 * @return self
196179 */
197180 run : function ( ) {
198- var url = this . getUrl ( ) , i , matched , routeOptions , routeRegex ;
181+ var url = this . getUrl ( ) , route ;
199182
200- for ( i in this . routes )
183+ for ( var i in this . routes )
201184 {
202- routeOptions = this . routes [ i ] ;
203- routeRegex = this . regexRoute ( routeOptions . route ) ;
204- matched = url . match ( routeRegex ) ;
205-
206- if ( matched )
207- {
208- routeOptions . callback . apply ( undefined , matched . slice ( 1 ) ) ;
209- }
185+ // Get the route
186+ route = this . routes [ i ] ;
187+
188+ // Test and run the route if it matches
189+ route . test ( url ) && route . run ( ) ;
210190 }
211191 return this ;
212192 }
213193 } ;
214194
195+
196+ /**
197+ * Route object
198+ * @param {object } options Options passed to the route
199+ * @param {LightRouter } router Instance of the light router the route belongs to.
200+ */
201+ function Route ( options , router )
202+ {
203+ this . options = options ;
204+ this . router = router ;
205+ this . values = [ ] ;
206+ }
207+
208+ Route . prototype = {
209+
210+ /**
211+ * Converts route to a regex (if required) so that it's suitable for matching against.
212+ * @param string route
213+ * @return RegExp
214+ */
215+ regex : function ( ) {
216+
217+ var route = this . options . route ;
218+
219+ if ( typeof route === 'string' )
220+ {
221+ return new RegExp ( '^' + route . replace ( / \/ / g, '\\/' ) . replace ( this . router . namedParam . match , this . router . namedParam . replace ) + '$' ) ;
222+ }
223+ return route ;
224+ } ,
225+
226+ /**
227+ * Get the matching param keys
228+ * @return object Object keyed with param name (or index) with the value.
229+ */
230+ params : function ( ) {
231+
232+ var obj = { } , name , values = this . values , params = values , i , t = 0 , route = this . options . route ;
233+
234+ if ( typeof route === 'string' )
235+ {
236+ t = 1 ;
237+ params = route . match ( this . router . namedParam . match ) ;
238+ }
239+
240+ for ( i in params )
241+ {
242+ name = t ? params [ i ] . replace ( this . router . namedParam . match , '$1' ) : i ;
243+ obj [ name ] = values [ i ] ;
244+ }
245+
246+ return obj ;
247+ } ,
248+
249+ /**
250+ * Test the route to see if it matches
251+ * @param {string } url Url to match against
252+ * @return {boolean }
253+ */
254+ test : function ( url ) {
255+ var matches ;
256+ if ( matches = url . match ( this . regex ( ) ) )
257+ {
258+ this . values = matches . slice ( 1 ) ;
259+ return true ;
260+ }
261+ return false ;
262+ } ,
263+
264+ /**
265+ * Run the route callback with the matched params
266+ * @return {mixed }
267+ */
268+ run : function ( ) {
269+ return this . options . callback . apply ( undefined , [ this . params ( ) ] ) ;
270+ }
271+ } ;
272+
215273 return LightRouter ;
216274
217275} ) ) ;
0 commit comments