Skip to content

Instantly share code, notes, and snippets.

@brianleroux
Created January 4, 2018 22:15
Show Gist options
  • Save brianleroux/8a16652f99745c1b523807561f8dd6f5 to your computer and use it in GitHub Desktop.
Save brianleroux/8a16652f99745c1b523807561f8dd6f5 to your computer and use it in GitHub Desktop.
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