Normalize a URL
Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.
Install
$ npm install normalize-url
Usage
const normalizeUrl = ; ;//=> 'http://sindresorhus.com' ;//=> 'http://êxample.com/?a=foo&b=bar'
API
normalizeUrl(url, options?)
url
Type: string
URL to normalize, including data URL.
options
Type: object
defaultProtocol
Type: string
Default: http:
normalizeProtocol
Type: boolean
Default: true
Prepend defaultProtocol
to the URL if it's protocol-relative.
;//=> 'http://sindresorhus.com' ;//=> '//sindresorhus.com'
forceHttp
Type: boolean
Default: false
Normalize https:
to http:
.
;//=> 'https://sindresorhus.com' ;//=> 'http://sindresorhus.com'
forceHttps
Type: boolean
Default: false
Normalize http:
to https:
.
;//=> 'https://sindresorhus.com' ;//=> 'https://sindresorhus.com'
This option can't be used with the forceHttp
option at the same time.
stripAuthentication
Type: boolean
Default: true
Strip the authentication part of the URL.
;//=> 'https://sindresorhus.com' ;//=> 'https://user:password@sindresorhus.com'
stripHash
Type: boolean
Default: false
Strip the hash part of the URL.
;//=> 'http://sindresorhus.com/about.html#contact' ;//=> 'http://sindresorhus.com/about.html'
stripProtocol
Type: boolean
Default: false
Remove HTTP(S) protocol from the URL: http://sindresorhus.com
→ sindresorhus.com
.
;//=> 'https://sindresorhus.com' ;//=> 'sindresorhus.com'
stripWWW
Type: boolean
Default: true
Remove www.
from the URL.
;//=> 'http://sindresorhus.com' ;//=> 'http://www.sindresorhus.com'
removeQueryParameters
Type: Array<RegExp | string>
Default: [/^utm_\w+/i]
Remove query parameters that matches any of the provided strings or regexes.
;//=> 'http://sindresorhus.com/?foo=bar'
removeTrailingSlash
Type: boolean
Default: true
Remove trailing slash.
Note: Trailing slash is always removed if the URL doesn't have a pathname.
;//=> 'http://sindresorhus.com/redirect' ;//=> 'http://sindresorhus.com/redirect/' ;//=> 'http://sindresorhus.com'
removeDirectoryIndex
Type: boolean | Array<RegExp | string>
Default: false
Removes 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.
;//=> 'http://sindresorhus.com/foo'
sortQueryParameters
Type: boolean
Default: true
Sorts the query parameters alphabetically by key.
;//=> 'http://sindresorhus.com/?b=two&a=one&c=three'
Related
- compare-urls - Compare URLs by first normalizing them
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.