Created
July 26, 2020 09:40
-
-
Save nsivertsen/60a0f83b156ac3d128c79d71c5b1dfb3 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
function match(pattern, path) { | |
const pattern_components = pattern.split('/') | |
const path_components = path.split('/') | |
if (pattern_components.length !== path_components.length) { | |
return null | |
} | |
const params = {} | |
for (let i = 0; i < pattern_components.length; i++) { | |
const pattern_component = pattern_components[i] | |
const path_component = path_components[i] | |
const is_var = pattern_component.startsWith(':') | |
if (is_var) { | |
const key = pattern_component.slice(1) | |
params[key] = path_component | |
} else { | |
if (pattern_component !== path_component) { | |
return null | |
} | |
} | |
} | |
return params | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment