source: trip-planner-front/node_modules/normalize-url/readme.md@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.7 KB
Line 
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> | boolean`\
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
190If a boolean is provided, `true` will remove all the query parameters.
191
192```js
193normalizeUrl('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
202normalizeUrl('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
210Type: `boolean`\
211Default: `true`
212
213Remove 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
218normalizeUrl('http://sindresorhus.com/redirect/');
219//=> 'http://sindresorhus.com/redirect'
220
221normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false});
222//=> 'http://sindresorhus.com/redirect/'
223
224normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
225//=> 'http://sindresorhus.com'
226```
227
228##### removeSingleSlash
229
230Type: `boolean`\
231Default: `true`
232
233Remove a sole `/` pathname in the output. This option is independant of `removeTrailingSlash`.
234
235```js
236normalizeUrl('https://sindresorhus.com/');
237//=> 'https://sindresorhus.com'
238
239normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false});
240//=> 'https://sindresorhus.com/'
241```
242
243
244##### removeDirectoryIndex
245
246Type: `boolean | Array<RegExp | string>`\
247Default: `false`
248
249Removes 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
252normalizeUrl('www.sindresorhus.com/foo/default.php', {
253 removeDirectoryIndex: [/^default\.[a-z]+$/]
254});
255//=> 'http://sindresorhus.com/foo'
256```
257
258##### sortQueryParameters
259
260Type: `boolean`\
261Default: `true`
262
263Sorts the query parameters alphabetically by key.
264
265```js
266normalizeUrl('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>
Note: See TracBrowser for help on using the repository browser.