Skip to content

Instantly share code, notes, and snippets.

@kevinzhow
Created August 2, 2014 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinzhow/4c372ddd8b7b13a11b06 to your computer and use it in GitHub Desktop.
Save kevinzhow/4c372ddd8b7b13a11b06 to your computer and use it in GitHub Desktop.
recursive_route_convert = (_value, _key = '/', _stack = ['/'], _strict=false) ->
return _.flatten(_.map(_value, (__value, __key) ->
__stack = _.clone(_stack)
__stack.push(__key)
if _.isFunction(__value)
http_verb = __stack.splice(__stack.length - 2, 1)[0].toLowerCase()
if http_verb not in ['options','get','head','post','put','delete','trace','connect','patch']
throw(new Error("#{http_verb} is not in the HTTP Methods white list"))
else
strict_url = __stack.join('/').replace(/\/+/g,'/')
loose_url = strict_url.replace(/(\b\/$)/g,'')
if _strict then return [[http_verb, strict_url, __value]] else return [[http_verb, loose_url, __value],[http_verb, loose_url+'/', __value]]
else if _.isObject(__value) and not _.isEmpty(__value)
return recursive_route_convert(__value, __key, __stack, _strict)
else
throw(new Error('Invalid route map: Unexpected structure or Empty Object'))
), true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment