Skip to content

url

Utility functions for building, encoding, and manipulating URLs.

build

Signature

js
url.build (baseUrl, params)

Description

Builds a URL from a base URL and a params object. Only defined values are appended as query parameters.

Parameters

NameTypeRequiredDescription
baseUrlstringyesThe base URL
paramsobjectyesQuery parameters to append

Returns

TypeDescription
stringThe constructed URL with query parameters

Throws

Throws a TypeError if baseUrl is not a valid URL or if params is not a non-empty object.

Examples

js
url.build('https://api.example.com/search', { q: 'hello', page: 1 })
// 'https://api.example.com/search?q=hello&page=1'

url.build('https://api.example.com', { q: 'hello', filter: undefined })
// 'https://api.example.com/?q=hello' — undefined values are ignored

parse

Signature

js
url.parse (url, defaultPort)

Description

Parses a URL and extracts its host, port and path. If no port is present in the URL, defaultPort is used as fallback.

Parameters

NameTypeRequiredDescription
urlstringyesThe URL to parse
defaultPortnumbernoFallback port when none is specified in the URL (default: 80)

Returns

TypeDescription
{ host: string, port: number, path: string }The parsed URL components

Throws

Throws a TypeError if url is not a valid URL or if defaultPort is not a number.

Examples

js
url.parse('https://example.com:3000/api/users')
// { host: 'example.com', port: 3000, path: '/api/users' }

url.parse('https://example.com/api/users')
// { host: 'example.com', port: 80, path: '/api/users' } — defaultPort used as fallback

url.parse('https://example.com/api/users', 443)
// { host: 'example.com', port: 443, path: '/api/users' }

addQueryParam

Signature

js
url.addQueryParam (url, params)

Description

Appends query parameters to an existing URL. Existing parameters are preserved. Only defined values are added. If a key already exists, its value is overwritten.

Parameters

NameTypeRequiredDescription
urlstringyesThe URL to extend
paramsobjectyesQuery parameters to append

Returns

TypeDescription
stringThe URL with the additional query parameters

Throws

Throws a TypeError if url is not a valid URL or if params is not a non-empty object.

Examples

js
url.addQueryParam('https://api.example.com?q=hello', { page: 2 })
// 'https://api.example.com/?q=hello&page=2'

url.addQueryParam('https://api.example.com?page=1', { page: 2 })
// 'https://api.example.com/?page=2' — existing key is overwritten

encode

Signature

js
url.encode (url)

Description

Encodes a URL using encodeURI, making it safe for use in HTTP requests. Characters that have special meaning in URLs (such as spaces) are percent-encoded.

Parameters

NameTypeRequiredDescription
urlstringyesThe URL to encode

Returns

TypeDescription
stringThe encoded URL

Throws

Throws a TypeError if url is not a valid URL.

Examples

js
url.encode('https://example.com/path with spaces')
// 'https://example.com/path%20with%20spaces'

obfuscate

Signature

js
url.obfuscate (url, mask = '*****')

Description

Replaces the username and password in a URL with a mask string. Useful for safely logging URLs that contain credentials.

Parameters

NameTypeRequiredDescription
urlstringyesThe URL to obfuscate
maskstringnoThe string to replace credentials with. Defaults to '*****'

Returns

TypeDescription
stringThe URL with credentials replaced by the mask

Throws

Throws a TypeError if url is not a valid URL or if mask is not a string.

Examples

js
url.obfuscate('https://user:password@example.com')
// 'https://*****:*****@example.com'

url.obfuscate('https://user:password@example.com', '###')
// 'https://###:###@example.com'

url.obfuscate('https://example.com')
// 'https://example.com' — no credentials, unchanged