Skip to main content

Url

URL parsing and query-string editing built on the platform URL — hostname, params, joining segments, setting and removing query parameters. Delegating to URL means percent-encoding, internationalised domains and relative resolution are the platform's problem rather than a regex's; isValidUrl is literally an attempted new URL(). joinUrl is the exception — plain string joining with slash normalisation, for assembling a path before you have a base.

Example

import { getQueryParams, joinUrl, setQueryParam } from '@rtorcato/js-common/url'

joinUrl('https://api.example.com/', '/v2/', 'users') // 'https://api.example.com/v2/users'

// Pagination without hand-rolling query-string surgery.
setQueryParam('https://x.dev/search?q=cats', 'page', '2')
// 'https://x.dev/search?q=cats&page=2'

getQueryParams('https://x.dev/search?q=cats&page=2') // { q: 'cats', page: '2' }

Everything except joinUrl goes through the platform URL, and returns the input unchanged (or an empty result) when it will not parse.

Import

import { getHostname, getQueryParams, isValidUrl } from '@rtorcato/js-common/url'

Exports

NameSummary
getHostnameGets the hostname from a URL string.
getQueryParamsGets the query parameters from a URL as an object.
isValidUrlChecks if a string is a valid URL.
joinUrlJoins multiple URL segments into a single URL, ensuring proper slashes.
removeQueryParamRemoves a query parameter from a URL.
setQueryParamAdds or updates a query parameter in a URL.

See also

  • validation — type guards — isString, isNumber, isUrl
  • security — password strength, secure tokens, sanitizing
  • emails — validate, normalize and mask email addresses
  • fetch — JSON helpers and fetch with a timeout