UNPKG

6.42 kBMarkdownView Raw
1# normalize-url [![Coverage Status](https://codecov.io/gh/sindresorhus/normalize-url/branch/main/graph/badge.svg)](https://codecov.io/gh/sindresorhus/normalize-url)
2
3> [Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL
4
5Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.
6
7## Install
8
9```
10$ npm install normalize-url
11```
12
13*If you need to use this in the browser, use version 4: `npm i normalize-url@4`*
14
15## Usage
16
17```js
18const normalizeUrl = require('normalize-url');
19
20normalizeUrl('sindresorhus.com');
21//=> 'http://sindresorhus.com'
22
23normalizeUrl('//www.sindresorhus.com:80/../baz?b=bar&a=foo');
24//=> 'http://sindresorhus.com/baz?a=foo&b=bar'
25```
26
27## API
28
29### normalizeUrl(url, options?)
30
31#### url
32
33Type: `string`
34
35URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).
36
37#### options
38
39Type: `object`
40
41##### defaultProtocol
42
43Type: `string`\
44Default: `http:`
45
46##### normalizeProtocol
47
48Type: `boolean`\
49Default: `true`
50
51Prepend `defaultProtocol` to the URL if it's protocol-relative.
52
53```js
54normalizeUrl('//sindresorhus.com:80/');
55//=> 'http://sindresorhus.com'
56
57normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false});
58//=> '//sindresorhus.com'
59```
60
61##### forceHttp
62
63Type: `boolean`\
64Default: `false`
65
66Normalize `https:` to `http:`.
67
68```js
69normalizeUrl('https://sindresorhus.com:80/');
70//=> 'https://sindresorhus.com'
71
72normalizeUrl('https://sindresorhus.com:80/', {forceHttp: true});
73//=> 'http://sindresorhus.com'
74```
75
76##### forceHttps
77
78Type: `boolean`\
79Default: `false`
80
81Normalize `http:` to `https:`.
82
83```js
84normalizeUrl('https://sindresorhus.com:80/');
85//=> 'https://sindresorhus.com'
86
87normalizeUrl('http://sindresorhus.com:80/', {forceHttps: true});
88//=> 'https://sindresorhus.com'
89```
90
91This option can't be used with the `forceHttp` option at the same time.
92
93##### stripAuthentication
94
95Type: `boolean`\
96Default: `true`
97
98Strip the [authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) part of the URL.
99
100```js
101normalizeUrl('user:password@sindresorhus.com');
102//=> 'https://sindresorhus.com'
103
104normalizeUrl('user:password@sindresorhus.com', {stripAuthentication: false});
105//=> 'https://user:password@sindresorhus.com'
106```
107
108##### stripHash
109
110Type: `boolean`\
111Default: `false`
112
113Strip the hash part of the URL.
114
115```js
116normalizeUrl('sindresorhus.com/about.html#contact');
117//=> 'http://sindresorhus.com/about.html#contact'
118
119normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
120//=> 'http://sindresorhus.com/about.html'
121```
122
123##### stripProtocol
124
125Type: `boolean`\
126Default: `false`
127
128Remove HTTP(S) protocol from the URL: `http://sindresorhus.com``sindresorhus.com`.
129
130```js
131normalizeUrl('https://sindresorhus.com');
132//=> 'https://sindresorhus.com'
133
134normalizeUrl('https://sindresorhus.com', {stripProtocol: true});
135//=> 'sindresorhus.com'
136```
137
138##### stripTextFragment
139
140Type: `boolean`\
141Default: `true`
142
143Strip the [text fragment](https://web.dev/text-fragments/) part of the URL.
144
145**Note:** The text fragment will always be removed if the `stripHash` option is set to `true`, as the hash contains the text fragment.
146
147```js
148normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello');
149//=> 'http://sindresorhus.com/about.html#'
150
151normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello');
152//=> 'http://sindresorhus.com/about.html#section'
153
154normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello', {stripTextFragment: false});
155//=> 'http://sindresorhus.com/about.html#:~:text=hello'
156
157normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello', {stripTextFragment: false});
158//=> 'http://sindresorhus.com/about.html#section:~:text=hello'
159```
160
161##### stripWWW
162
163Type: `boolean`\
164Default: `true`
165
166Remove `www.` from the URL.
167
168```js
169normalizeUrl('http://www.sindresorhus.com');
170//=> 'http://sindresorhus.com'
171
172normalizeUrl('http://www.sindresorhus.com', {stripWWW: false});
173//=> 'http://www.sindresorhus.com'
174```
175
176##### removeQueryParameters
177
178Type: `Array<RegExp | string>`\
179Default: `[/^utm_\w+/i]`
180
181Remove query parameters that matches any of the provided strings or regexes.
182
183```js
184normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
185 removeQueryParameters: ['ref']
186});
187//=> 'http://sindresorhus.com/?foo=bar'
188```
189
190##### removeTrailingSlash
191
192Type: `boolean`\
193Default: `true`
194
195Remove trailing slash.
196
197**Note:** Trailing slash is always removed if the URL doesn't have a pathname unless the `removeSingleSlash` option is set to `false`.
198
199```js
200normalizeUrl('http://sindresorhus.com/redirect/');
201//=> 'http://sindresorhus.com/redirect'
202
203normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false});
204//=> 'http://sindresorhus.com/redirect/'
205
206normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
207//=> 'http://sindresorhus.com'
208```
209
210##### removeSingleSlash
211
212Type: `boolean`\
213Default: `true`
214
215Remove a sole `/` pathname in the output. This option is independant of `removeTrailingSlash`.
216
217```js
218normalizeUrl('https://sindresorhus.com/');
219//=> 'https://sindresorhus.com'
220
221normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false});
222//=> 'https://sindresorhus.com/'
223```
224
225
226##### removeDirectoryIndex
227
228Type: `boolean | Array<RegExp | string>`\
229Default: `false`
230
231Removes 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.
232
233```js
234normalizeUrl('www.sindresorhus.com/foo/default.php', {
235 removeDirectoryIndex: [/^default\.[a-z]+$/]
236});
237//=> 'http://sindresorhus.com/foo'
238```
239
240##### sortQueryParameters
241
242Type: `boolean`\
243Default: `true`
244
245Sorts the query parameters alphabetically by key.
246
247```js
248normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
249 sortQueryParameters: false
250});
251//=> 'http://sindresorhus.com/?b=two&a=one&c=three'
252```
253
254## Related
255
256- [compare-urls](https://github.com/sindresorhus/compare-urls) - Compare URLs by first normalizing them
257
258---
259
260<div align="center">
261 <b>
262 <a href="https://tidelift.com/subscription/pkg/npm-normalize-url?utm_source=npm-normalize-url&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
263 </b>
264 <br>
265 <sub>
266 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
267 </sub>
268</div>