{ kindof, mixin } = require './helpers'
{ kindof, mixin } = require './helpers'
exports.Key =
class Key
regex: /[\w\-\s]+/
constructor: ( @name, @optional )->
special defaults for controllers & actions, which will always be function-name-safe
if @name == 'controller' || @name == 'action'
@regex = /[a-zA-Z_][\w\-]*/
regexString: ->
ret = ['(']
ret.push @regex.source
ret.push ')'
ret.push '?' if @optional
ret.join ''
test: ( string )-> # this regex test passes for null & undefined :-(
new RegExp("^#{@regexString()}$").test string
returns a string for building the url if it matches the key conditions
url: ( string )->
if @test(string) then string else false
where: ( conditions )->
condition = conditions[@name]
if condition instanceof RegExp
@regex = condition # e.g. /\d+/
if condition instanceof String
@regex = new RegExp condition # e.g. "\d+"
an array of allowed values, e.g. [‘stop’,’play’,’pause’]
if condition instanceof Array
ret = []
for c in condition
ret.push c.source if 'regex' == kindof c
ret.push c if 'string' == kindof c
@regex = new RegExp ret.join '|'
this # chainable
returns the original key definition
toString: ->
":#{@name}"
@regex = /:([a-zA-Z_][\w\-]*)/
@parse = ( string, optional=false )->
[ definition, name ] = @regex.exec string
new @ name, optional
exports.Glob =
class Glob extends Key
regex: /[\w\-\/\s]+?/ # default url-friendly regex
constructor: ( @name, @optional )->
special defaults for controllers & actions, which will always be function-name-safe
if @name == 'controller' || @name == 'action'
@regex = /[a-zA-Z_][\w\-]*/
returns the original glob definition
toString: ->
"*#{@name}"
@regex = /\*([a-zA-Z_][\w\-]*)/