PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/node-v4.1.0-windows-x86/lib/node_modules/npm/node_modules/request/node_modules/qs/Readme.md

https://gitlab.com/zerotonin/indinero_connect4
Markdown | 317 lines | 237 code | 80 blank | 0 comment | 0 complexity | e9bad04d45b4ac96ec04850c77767efb MD5 | raw file
  1. # qs
  2. A querystring parsing and stringifying library with some added security.
  3. [![Build Status](https://secure.travis-ci.org/hapijs/qs.svg)](http://travis-ci.org/hapijs/qs)
  4. Lead Maintainer: [Nathan LaFreniere](https://github.com/nlf)
  5. The **qs** module was originally created and maintained by [TJ Holowaychuk](https://github.com/visionmedia/node-querystring).
  6. ## Usage
  7. ```javascript
  8. var Qs = require('qs');
  9. var obj = Qs.parse('a=c'); // { a: 'c' }
  10. var str = Qs.stringify(obj); // 'a=c'
  11. ```
  12. ### Parsing Objects
  13. ```javascript
  14. Qs.parse(string, [options]);
  15. ```
  16. **qs** allows you to create nested objects within your query strings, by surrounding the name of sub-keys with square brackets `[]`, or prefixing the sub-key with a dot `.`.
  17. For example, the string `'foo[bar]=baz'` converts to:
  18. ```javascript
  19. {
  20. foo: {
  21. bar: 'baz'
  22. }
  23. }
  24. ```
  25. 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:
  26. ```javascript
  27. Qs.parse('a.hasOwnProperty=b', { plainObjects: true });
  28. // { a: { hasOwnProperty: 'b' } }
  29. ```
  30. 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.
  31. ```javascript
  32. Qs.parse('a.hasOwnProperty=b', { allowPrototypes: true });
  33. // { a: { hasOwnProperty: 'b' } }
  34. ```
  35. URI encoded strings work too:
  36. ```javascript
  37. Qs.parse('a%5Bb%5D=c');
  38. // { a: { b: 'c' } }
  39. ```
  40. You can also nest your objects, like `'foo[bar][baz]=foobarbaz'`:
  41. ```javascript
  42. {
  43. foo: {
  44. bar: {
  45. baz: 'foobarbaz'
  46. }
  47. }
  48. }
  49. ```
  50. By default, when nesting objects **qs** will only parse up to 5 children deep. This means if you attempt to parse a string like
  51. `'a[b][c][d][e][f][g][h][i]=j'` your resulting object will be:
  52. ```javascript
  53. {
  54. a: {
  55. b: {
  56. c: {
  57. d: {
  58. e: {
  59. f: {
  60. '[g][h][i]': 'j'
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. ```
  69. This depth can be overridden by passing a `depth` option to `Qs.parse(string, [options])`:
  70. ```javascript
  71. Qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1 });
  72. // { a: { b: { '[c][d][e][f][g][h][i]': 'j' } } }
  73. ```
  74. 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.
  75. For similar reasons, by default **qs** will only parse up to 1000 parameters. This can be overridden by passing a `parameterLimit` option:
  76. ```javascript
  77. Qs.parse('a=b&c=d', { parameterLimit: 1 });
  78. // { a: 'b' }
  79. ```
  80. An optional delimiter can also be passed:
  81. ```javascript
  82. Qs.parse('a=b;c=d', { delimiter: ';' });
  83. // { a: 'b', c: 'd' }
  84. ```
  85. Delimiters can be a regular expression too:
  86. ```javascript
  87. Qs.parse('a=b;c=d,e=f', { delimiter: /[;,]/ });
  88. // { a: 'b', c: 'd', e: 'f' }
  89. ```
  90. Option `allowDots` can be used to disable dot notation:
  91. ```javascript
  92. Qs.parse('a.b=c', { allowDots: false });
  93. // { 'a.b': 'c' } }
  94. ```
  95. ### Parsing Arrays
  96. **qs** can also parse arrays using a similar `[]` notation:
  97. ```javascript
  98. Qs.parse('a[]=b&a[]=c');
  99. // { a: ['b', 'c'] }
  100. ```
  101. You may specify an index as well:
  102. ```javascript
  103. Qs.parse('a[1]=c&a[0]=b');
  104. // { a: ['b', 'c'] }
  105. ```
  106. 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
  107. to create an array. When creating arrays with specific indices, **qs** will compact a sparse array to only the existing values preserving
  108. their order:
  109. ```javascript
  110. Qs.parse('a[1]=b&a[15]=c');
  111. // { a: ['b', 'c'] }
  112. ```
  113. Note that an empty string is also a value, and will be preserved:
  114. ```javascript
  115. Qs.parse('a[]=&a[]=b');
  116. // { a: ['', 'b'] }
  117. Qs.parse('a[0]=b&a[1]=&a[2]=c');
  118. // { a: ['b', '', 'c'] }
  119. ```
  120. **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
  121. instead be converted to an object with the index as the key:
  122. ```javascript
  123. Qs.parse('a[100]=b');
  124. // { a: { '100': 'b' } }
  125. ```
  126. This limit can be overridden by passing an `arrayLimit` option:
  127. ```javascript
  128. Qs.parse('a[1]=b', { arrayLimit: 0 });
  129. // { a: { '1': 'b' } }
  130. ```
  131. To disable array parsing entirely, set `parseArrays` to `false`.
  132. ```javascript
  133. Qs.parse('a[]=b', { parseArrays: false });
  134. // { a: { '0': 'b' } }
  135. ```
  136. If you mix notations, **qs** will merge the two items into an object:
  137. ```javascript
  138. Qs.parse('a[0]=b&a[b]=c');
  139. // { a: { '0': 'b', b: 'c' } }
  140. ```
  141. You can also create arrays of objects:
  142. ```javascript
  143. Qs.parse('a[][b]=c');
  144. // { a: [{ b: 'c' }] }
  145. ```
  146. ### Stringifying
  147. ```javascript
  148. Qs.stringify(object, [options]);
  149. ```
  150. When stringifying, **qs** always URI encodes output. Objects are stringified as you would expect:
  151. ```javascript
  152. Qs.stringify({ a: 'b' });
  153. // 'a=b'
  154. Qs.stringify({ a: { b: 'c' } });
  155. // 'a%5Bb%5D=c'
  156. ```
  157. 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.
  158. When arrays are stringified, by default they are given explicit indices:
  159. ```javascript
  160. Qs.stringify({ a: ['b', 'c', 'd'] });
  161. // 'a[0]=b&a[1]=c&a[2]=d'
  162. ```
  163. You may override this by setting the `indices` option to `false`:
  164. ```javascript
  165. Qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false });
  166. // 'a=b&a=c&a=d'
  167. ```
  168. You may use the `arrayFormat` option to specify the format of the output array
  169. ```javascript
  170. Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })
  171. // 'a[0]=b&a[1]=c'
  172. Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })
  173. // 'a[]=b&a[]=c'
  174. Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })
  175. // 'a=b&a=c'
  176. ```
  177. Empty strings and null values will omit the value, but the equals sign (=) remains in place:
  178. ```javascript
  179. Qs.stringify({ a: '' });
  180. // 'a='
  181. ```
  182. Properties that are set to `undefined` will be omitted entirely:
  183. ```javascript
  184. Qs.stringify({ a: null, b: undefined });
  185. // 'a='
  186. ```
  187. The delimiter may be overridden with stringify as well:
  188. ```javascript
  189. Qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' });
  190. // 'a=b;c=d'
  191. ```
  192. Finally, you can use the `filter` option to restrict which keys will be included in the stringified output.
  193. If you pass a function, it will be called for each key to obtain the replacement value. Otherwise, if you
  194. pass an array, it will be used to select properties and array indices for stringification:
  195. ```javascript
  196. function filterFunc(prefix, value) {
  197. if (prefix == 'b') {
  198. // Return an `undefined` value to omit a property.
  199. return;
  200. }
  201. if (prefix == 'e[f]') {
  202. return value.getTime();
  203. }
  204. if (prefix == 'e[g][0]') {
  205. return value * 2;
  206. }
  207. return value;
  208. }
  209. Qs.stringify({ a: 'b', c: 'd', e: { f: new Date(123), g: [2] } }, { filter: filterFunc })
  210. // 'a=b&c=d&e[f]=123&e[g][0]=4'
  211. Qs.stringify({ a: 'b', c: 'd', e: 'f' }, { filter: ['a', 'e'] })
  212. // 'a=b&e=f'
  213. Qs.stringify({ a: ['b', 'c', 'd'], e: 'f' }, { filter: ['a', 0, 2] })
  214. // 'a[0]=b&a[2]=d'
  215. ```
  216. ### Handling of `null` values
  217. By default, `null` values are treated like empty strings:
  218. ```javascript
  219. Qs.stringify({ a: null, b: '' });
  220. // 'a=&b='
  221. ```
  222. Parsing does not distinguish between parameters with and without equal signs. Both are converted to empty strings.
  223. ```javascript
  224. Qs.parse('a&b=')
  225. // { a: '', b: '' }
  226. ```
  227. To distinguish between `null` values and empty strings use the `strictNullHandling` flag. In the result string the `null`
  228. values have no `=` sign:
  229. ```javascript
  230. Qs.stringify({ a: null, b: '' }, { strictNullHandling: true });
  231. // 'a&b='
  232. ```
  233. To parse values without `=` back to `null` use the `strictNullHandling` flag:
  234. ```javascript
  235. Qs.parse('a&b=', { strictNullHandling: true });
  236. // { a: null, b: '' }
  237. ```