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