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.

readme.md 3.5KB

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