Created
August 2, 2014 17:12
-
-
Save kevinzhow/4c372ddd8b7b13a11b06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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