Skip to content

Instantly share code, notes, and snippets.

@delvedor
Created April 2, 2017 08:04
Show Gist options
  • Save delvedor/0a9d8f8383e6e6bf76418678df194c9e to your computer and use it in GitHub Desktop.
Save delvedor/0a9d8f8383e6e6bf76418678df194c9e to your computer and use it in GitHub Desktop.
/*
/some/:id/path => /some/{id}/path
/another/:example/for/:you => /another/{example}/for/{you}
*/
function formatParamUrl (url) {
var start = url.indexOf('/:')
if (start === -1) return url
var end = url.indexOf('/', ++start)
if (end === -1) {
return url.slice(0, start) + '{' + url.slice(++start) + '}'
} else {
return formatParamUrl(url.slice(0, start) + '{' + url.slice(++start, end) + '}' + url.slice(end))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment