url-clean

Normalize a URL

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
url-clean
1.0.25 years ago5 years agoMinified + gzip package size for url-clean in KB

Readme

url-clean
Normalize a URL

version downloads node status
Fork of normalize-url

Added extra test cases, fixed minor bugs and reduced dependency.
Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.

Install

$ npm install url-clean

Usage

const clean = require('url-clean');

clean('sindresorhus.com');
//=> 'http://sindresorhus.com'

clean('HTTP://xn--xample-hva.com:80/?b=bar&a=foo');
//=> 'http://êxample.com/?a=foo&b=bar'

API

clean(url, options)

url

Type: string
URL to normalize.

options

Type: Object
normalizeProtocol
Type: boolean
Default: true
Prepend http: to the URL if it's protocol-relative.
clean('//sindresorhus.com:80/');
//=> 'http://sindresorhus.com'

clean('//sindresorhus.com:80/', {normalizeProtocol: false});
//=> '//sindresorhus.com'
normalizeHttps
Type: boolean
Default: false
Normalize https: URLs to http:.
clean('https://sindresorhus.com:80/');
//=> 'https://sindresorhus.com'

clean('https://sindresorhus.com:80/', {normalizeHttps: true});
//=> 'http://sindresorhus.com'
stripFragment
Type: boolean
Default: true
Remove the fragment at the end of the URL.
clean('sindresorhus.com/about.html#contact');
//=> 'http://sindresorhus.com/about.html'

clean('sindresorhus.com/about.html#contact', {stripFragment: false});
//=> 'http://sindresorhus.com/about.html#contact'
stripWWW
Type: boolean
Default: true
Remove www. from the URL.
clean('http://www.sindresorhus.com/about.html#contact');
//=> 'http://sindresorhus.com/about.html#contact'

clean('http://www.sindresorhus.com/about.html#contact', {stripWWW: false});
//=> 'http://www.sindresorhus.com/about.html#contact'
removeQueryParameters
Type: Array<RegExp|string>
Default: [/^utm_\w+/i]
Remove query parameters that matches any of the provided strings or regexes.
clean('www.sindresorhus.com?foo=bar&ref=test_ref', {
	removeQueryParameters: ['ref']
});
//=> 'http://sindresorhus.com/?foo=bar'
removeTrailingSlash
Type: boolean
Default: true
Remove trailing slash.
clean('http://sindresorhus.com/redirect/');
//=> 'http://sindresorhus.com/redirect'

clean('http://sindresorhus.com/redirect/', {removeTrailingSlash: false});
//=> 'http://sindresorhus.com/redirect/'

clean('http://sindresorhus.com/', {removeTrailingSlash: false});
//=> 'http://sindresorhus.com/'
removeDirectoryIndex
Type: boolean Array<RegExp|string>
Default: false
Remove the default directory index file from path that matches any of the provided strings or regexes. When true, the regex /^index\.[a-z]+$/ is used.
clean('www.sindresorhus.com/foo/default.php', {
	removeDirectoryIndex: [/^default\.[a-z]+$/]
});
//=> 'http://sindresorhus.com/foo'
sortQueryParameters
Type: boolean
Default: true
Sort the query parameters alphabetically by key.
clean('www.sindresorhus.com?b=two&a=one&c=three', {
	sortQueryParameters: false
});
//=> 'http://sindresorhus.com/?b=two&a=one&c=three'
removeJSessionId
Type: boolean
Default: true
remove the Spring JSessionId
clean('www.sindresorhus.com;jsessionid=CEC3FD6EBB4FE3CBBE37AE3322B23051', {
	removeJSessionId: true
});
//=> 'http://sindresorhus.com'