Initial commit
[yaffs-website] / node_modules / body-parser / node_modules / qs / README.md
1 # qs
2
3 A querystring parsing and stringifying library with some added security.
4
5 [![Build Status](https://secure.travis-ci.org/hapijs/qs.svg)](http://travis-ci.org/hapijs/qs)
6
7 Lead Maintainer: [Nathan LaFreniere](https://github.com/nlf)
8
9 The **qs** module was originally created and maintained by [TJ Holowaychuk](https://github.com/visionmedia/node-querystring).
10
11 ## Usage
12
13 ```javascript
14 var Qs = require('qs');
15
16 var obj = Qs.parse('a=c');    // { a: 'c' }
17 var str = Qs.stringify(obj);  // 'a=c'
18 ```
19
20 ### Parsing Objects
21
22 ```javascript
23 Qs.parse(string, [options]);
24 ```
25
26 **qs** allows you to create nested objects within your query strings, by surrounding the name of sub-keys with square brackets `[]`.
27 For example, the string `'foo[bar]=baz'` converts to:
28
29 ```javascript
30 {
31   foo: {
32     bar: 'baz'
33   }
34 }
35 ```
36
37 When using the `plainObjects` option the parsed value is returned as a plain object, created via `Object.create(null)` and as such you should be aware that prototype methods will not exist on it and a user may set those names to whatever value they like:
38
39 ```javascript
40 Qs.parse('a.hasOwnProperty=b', { plainObjects: true });
41 // { a: { hasOwnProperty: 'b' } }
42 ```
43
44 By default parameters that would overwrite properties on the object prototype are ignored, if you wish to keep the data from those fields either use `plainObjects` as mentioned above, or set `allowPrototypes` to `true` which will allow user input to overwrite those properties. *WARNING* It is generally a bad idea to enable this option as it can cause problems when attempting to use the properties that have been overwritten. Always be careful with this option.
45
46 ```javascript
47 Qs.parse('a.hasOwnProperty=b', { allowPrototypes: true });
48 // { a: { hasOwnProperty: 'b' } }
49 ```
50
51 URI encoded strings work too:
52
53 ```javascript
54 Qs.parse('a%5Bb%5D=c');
55 // { a: { b: 'c' } }
56 ```
57
58 You can also nest your objects, like `'foo[bar][baz]=foobarbaz'`:
59
60 ```javascript
61 {
62   foo: {
63     bar: {
64       baz: 'foobarbaz'
65     }
66   }
67 }
68 ```
69
70 By default, when nesting objects **qs** will only parse up to 5 children deep. This means if you attempt to parse a string like
71 `'a[b][c][d][e][f][g][h][i]=j'` your resulting object will be:
72
73 ```javascript
74 {
75   a: {
76     b: {
77       c: {
78         d: {
79           e: {
80             f: {
81               '[g][h][i]': 'j'
82             }
83           }
84         }
85       }
86     }
87   }
88 }
89 ```
90
91 This depth can be overridden by passing a `depth` option to `Qs.parse(string, [options])`:
92
93 ```javascript
94 Qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1 });
95 // { a: { b: { '[c][d][e][f][g][h][i]': 'j' } } }
96 ```
97
98 The depth limit helps mitigate abuse when **qs** is used to parse user input, and it is recommended to keep it a reasonably small number.
99
100 For similar reasons, by default **qs** will only parse up to 1000 parameters. This can be overridden by passing a `parameterLimit` option:
101
102 ```javascript
103 Qs.parse('a=b&c=d', { parameterLimit: 1 });
104 // { a: 'b' }
105 ```
106
107 An optional delimiter can also be passed:
108
109 ```javascript
110 Qs.parse('a=b;c=d', { delimiter: ';' });
111 // { a: 'b', c: 'd' }
112 ```
113
114 Delimiters can be a regular expression too:
115
116 ```javascript
117 Qs.parse('a=b;c=d,e=f', { delimiter: /[;,]/ });
118 // { a: 'b', c: 'd', e: 'f' }
119 ```
120
121 Option `allowDots` can be used to enable dot notation:
122
123 ```javascript
124 Qs.parse('a.b=c', { allowDots: true });
125 // { a: { b: 'c' } }
126 ```
127
128 ### Parsing Arrays
129
130 **qs** can also parse arrays using a similar `[]` notation:
131
132 ```javascript
133 Qs.parse('a[]=b&a[]=c');
134 // { a: ['b', 'c'] }
135 ```
136
137 You may specify an index as well:
138
139 ```javascript
140 Qs.parse('a[1]=c&a[0]=b');
141 // { a: ['b', 'c'] }
142 ```
143
144 Note that the only difference between an index in an array and a key in an object is that the value between the brackets must be a number
145 to create an array. When creating arrays with specific indices, **qs** will compact a sparse array to only the existing values preserving
146 their order:
147
148 ```javascript
149 Qs.parse('a[1]=b&a[15]=c');
150 // { a: ['b', 'c'] }
151 ```
152
153 Note that an empty string is also a value, and will be preserved:
154
155 ```javascript
156 Qs.parse('a[]=&a[]=b');
157 // { a: ['', 'b'] }
158 Qs.parse('a[0]=b&a[1]=&a[2]=c');
159 // { a: ['b', '', 'c'] }
160 ```
161
162 **qs** will also limit specifying indices in an array to a maximum index of `20`. Any array members with an index of greater than `20` will
163 instead be converted to an object with the index as the key:
164
165 ```javascript
166 Qs.parse('a[100]=b');
167 // { a: { '100': 'b' } }
168 ```
169
170 This limit can be overridden by passing an `arrayLimit` option:
171
172 ```javascript
173 Qs.parse('a[1]=b', { arrayLimit: 0 });
174 // { a: { '1': 'b' } }
175 ```
176
177 To disable array parsing entirely, set `parseArrays` to `false`.
178
179 ```javascript
180 Qs.parse('a[]=b', { parseArrays: false });
181 // { a: { '0': 'b' } }
182 ```
183
184 If you mix notations, **qs** will merge the two items into an object:
185
186 ```javascript
187 Qs.parse('a[0]=b&a[b]=c');
188 // { a: { '0': 'b', b: 'c' } }
189 ```
190
191 You can also create arrays of objects:
192
193 ```javascript
194 Qs.parse('a[][b]=c');
195 // { a: [{ b: 'c' }] }
196 ```
197
198 ### Stringifying
199
200 ```javascript
201 Qs.stringify(object, [options]);
202 ```
203
204 When stringifying, **qs** by default URI encodes output. Objects are stringified as you would expect:
205
206 ```javascript
207 Qs.stringify({ a: 'b' });
208 // 'a=b'
209 Qs.stringify({ a: { b: 'c' } });
210 // 'a%5Bb%5D=c'
211 ```
212
213 This encoding can be disabled by setting the `encode` option to `false`:
214
215 ```javascript
216 Qs.stringify({ a: { b: 'c' } }, { encode: false });
217 // 'a[b]=c'
218 ```
219
220 Examples beyond this point will be shown as though the output is not URI encoded for clarity. Please note that the return values in these cases *will* be URI encoded during real usage.
221
222 When arrays are stringified, by default they are given explicit indices:
223
224 ```javascript
225 Qs.stringify({ a: ['b', 'c', 'd'] });
226 // 'a[0]=b&a[1]=c&a[2]=d'
227 ```
228
229 You may override this by setting the `indices` option to `false`:
230
231 ```javascript
232 Qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false });
233 // 'a=b&a=c&a=d'
234 ```
235
236 You may use the `arrayFormat` option to specify the format of the output array
237
238 ```javascript
239 Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })
240 // 'a[0]=b&a[1]=c'
241 Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })
242 // 'a[]=b&a[]=c'
243 Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })
244 // 'a=b&a=c'
245 ```
246
247 Empty strings and null values will omit the value, but the equals sign (=) remains in place:
248
249 ```javascript
250 Qs.stringify({ a: '' });
251 // 'a='
252 ```
253
254 Properties that are set to `undefined` will be omitted entirely:
255
256 ```javascript
257 Qs.stringify({ a: null, b: undefined });
258 // 'a='
259 ```
260
261 The delimiter may be overridden with stringify as well:
262
263 ```javascript
264 Qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' });
265 // 'a=b;c=d'
266 ```
267
268 Finally, you can use the `filter` option to restrict which keys will be included in the stringified output.
269 If you pass a function, it will be called for each key to obtain the replacement value. Otherwise, if you
270 pass an array, it will be used to select properties and array indices for stringification:
271
272 ```javascript
273 function filterFunc(prefix, value) {
274   if (prefix == 'b') {
275     // Return an `undefined` value to omit a property.
276     return;
277   }
278   if (prefix == 'e[f]') {
279     return value.getTime();
280   }
281   if (prefix == 'e[g][0]') {
282     return value * 2;
283   }
284   return value;
285 }
286 Qs.stringify({ a: 'b', c: 'd', e: { f: new Date(123), g: [2] } }, { filter: filterFunc })
287 // 'a=b&c=d&e[f]=123&e[g][0]=4'
288 Qs.stringify({ a: 'b', c: 'd', e: 'f' }, { filter: ['a', 'e'] })
289 // 'a=b&e=f'
290 Qs.stringify({ a: ['b', 'c', 'd'], e: 'f' }, { filter: ['a', 0, 2] })
291 // 'a[0]=b&a[2]=d'
292 ```
293
294 ### Handling of `null` values
295
296 By default, `null` values are treated like empty strings:
297
298 ```javascript
299 Qs.stringify({ a: null, b: '' });
300 // 'a=&b='
301 ```
302
303 Parsing does not distinguish between parameters with and without equal signs. Both are converted to empty strings.
304
305 ```javascript
306 Qs.parse('a&b=')
307 // { a: '', b: '' }
308 ```
309
310 To distinguish between `null` values and empty strings use the `strictNullHandling` flag. In the result string the `null`
311 values have no `=` sign:
312
313 ```javascript
314 Qs.stringify({ a: null, b: '' }, { strictNullHandling: true });
315 // 'a&b='
316 ```
317
318 To parse values without `=` back to `null` use the `strictNullHandling` flag:
319
320 ```javascript
321 Qs.parse('a&b=', { strictNullHandling: true });
322 // { a: null, b: '' }
323
324 ```
325
326 To completely skip rendering keys with `null` values, use the `skipNulls` flag:
327
328 ```javascript
329 qs.stringify({ a: 'b', c: null}, { skipNulls: true })
330 // 'a=b'
331 ```