Utilities
URL
Create, resolve, and modify URLs without implicit mutation.
URL
The URL utilities use the Web URL standard exclusively. They do not manipulate file-system paths.
toURL()
const absolute = toURL('https://example.com/docs')
const relative = toURL('/users/1', 'https://api.example.com')
A relative URL requires a base. Passing a URL instance still returns a copy.
resolveURL()
const endpoint = resolveURL('https://api.example.com/v1/', 'users', '42')
// https://api.example.com/v1/users/42
Resolution follows the standard URL constructor rules, including the effect of a trailing slash on the base.
withURLQuery()
const url = withURLQuery('https://example.com/search', {
page: 2,
exact: true,
tag: ['runtime', 'javascript'],
obsolete: null
})
| Value | Behavior |
|---|---|
string | Added directly |
number or boolean | Converted with String() |
| Array | Produces repeated parameters |
null or undefined | Removes the parameter |
Existing parameters with the same name are removed before new values are added. Every utility returns a new instance and leaves its input unchanged. Invalid URLs throw native URL constructor errors.