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.

147 lines
6.1 KiB

4 years ago
  1. # url-parse
  2. [![Made by unshift](https://img.shields.io/badge/made%20by-unshift-00ffcc.svg?style=flat-square)](http://unshift.io)[![Version npm](https://img.shields.io/npm/v/url-parse.svg?style=flat-square)](https://www.npmjs.com/package/url-parse)[![Build Status](https://img.shields.io/travis/unshiftio/url-parse/master.svg?style=flat-square)](https://travis-ci.org/unshiftio/url-parse)[![Dependencies](https://img.shields.io/david/unshiftio/url-parse.svg?style=flat-square)](https://david-dm.org/unshiftio/url-parse)[![Coverage Status](https://img.shields.io/coveralls/unshiftio/url-parse/master.svg?style=flat-square)](https://coveralls.io/r/unshiftio/url-parse?branch=master)[![IRC channel](https://img.shields.io/badge/IRC-irc.freenode.net%23unshift-00a8ff.svg?style=flat-square)](https://webchat.freenode.net/?channels=unshift)
  3. [![Sauce Test Status](https://saucelabs.com/browser-matrix/url-parse.svg)](https://saucelabs.com/u/url-parse)
  4. The `url-parse` method exposes two different API interfaces. The
  5. [`url`](https://nodejs.org/api/url.html) interface that you know from Node.js
  6. and the new [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL)
  7. interface that is available in the latest browsers.
  8. In version `0.1` we moved from a DOM based parsing solution, using the `<a>`
  9. element, to a full Regular Expression solution. The main reason for this was
  10. to make the URL parser available in different JavaScript environments as you
  11. don't always have access to the DOM. An example of such environment is the
  12. [`Worker`](https://developer.mozilla.org/en/docs/Web/API/Worker) interface.
  13. The RegExp based solution didn't work well as it required a lot of lookups
  14. causing major problems in FireFox. In version `1.0.0` we ditched the RegExp
  15. based solution in favor of a pure string parsing solution which chops up the
  16. URL into smaller pieces. This module still has a really small footprint as it
  17. has been designed to be used on the client side.
  18. In addition to URL parsing we also expose the bundled `querystringify` module.
  19. ## Installation
  20. This module is designed to be used using either browserify or Node.js it's
  21. released in the public npm registry and can be installed using:
  22. ```
  23. npm install url-parse
  24. ```
  25. ## Usage
  26. All examples assume that this library is bootstrapped using:
  27. ```js
  28. 'use strict';
  29. var Url = require('url-parse');
  30. ```
  31. To parse an URL simply call the `URL` method with the URL that needs to be
  32. transformed into an object.
  33. ```js
  34. var url = new Url('https://github.com/foo/bar');
  35. ```
  36. The `new` keyword is optional but it will save you an extra function invocation.
  37. The constructor takes the following arguments:
  38. - `url` (`String`): A string representing an absolute or relative URL.
  39. - `baseURL` (`Object` | `String`): An object or string representing
  40. the base URL to use in case `url` is a relative URL. This argument is
  41. optional and defaults to [`location`](https://developer.mozilla.org/en-US/docs/Web/API/Location)
  42. in the browser.
  43. - `parser` (`Boolean` | `Function`): This argument is optional and specifies
  44. how to parse the query string. By default it is `false` so the query string
  45. is not parsed. If you pass `true` the query string is parsed using the
  46. embedded `querystringify` module. If you pass a function the query string
  47. will be parsed using this function.
  48. As said above we also support the Node.js interface so you can also use the
  49. library in this way:
  50. ```js
  51. 'use strict';
  52. var parse = require('url-parse')
  53. , url = parse('https://github.com/foo/bar', true);
  54. ```
  55. The returned `url` instance contains the following properties:
  56. - `protocol`: The protocol scheme of the URL (e.g. `http:`).
  57. - `slashes`: A boolean which indicates whether the `protocol` is followed by two
  58. forward slashes (`//`).
  59. - `auth`: Authentication information portion (e.g. `username:password`).
  60. - `username`: Username of basic authentication.
  61. - `password`: Password of basic authentication.
  62. - `host`: Host name with port number.
  63. - `hostname`: Host name without port number.
  64. - `port`: Optional port number.
  65. - `pathname`: URL path.
  66. - `query`: Parsed object containing query string, unless parsing is set to false.
  67. - `hash`: The "fragment" portion of the URL including the pound-sign (`#`).
  68. - `href`: The full URL.
  69. - `origin`: The origin of the URL.
  70. Note that when `url-parse` is used in a browser environment, it will default to
  71. using the browser's current window location as the base URL when parsing all
  72. inputs. To parse an input independently of the browser's current URL (e.g. for
  73. functionality parity with the library in a Node environment), pass an empty
  74. location object as the second parameter:
  75. ```js
  76. var parse = require('url-parse');
  77. parse('hostname', {});
  78. ```
  79. ### Url.set(key, value)
  80. A simple helper function to change parts of the URL and propagating it through
  81. all properties. When you set a new `host` you want the same value to be applied
  82. to `port` if has a different port number, `hostname` so it has a correct name
  83. again and `href` so you have a complete URL.
  84. ```js
  85. var parsed = parse('http://google.com/parse-things');
  86. parsed.set('hostname', 'yahoo.com');
  87. console.log(parsed.href); // http://yahoo.com/parse-things
  88. ```
  89. It's aware of default ports so you cannot set a port 80 on an URL which has
  90. `http` as protocol.
  91. ### Url.toString()
  92. The returned `url` object comes with a custom `toString` method which will
  93. generate a full URL again when called. The method accepts an extra function
  94. which will stringify the query string for you. If you don't supply a function we
  95. will use our default method.
  96. ```js
  97. var location = url.toString(); // http://example.com/whatever/?qs=32
  98. ```
  99. You would rarely need to use this method as the full URL is also available as
  100. `href` property. If you are using the `URL.set` method to make changes, this
  101. will automatically update.
  102. ## Testing
  103. The testing of this module is done in 3 different ways:
  104. 1. We have unit tests that run under Node.js. You can run these tests with the
  105. `npm test` command.
  106. 2. Code coverage can be run manually using `npm run coverage`.
  107. 3. For browser testing we use Sauce Labs and `zuul`. You can run browser tests
  108. using the `npm run test-browser` command.
  109. ## License
  110. [MIT](LICENSE)