You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
6.7 KiB

3 months ago
  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. > [Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL
  3. Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.
  4. ## Install
  5. ```
  6. $ npm install normalize-url
  7. ```
  8. *If you need to use this in the browser, use version 4: `npm i normalize-url@4`*
  9. ## Usage
  10. ```js
  11. const normalizeUrl = require('normalize-url');
  12. normalizeUrl('sindresorhus.com');
  13. //=> 'http://sindresorhus.com'
  14. normalizeUrl('//www.sindresorhus.com:80/../baz?b=bar&a=foo');
  15. //=> 'http://sindresorhus.com/baz?a=foo&b=bar'
  16. ```
  17. ## API
  18. ### normalizeUrl(url, options?)
  19. #### url
  20. Type: `string`
  21. URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).
  22. #### options
  23. Type: `object`
  24. ##### defaultProtocol
  25. Type: `string`\
  26. Default: `http:`
  27. ##### normalizeProtocol
  28. Type: `boolean`\
  29. Default: `true`
  30. Prepend `defaultProtocol` to the URL if it's protocol-relative.
  31. ```js
  32. normalizeUrl('//sindresorhus.com:80/');
  33. //=> 'http://sindresorhus.com'
  34. normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false});
  35. //=> '//sindresorhus.com'
  36. ```
  37. ##### forceHttp
  38. Type: `boolean`\
  39. Default: `false`
  40. Normalize `https:` to `http:`.
  41. ```js
  42. normalizeUrl('https://sindresorhus.com:80/');
  43. //=> 'https://sindresorhus.com'
  44. normalizeUrl('https://sindresorhus.com:80/', {forceHttp: true});
  45. //=> 'http://sindresorhus.com'
  46. ```
  47. ##### forceHttps
  48. Type: `boolean`\
  49. Default: `false`
  50. Normalize `http:` to `https:`.
  51. ```js
  52. normalizeUrl('https://sindresorhus.com:80/');
  53. //=> 'https://sindresorhus.com'
  54. normalizeUrl('http://sindresorhus.com:80/', {forceHttps: true});
  55. //=> 'https://sindresorhus.com'
  56. ```
  57. This option can't be used with the `forceHttp` option at the same time.
  58. ##### stripAuthentication
  59. Type: `boolean`\
  60. Default: `true`
  61. Strip the [authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) part of the URL.
  62. ```js
  63. normalizeUrl('user:password@sindresorhus.com');
  64. //=> 'https://sindresorhus.com'
  65. normalizeUrl('user:password@sindresorhus.com', {stripAuthentication: false});
  66. //=> 'https://user:password@sindresorhus.com'
  67. ```
  68. ##### stripHash
  69. Type: `boolean`\
  70. Default: `false`
  71. Strip the hash part of the URL.
  72. ```js
  73. normalizeUrl('sindresorhus.com/about.html#contact');
  74. //=> 'http://sindresorhus.com/about.html#contact'
  75. normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
  76. //=> 'http://sindresorhus.com/about.html'
  77. ```
  78. ##### stripProtocol
  79. Type: `boolean`\
  80. Default: `false`
  81. Remove HTTP(S) protocol from the URL: `http://sindresorhus.com``sindresorhus.com`.
  82. ```js
  83. normalizeUrl('https://sindresorhus.com');
  84. //=> 'https://sindresorhus.com'
  85. normalizeUrl('https://sindresorhus.com', {stripProtocol: true});
  86. //=> 'sindresorhus.com'
  87. ```
  88. ##### stripTextFragment
  89. Type: `boolean`\
  90. Default: `true`
  91. Strip the [text fragment](https://web.dev/text-fragments/) part of the URL.
  92. **Note:** The text fragment will always be removed if the `stripHash` option is set to `true`, as the hash contains the text fragment.
  93. ```js
  94. normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello');
  95. //=> 'http://sindresorhus.com/about.html#'
  96. normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello');
  97. //=> 'http://sindresorhus.com/about.html#section'
  98. normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello', {stripTextFragment: false});
  99. //=> 'http://sindresorhus.com/about.html#:~:text=hello'
  100. normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello', {stripTextFragment: false});
  101. //=> 'http://sindresorhus.com/about.html#section:~:text=hello'
  102. ```
  103. ##### stripWWW
  104. Type: `boolean`\
  105. Default: `true`
  106. Remove `www.` from the URL.
  107. ```js
  108. normalizeUrl('http://www.sindresorhus.com');
  109. //=> 'http://sindresorhus.com'
  110. normalizeUrl('http://www.sindresorhus.com', {stripWWW: false});
  111. //=> 'http://www.sindresorhus.com'
  112. ```
  113. ##### removeQueryParameters
  114. Type: `Array<RegExp | string> | boolean`\
  115. Default: `[/^utm_\w+/i]`
  116. Remove query parameters that matches any of the provided strings or regexes.
  117. ```js
  118. normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
  119. removeQueryParameters: ['ref']
  120. });
  121. //=> 'http://sindresorhus.com/?foo=bar'
  122. ```
  123. If a boolean is provided, `true` will remove all the query parameters.
  124. ```js
  125. normalizeUrl('www.sindresorhus.com?foo=bar', {
  126. removeQueryParameters: true
  127. });
  128. //=> 'http://sindresorhus.com'
  129. ```
  130. `false` will not remove any query parameter.
  131. ```js
  132. normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
  133. removeQueryParameters: false
  134. });
  135. //=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test'
  136. ```
  137. ##### removeTrailingSlash
  138. Type: `boolean`\
  139. Default: `true`
  140. Remove trailing slash.
  141. **Note:** Trailing slash is always removed if the URL doesn't have a pathname unless the `removeSingleSlash` option is set to `false`.
  142. ```js
  143. normalizeUrl('http://sindresorhus.com/redirect/');
  144. //=> 'http://sindresorhus.com/redirect'
  145. normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false});
  146. //=> 'http://sindresorhus.com/redirect/'
  147. normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
  148. //=> 'http://sindresorhus.com'
  149. ```
  150. ##### removeSingleSlash
  151. Type: `boolean`\
  152. Default: `true`
  153. Remove a sole `/` pathname in the output. This option is independant of `removeTrailingSlash`.
  154. ```js
  155. normalizeUrl('https://sindresorhus.com/');
  156. //=> 'https://sindresorhus.com'
  157. normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false});
  158. //=> 'https://sindresorhus.com/'
  159. ```
  160. ##### removeDirectoryIndex
  161. Type: `boolean | Array<RegExp | string>`\
  162. Default: `false`
  163. 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.
  164. ```js
  165. normalizeUrl('www.sindresorhus.com/foo/default.php', {
  166. removeDirectoryIndex: [/^default\.[a-z]+$/]
  167. });
  168. //=> 'http://sindresorhus.com/foo'
  169. ```
  170. ##### sortQueryParameters
  171. Type: `boolean`\
  172. Default: `true`
  173. Sorts the query parameters alphabetically by key.
  174. ```js
  175. normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
  176. sortQueryParameters: false
  177. });
  178. //=> 'http://sindresorhus.com/?b=two&a=one&c=three'
  179. ```
  180. ## Related
  181. - [compare-urls](https://github.com/sindresorhus/compare-urls) - Compare URLs by first normalizing them
  182. ---
  183. <div align="center">
  184. <b>
  185. <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>
  186. </b>
  187. <br>
  188. <sub>
  189. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  190. </sub>
  191. </div>