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