A collection of common functions to make working with URLs less painful.
$ npm install @njkleiner/urls
const urls = require('@njkleiner/urls');
urls.isURL('https://example.com');
// => true
urls.isURL('not a url');
// => false
urls.isAbsolute('https://example.com/about');
// => true
urls.isRelative('/test');
// => true
urls.normalize('EXAMPLE.com');
// => http://example.com/
urls.compare('http://example.com/', 'example.com');
// => true
urls.matchesHost('https://www.example.com/test', 'example.com');
// => true
urls.normalizeQuery('https://www.example.com/?abc=def&xyz=123');
// => {'abc': 'def', 'xyz': '123'}
urls.appendQuery('https://example.com/test?abc=xyz', {'foo': 'bar'});
// => https://example.com/test?abc=xyz&foo=bar
urls.join('https://example.com', '/test');
// => https://example.com/test
Returns true
if value
is a valid URL.
Type: string
The string for which to determine whether it is a URL.
Type: Array<string>
Default: ['http', 'https']
A list of valid protocols.
By default, only http
and https
are considered valid protocols. If you want to accept any protocol, use an empty array.
Returns true
if value
is an absolute URL.
Type: string
The URL to test.
Returns true
if value
is a relative URL.
Type: string
The URL to test.
Takes a URL-like string and returns a normalized URL.
Appends http://
if no protocol is specified, normalizes the URL by calling new URL(value).toString()
and removes trailing #
and ?
characters when possible.
Type: string
The URL to normalize.
Returns true
if left
and right
are valid URLs and are equal after normalizing them.
Type: string
The first URL to compare.
Type: string
The second URL to compare.
Returns true
if value
is a valid URL and its host is host
.
Type: string
The URL whoose host to test.
Type: string
The host to test against.
Returns a dictionary object
of query parameters parsed from value
.
Type: string
The URL whoose query parameters to parse and normalize.
Appends query parameters to an existing URL and returns the stringified result.
Type: string
The URL to append query parameters to.
Type: object
A dictionary of query parameters to append.
Returns a stringified URL that is the result of appending path
to base
.
Type: string
The base URL to append path
to.
Type: string
The path segment appended to base
.
You can contribute to this project by sending patches to noah@njkleiner.com
.
See also the list of contributors who participated in this project.
This project is licensed under the MIT License. See the LICENSE.md file for details.