1 |
3a515b92
|
cagy
|
# querystringify
|
2 |
|
|
|
3 |
|
|
[](http://unshift.io)[](http://browsenpm.org/package/querystringify)[](https://travis-ci.org/unshiftio/querystringify)[](https://david-dm.org/unshiftio/querystringify)[](https://coveralls.io/r/unshiftio/querystringify?branch=master)[](http://webchat.freenode.net/?channels=unshift)
|
4 |
|
|
|
5 |
|
|
A somewhat JSON compatible interface for query string parsing. This query string
|
6 |
|
|
parser is dumb, don't expect to much from it as it only wants to parse simple
|
7 |
|
|
query strings. If you want to parse complex, multi level and deeply nested
|
8 |
|
|
query strings then you should ask your self. WTF am I doing?
|
9 |
|
|
|
10 |
|
|
## Installation
|
11 |
|
|
|
12 |
|
|
This module is released in npm as `querystringify`. It's also compatible with
|
13 |
|
|
`browserify` so it can be used on the server as well as on the client. To
|
14 |
|
|
install it simply run the following command from your CLI:
|
15 |
|
|
|
16 |
|
|
```
|
17 |
|
|
npm install --save querystringify
|
18 |
|
|
```
|
19 |
|
|
|
20 |
|
|
## Usage
|
21 |
|
|
|
22 |
|
|
In the following examples we assume that you've already required the library as:
|
23 |
|
|
|
24 |
|
|
```js
|
25 |
|
|
'use strict';
|
26 |
|
|
|
27 |
|
|
var qs = require('querystringify');
|
28 |
|
|
```
|
29 |
|
|
|
30 |
|
|
### qs.parse()
|
31 |
|
|
|
32 |
|
|
The parse method transforms a given query string in to an object. Parameters
|
33 |
|
|
without values are set to empty strings. It does not care if your query string
|
34 |
|
|
is prefixed with a `?` or not. It just extracts the parts between the `=` and
|
35 |
|
|
`&`:
|
36 |
|
|
|
37 |
|
|
```js
|
38 |
|
|
qs.parse('?foo=bar'); // { foo: 'bar' }
|
39 |
|
|
qs.parse('foo=bar'); // { foo: 'bar' }
|
40 |
|
|
qs.parse('foo=bar&bar=foo'); // { foo: 'bar', bar: 'foo' }
|
41 |
|
|
qs.parse('foo&bar=foo'); // { foo: '', bar: 'foo' }
|
42 |
|
|
```
|
43 |
|
|
|
44 |
|
|
### qs.stringify()
|
45 |
|
|
|
46 |
|
|
This transforms a given object in to a query string. By default we return the
|
47 |
|
|
query string without a `?` prefix. If you want to prefix it by default simply
|
48 |
|
|
supply `true` as second argument. If it should be prefixed by something else
|
49 |
|
|
simply supply a string with the prefix value as second argument:
|
50 |
|
|
|
51 |
|
|
```js
|
52 |
|
|
qs.stringify({ foo: bar }); // foo=bar
|
53 |
|
|
qs.stringify({ foo: bar }, true); // ?foo=bar
|
54 |
|
|
qs.stringify({ foo: bar }, '&'); // &foo=bar
|
55 |
|
|
qs.stringify({ foo: '' }, '&'); // &foo=
|
56 |
|
|
```
|
57 |
|
|
|
58 |
|
|
## License
|
59 |
|
|
|
60 |
|
|
MIT
|