UNPKG

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