• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# query-string [![Build Status](https://travis-ci.org/sindresorhus/query-string.svg?branch=master)](https://travis-ci.org/sindresorhus/query-string)
2
3> Parse and stringify URL [query strings](https://en.wikipedia.org/wiki/Query_string)
4
5
6## Install
7
8```
9$ npm install query-string
10```
11
12This module targets Node.js 6 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, or, [if your project is using create-react-app v1](https://github.com/sindresorhus/query-string/pull/148#issuecomment-399656020), use version 5: `npm install query-string@5`.
13
14
15## Usage
16
17```js
18const queryString = require('query-string');
19
20console.log(location.search);
21//=> '?foo=bar'
22
23const parsed = queryString.parse(location.search);
24console.log(parsed);
25//=> {foo: 'bar'}
26
27console.log(location.hash);
28//=> '#token=bada55cafe'
29
30const parsedHash = queryString.parse(location.hash);
31console.log(parsedHash);
32//=> {token: 'bada55cafe'}
33
34parsed.foo = 'unicorn';
35parsed.ilike = 'pizza';
36
37const stringified = queryString.stringify(parsed);
38//=> 'foo=unicorn&ilike=pizza'
39
40location.search = stringified;
41// note that `location.search` automatically prepends a question mark
42console.log(location.search);
43//=> '?foo=unicorn&ilike=pizza'
44```
45
46
47## API
48
49### .parse(string, options?)
50
51Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.
52
53The returned object is created with [`Object.create(null)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) and thus does not have a `prototype`.
54
55#### options
56
57Type: `object`
58
59##### decode
60
61Type: `boolean`<br>
62Default: `true`
63
64Decode the keys and values. URL components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
65
66##### arrayFormat
67
68Type: `string`<br>
69Default: `'none'`
70
71- `'bracket'`: Parse arrays with bracket representation:
72
73```js
74queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
75//=> {foo: ['1', '2', '3']}
76```
77
78- `'index'`: Parse arrays with index representation:
79
80```js
81queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
82//=> {foo: ['1', '2', '3']}
83```
84
85- `'comma'`: Parse arrays with elements separated by comma:
86
87```js
88queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
89//=> {foo: ['1', '2', '3']}
90```
91
92- `'none'`: Parse arrays with elements using duplicate keys:
93
94```js
95queryString.parse('foo=1&foo=2&foo=3');
96//=> {foo: ['1', '2', '3']}
97```
98
99##### sort
100
101Type: `Function | boolean`<br>
102Default: `true`
103
104Supports both `Function` as a custom sorting function or `false` to disable sorting.
105
106##### parseNumbers
107
108Type: `boolean`<br>
109Default: `false`
110
111```js
112queryString.parse('foo=1', {parseNumbers: true});
113//=> {foo: 1}
114```
115
116Parse the value as a number type instead of string type if it's a number.
117
118##### parseBooleans
119
120Type: `boolean`<br>
121Default: `false`
122
123```js
124queryString.parse('foo=true', {parseBooleans: true});
125//=> {foo: true}
126```
127
128Parse the value as a boolean type instead of string type if it's a boolean.
129
130### .stringify(object, [options])
131
132Stringify an object into a query string and sorting the keys.
133
134#### options
135
136Type: `object`
137
138##### strict
139
140Type: `boolean`<br>
141Default: `true`
142
143Strictly encode URI components with [strict-uri-encode](https://github.com/kevva/strict-uri-encode). It uses [encodeURIComponent](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) if set to false. You probably [don't care](https://github.com/sindresorhus/query-string/issues/42) about this option.
144
145##### encode
146
147Type: `boolean`<br>
148Default: `true`
149
150[URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
151
152##### arrayFormat
153
154Type: `string`<br>
155Default: `'none'`
156
157- `'bracket'`: Serialize arrays using bracket representation:
158
159```js
160queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
161//=> 'foo[]=1&foo[]=2&foo[]=3'
162```
163
164- `'index'`: Serialize arrays using index representation:
165
166```js
167queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
168//=> 'foo[0]=1&foo[1]=2&foo[2]=3'
169```
170
171- `'comma'`: Serialize arrays by separating elements with comma:
172
173```js
174queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
175//=> 'foo=1,2,3'
176```
177
178- `'none'`: Serialize arrays by using duplicate keys:
179
180```js
181queryString.stringify({foo: [1, 2, 3]});
182//=> 'foo=1&foo=2&foo=3'
183```
184
185##### sort
186
187Type: `Function | boolean`
188
189Supports both `Function` as a custom sorting function or `false` to disable sorting.
190
191```js
192const order = ['c', 'a', 'b'];
193
194queryString.stringify({a: 1, b: 2, c: 3}, {
195	sort: (a, b) => order.indexOf(a) - order.indexOf(b)
196});
197//=> 'c=3&a=1&b=2'
198```
199
200```js
201queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
202//=> 'b=1&c=2&a=3'
203```
204
205If omitted, keys are sorted using `Array#sort()`, which means, converting them to strings and comparing strings in Unicode code point order.
206
207### .extract(string)
208
209Extract a query string from a URL that can be passed into `.parse()`.
210
211### .parseUrl(string, options?)
212
213Extract the URL and the query string as an object.
214
215The `options` are the same as for `.parse()`.
216
217Returns an object with a `url` and `query` property.
218
219```js
220queryString.parseUrl('https://foo.bar?foo=bar');
221//=> {url: 'https://foo.bar', query: {foo: 'bar'}}
222```
223
224
225## Nesting
226
227This module intentionally doesn't support nesting as it's not spec'd and varies between implementations, which causes a lot of [edge cases](https://github.com/visionmedia/node-querystring/issues).
228
229You're much better off just converting the object to a JSON string:
230
231```js
232queryString.stringify({
233	foo: 'bar',
234	nested: JSON.stringify({
235		unicorn: 'cake'
236	})
237});
238//=> 'foo=bar&nested=%7B%22unicorn%22%3A%22cake%22%7D'
239```
240
241However, there is support for multiple instances of the same key:
242
243```js
244queryString.parse('likes=cake&name=bob&likes=icecream');
245//=> {likes: ['cake', 'icecream'], name: 'bob'}
246
247queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'});
248//=> 'color=taupe&color=chartreuse&id=515'
249```
250
251
252## Falsy values
253
254Sometimes you want to unset a key, or maybe just make it present without assigning a value to it. Here is how falsy values are stringified:
255
256```js
257queryString.stringify({foo: false});
258//=> 'foo=false'
259
260queryString.stringify({foo: null});
261//=> 'foo'
262
263queryString.stringify({foo: undefined});
264//=> ''
265```
266
267
268---
269
270<div align="center">
271	<b>
272		<a href="https://tidelift.com/subscription/pkg/npm-query-string?utm_source=npm-query-string&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
273	</b>
274	<br>
275	<sub>
276		Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
277	</sub>
278</div>
279