5b2c591c6ca3b99bb7ed8b9c5e33553948f03a01b0c924fb5953f568fe10f63694d35f6850f9f96ce4185538c2211c10f9874ba7cb51cd9d5e77041efe9c2e 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # normalize-url [![Build Status](https://travis-ci.org/sindresorhus/normalize-url.svg?branch=master)](https://travis-ci.org/sindresorhus/normalize-url)
  2. > [Normalize](http://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 --save 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. ##### normalizeProtocol
  23. Type: `boolean`<br>
  24. Default: `true`
  25. Prepend `http:` to the URL if it's protocol-relative.
  26. ```js
  27. normalizeUrl('//sindresorhus.com:80/');
  28. //=> 'http://sindresorhus.com'
  29. normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false});
  30. //=> '//sindresorhus.com'
  31. ```
  32. ##### normalizeHttps
  33. Type: `boolean`<br>
  34. Default: `false`
  35. Normalize `https:` URLs to `http:`.
  36. ```js
  37. normalizeUrl('https://sindresorhus.com:80/');
  38. //=> 'https://sindresorhus.com'
  39. normalizeUrl('https://sindresorhus.com:80/', {normalizeHttps: true});
  40. //=> 'http://sindresorhus.com'
  41. ```
  42. ##### stripFragment
  43. Type: `boolean`<br>
  44. Default: `true`
  45. Remove the fragment at the end of the URL.
  46. ```js
  47. normalizeUrl('sindresorhus.com/about.html#contact');
  48. //=> 'http://sindresorhus.com/about.html'
  49. normalizeUrl('sindresorhus.com/about.html#contact', {stripFragment: false});
  50. //=> 'http://sindresorhus.com/about.html#contact'
  51. ```
  52. ##### stripWWW
  53. Type: `boolean`<br>
  54. Default: `true`
  55. Remove `www.` from the URL.
  56. ```js
  57. normalizeUrl('http://www.sindresorhus.com/about.html#contact');
  58. //=> 'http://sindresorhus.com/about.html#contact'
  59. normalizeUrl('http://www.sindresorhus.com/about.html#contact', {stripWWW: false});
  60. //=> 'http://www.sindresorhus.com/about.html#contact'
  61. ```
  62. ##### removeQueryParameters
  63. Type: `Array<RegExp|string>`<br>
  64. Default: `[/^utm_\w+/i]`
  65. Remove query parameters that matches any of the provided strings or regexes.
  66. ```js
  67. normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
  68. removeQueryParameters: ['ref']
  69. });
  70. //=> 'http://sindresorhus.com/?foo=bar'
  71. ```
  72. ##### removeTrailingSlash
  73. Type: `boolean`<br>
  74. Default: `true`
  75. Remove trailing slash.
  76. **Note:** Trailing slash is always removed if the URL doesn't have a pathname.
  77. ```js
  78. normalizeUrl('http://sindresorhus.com/redirect/');
  79. //=> 'http://sindresorhus.com/redirect'
  80. normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false});
  81. //=> 'http://sindresorhus.com/redirect/'
  82. normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
  83. //=> 'http://sindresorhus.com'
  84. ```
  85. ##### removeDirectoryIndex
  86. Type: `boolean` `Array<RegExp|string>`<br>
  87. Default: `false`
  88. Remove 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.
  89. ```js
  90. normalizeUrl('www.sindresorhus.com/foo/default.php', {
  91. removeDirectoryIndex: [/^default\.[a-z]+$/]
  92. });
  93. //=> 'http://sindresorhus.com/foo'
  94. ```
  95. ## Related
  96. - [compare-urls](https://github.com/sindresorhus/compare-urls) - Compare URLs by first normalizing them
  97. ## License
  98. MIT © [Sindre Sorhus](https://sindresorhus.com)