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.

194 lines
4.1 KiB

4 years ago
  1. # normalize-url [![Build Status](https://travis-ci.org/sindresorhus/normalize-url.svg?branch=master)](https://travis-ci.org/sindresorhus/normalize-url) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/normalize-url/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/normalize-url?branch=master)
  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. ## Usage
  9. ```js
  10. const normalizeUrl = require('normalize-url');
  11. normalizeUrl('sindresorhus.com');
  12. //=> 'http://sindresorhus.com'
  13. normalizeUrl('HTTP://xn--xample-hva.com:80/?b=bar&a=foo');
  14. //=> 'http://êxample.com/?a=foo&b=bar'
  15. ```
  16. ## API
  17. ### normalizeUrl(url, [options])
  18. #### url
  19. Type: `string`
  20. URL to normalize.
  21. #### options
  22. Type: `Object`
  23. ##### defaultProtocol
  24. Type: `string`<br>
  25. Default: `http:`
  26. ##### normalizeProtocol
  27. Type: `boolean`<br>
  28. Default: `true`
  29. Prepends `defaultProtocol` to the URL if it's protocol-relative.
  30. ```js
  31. normalizeUrl('//sindresorhus.com:80/');
  32. //=> 'http://sindresorhus.com'
  33. normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false});
  34. //=> '//sindresorhus.com'
  35. ```
  36. ##### forceHttp
  37. Type: `boolean`<br>
  38. Default: `false`
  39. Normalizes `https:` URLs to `http:`.
  40. ```js
  41. normalizeUrl('https://sindresorhus.com:80/');
  42. //=> 'https://sindresorhus.com'
  43. normalizeUrl('https://sindresorhus.com:80/', {normalizeHttps: true});
  44. //=> 'http://sindresorhus.com'
  45. ```
  46. ##### forceHttps
  47. Type: `boolean`<br>
  48. Default: `false`
  49. Normalizes `http:` URLs to `https:`.
  50. ```js
  51. normalizeUrl('https://sindresorhus.com:80/');
  52. //=> 'https://sindresorhus.com'
  53. normalizeUrl('http://sindresorhus.com:80/', {normalizeHttp: true});
  54. //=> 'https://sindresorhus.com'
  55. ```
  56. This option can't be used with the `forceHttp` option at the same time.
  57. ##### stripHash
  58. Type: `boolean`<br>
  59. Default: `true`
  60. Removes hash from the URL.
  61. ```js
  62. normalizeUrl('sindresorhus.com/about.html#contact');
  63. //=> 'http://sindresorhus.com/about.html'
  64. normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: false});
  65. //=> 'http://sindresorhus.com/about.html#contact'
  66. ```
  67. ##### stripWWW
  68. Type: `boolean`<br>
  69. Default: `true`
  70. Removes `www.` from the URL.
  71. ```js
  72. normalizeUrl('http://www.sindresorhus.com/about.html#contact');
  73. //=> 'http://sindresorhus.com/about.html#contact'
  74. normalizeUrl('http://www.sindresorhus.com/about.html#contact', {stripWWW: false});
  75. //=> 'http://www.sindresorhus.com/about.html#contact'
  76. ```
  77. ##### removeQueryParameters
  78. Type: `Array<RegExp|string>`<br>
  79. Default: `[/^utm_\w+/i]`
  80. Removes query parameters that matches any of the provided strings or regexes.
  81. ```js
  82. normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
  83. removeQueryParameters: ['ref']
  84. });
  85. //=> 'http://sindresorhus.com/?foo=bar'
  86. ```
  87. ##### removeTrailingSlash
  88. Type: `boolean`<br>
  89. Default: `true`
  90. Removes trailing slash.
  91. **Note:** Trailing slash is always removed if the URL doesn't have a pathname.
  92. ```js
  93. normalizeUrl('http://sindresorhus.com/redirect/');
  94. //=> 'http://sindresorhus.com/redirect'
  95. normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false});
  96. //=> 'http://sindresorhus.com/redirect/'
  97. normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
  98. //=> 'http://sindresorhus.com'
  99. ```
  100. ##### removeDirectoryIndex
  101. Type: `boolean` `Array<RegExp|string>`<br>
  102. Default: `false`
  103. 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.
  104. ```js
  105. normalizeUrl('www.sindresorhus.com/foo/default.php', {
  106. removeDirectoryIndex: [/^default\.[a-z]+$/]
  107. });
  108. //=> 'http://sindresorhus.com/foo'
  109. ```
  110. ##### sortQueryParameters
  111. Type: `boolean`<br>
  112. Default: `true`
  113. Sorts the query parameters alphabetically by key.
  114. ```js
  115. normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
  116. sortQueryParameters: false
  117. });
  118. //=> 'http://sindresorhus.com/?b=two&a=one&c=three'
  119. ```
  120. ## Related
  121. - [compare-urls](https://github.com/sindresorhus/compare-urls) - Compare URLs by first normalizing them
  122. ## License
  123. MIT © [Sindre Sorhus](https://sindresorhus.com)