Created
January 4, 2018 22:15
-
-
Save brianleroux/8a16652f99745c1b523807561f8dd6f5 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
function interpolateParams(req) { | |
var params = /\{\w+\}/g | |
if (params.test(req.path)) { | |
var matches = req.path.match(params) | |
var vars = matches.map(a=> a.replace(/\{|\}/g, '')) | |
var idx = 0 | |
matches.forEach(m=> { | |
req.path = req.path.replace(m, req.params[vars[idx]]) | |
idx += 1 | |
}) | |
} | |
return req | |
} | |
var r = { | |
path: '/foo/{bar}/baz/{foobaz}', | |
params: {bar: 'barValue', foobaz:'foobazValue'} | |
} | |
console.log(interpolateParams(r)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment