normalize-url

Normalize a URL

Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.

Install

$ npm install --save normalize-url

Usage

const normalizeUrl = require('normalize-url');

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

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

API

normalizeUrl(url, [options])

url

Type: string

URL to normalize.

options

normalizeProtocol

Type: boolean Default: true

Prepend http: to the URL if it's protocol-relative.

normalizeHttps

Type: boolean Default: false

Normalize https: URLs to http:.

stripFragment

Type: boolean Default: true

Remove the fragment at the end of the URL.

stripWWW

Type: boolean Default: true

Remove www. from the URL.

removeQueryParameters

Type: Array<RegExp|string> Default: [/^utm_\w+/i]

Remove query parameters that matches any of the provided strings or regexes.

removeTrailingSlash

Type: boolean Default: true

Remove trailing slash.

Note: Trailing slash is always removed if the URL doesn't have a pathname.

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.

License

MIT © Sindre Sorhus

Last updated

Was this helpful?