replaceParams(href, params) substitutes named :name placeholders
in a route pattern with URL-encoded values from params. Placeholders
are matched by paramsRegExp
(/\:([a-z0-9_\-]+)/gi). Keys missing from params are left intact,
so partial substitution works. This is the same primitive
<A/> uses internally. Part of
pota/use/url.
| Argument | Type | Description |
|---|---|---|
href |
string |
The route pattern containing :name placeholders. |
params |
object |
Optional. Key-value pairs; each value replaces its :key. |
Returns: the URL with matched params replaced by their
encodeURIComponent form. When params is omitted, href is
returned unchanged.
Replaces :id and :page with encoded values from the params object.
import { replaceParams } from 'pota/use/url'
replaceParams('/users/:id/posts/:page', { id: 7, page: 'latest' })
// '/users/7/posts/latest'
Keys absent from params keep their placeholder, so you can fill in a
URL across several passes.
import { replaceParams } from 'pota/use/url'
replaceParams('/users/:id/posts/:page', { id: 7 })
// '/users/7/posts/:page'