Skip to main content

@rtorcato/js-common / url

url

Functions​

isValidUrl()​

isValidUrl(str): boolean

Defined in: url/index.ts:13

Checks if a string is a valid URL.

Example​

isValidUrl('https://example.com/a?b=1') // true
isValidUrl('example.com') // false (no protocol)

Parameters​

str​

string

The string to check.

Returns​

boolean

True if valid URL, false otherwise.


getQueryParams()​

getQueryParams(url): Record<string, string>

Defined in: url/index.ts:29

Gets the query parameters from a URL as an object.

Example​

getQueryParams('https://example.com/s?q=cats&page=2') // { q: 'cats', page: '2' }
getQueryParams('not a url') // {}

Parameters​

url​

string

The URL string.

Returns​

Record<string, string>

An object of query parameters.


setQueryParam()​

setQueryParam(url, key, value): string

Defined in: url/index.ts:58

Adds or updates a query parameter in a URL.

Example​

setQueryParam('https://example.com/s?q=cats', 'page', '2')
// 'https://example.com/s?q=cats&page=2'
setQueryParam('https://example.com/s?q=cats', 'q', 'dogs')
// 'https://example.com/s?q=dogs'

Parameters​

url​

string

The URL string.

key​

string

The query parameter key.

value​

string

The query parameter value.

Returns​

string

The updated URL string.


removeQueryParam()​

removeQueryParam(url, key): string

Defined in: url/index.ts:74

Removes a query parameter from a URL.

Parameters​

url​

string

The URL string.

key​

string

The query parameter key to remove.

Returns​

string

The updated URL string.


joinUrl()​

joinUrl(...parts): string

Defined in: url/index.ts:103

Joins multiple URL segments into a single URL, ensuring proper slashes.

Parameters​

parts​

...string[]

The URL segments.

Returns​

string

The joined URL string.


getHostname()​

getHostname(url): string

Defined in: url/index.ts:115

Gets the hostname from a URL string.

Parameters​

url​

string

The URL string.

Returns​

string

The hostname, or an empty string if invalid.