Created
December 18, 2018 09:56
-
-
Save evanxg852000/c4bb44d90922151c24dfe4fa79aa15d0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Router { | |
... | |
_patternToRegex(pattern){ | |
let regex = '' | |
let params = [] | |
let parts = pattern.split('/') | |
for(let part of parts){ | |
if(part.trim() === '') | |
continue | |
if(part.startsWith(':')){ | |
if(part.endsWith('?')){ | |
regex += '(/([_a-zA-Z0-9\\-]+))?' | |
} else { | |
regex += '/([_a-zA-Z0-9\\-]+)' | |
} | |
params.push(part.replace(/(\?|:)/g, '')) | |
continue | |
} | |
regex += `/${part}` | |
} | |
// Handle special case (web root) | |
if(regex === ''){ | |
regex = '/' | |
} | |
regex = new RegExp(`^${regex}$`) | |
return {regex , params} | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment