You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.md 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # query-string [![Build Status](https://travis-ci.org/sindresorhus/query-string.svg?branch=master)](https://travis-ci.org/sindresorhus/query-string)
  2. > Parse and stringify URL [query strings](https://en.wikipedia.org/wiki/Query_string)
  3. ---
  4. <p align="center"><b>🔥 Want to strengthen your core JavaScript skills and master ES6?</b><br>I would personally recommend this awesome <a href="https://ES6.io/friend/AWESOME">ES6 course</a> by Wes Bos.<br>Also check out his <a href="https://LearnNode.com/friend/AWESOME">Node.js</a>, <a href="https://ReactForBeginners.com/friend/AWESOME">React</a>, <a href="https://SublimeTextBook.com/friend/AWESOME">Sublime</a> courses.</p>
  5. ---
  6. ## Install
  7. ```
  8. $ npm install query-string
  9. ```
  10. <a href="https://www.patreon.com/sindresorhus">
  11. <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
  12. </a>
  13. ## Usage
  14. ```js
  15. const queryString = require('query-string');
  16. console.log(location.search);
  17. //=> '?foo=bar'
  18. const parsed = queryString.parse(location.search);
  19. console.log(parsed);
  20. //=> {foo: 'bar'}
  21. console.log(location.hash);
  22. //=> '#token=bada55cafe'
  23. const parsedHash = queryString.parse(location.hash);
  24. console.log(parsedHash);
  25. //=> {token: 'bada55cafe'}
  26. parsed.foo = 'unicorn';
  27. parsed.ilike = 'pizza';
  28. const stringified = queryString.stringify(parsed);
  29. //=> 'foo=unicorn&ilike=pizza'
  30. location.search = stringified;
  31. // note that `location.search` automatically prepends a question mark
  32. console.log(location.search);
  33. //=> '?foo=unicorn&ilike=pizza'
  34. ```
  35. ## API
  36. ### .parse(*string*, *[options]*)
  37. Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.
  38. The 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`.
  39. URI components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
  40. #### arrayFormat
  41. Type: `string`<br>
  42. Default: `'none'`
  43. Supports both `index` for an indexed array representation or `bracket` for a *bracketed* array representation.
  44. - `bracket`: stands for parsing correctly arrays with bracket representation on the query string, such as:
  45. ```js
  46. queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
  47. //=> foo: [1,2,3]
  48. ```
  49. - `index`: stands for parsing taking the index into account, such as:
  50. ```js
  51. queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
  52. //=> foo: [1,2,3]
  53. ```
  54. - `none`: is the **default** option and removes any bracket representation, such as:
  55. ```js
  56. queryString.parse('foo=1&foo=2&foo=3');
  57. //=> foo: [1,2,3]
  58. ```
  59. ### .stringify(*object*, *[options]*)
  60. Stringify an object into a query string, sorting the keys.
  61. #### strict
  62. Type: `boolean`<br>
  63. Default: `true`
  64. Strictly 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)
  65. if set to false. You probably [don't care](https://github.com/sindresorhus/query-string/issues/42) about this option.
  66. #### encode
  67. Type: `boolean`<br>
  68. Default: `true`
  69. [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
  70. #### arrayFormat
  71. Type: `string`<br>
  72. Default: `'none'`
  73. Supports both `index` for an indexed array representation or `bracket` for a *bracketed* array representation.
  74. - `bracket`: stands for parsing correctly arrays with bracket representation on the query string, such as:
  75. ```js
  76. queryString.stringify({foo: [1,2,3]}, {arrayFormat: 'bracket'});
  77. // => foo[]=1&foo[]=2&foo[]=3
  78. ```
  79. - `index`: stands for parsing taking the index into account, such as:
  80. ```js
  81. queryString.stringify({foo: [1,2,3]}, {arrayFormat: 'index'});
  82. // => foo[0]=1&foo[1]=2&foo[3]=3
  83. ```
  84. - `none`: is the __default__ option and removes any bracket representation, such as:
  85. ```js
  86. queryString.stringify({foo: [1,2,3]});
  87. // => foo=1&foo=2&foo=3
  88. ```
  89. #### sort
  90. Type: `Function` `boolean`
  91. Supports both `Function` as a custom sorting function or `false` to disable sorting.
  92. ```js
  93. const order = ['c', 'a', 'b'];
  94. queryString.stringify({ a: 1, b: 2, c: 3}, {
  95. sort: (m, n) => order.indexOf(m) >= order.indexOf(n)
  96. });
  97. // => 'c=3&a=1&b=2'
  98. ```
  99. ```js
  100. queryString.stringify({ b: 1, c: 2, a: 3}, {sort: false});
  101. // => 'c=3&a=1&b=2'
  102. ```
  103. If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
  104. ### .extract(*string*)
  105. Extract a query string from a URL that can be passed into `.parse()`.
  106. ### .parseUrl(*string*, *[options]*)
  107. Extract the URL and the query string as an object.
  108. The `options` are the same as for `.parse()`.
  109. Returns an object with a `url` and `query` property.
  110. ```js
  111. queryString.parseUrl('https://foo.bar?foo=bar');
  112. //=> {url: 'https://foo.bar', query: {foo: 'bar'}}
  113. ```
  114. ## Nesting
  115. This 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).
  116. You're much better off just converting the object to a JSON string:
  117. ```js
  118. queryString.stringify({
  119. foo: 'bar',
  120. nested: JSON.stringify({
  121. unicorn: 'cake'
  122. })
  123. });
  124. //=> 'foo=bar&nested=%7B%22unicorn%22%3A%22cake%22%7D'
  125. ```
  126. However, there is support for multiple instances of the same key:
  127. ```js
  128. queryString.parse('likes=cake&name=bob&likes=icecream');
  129. //=> {likes: ['cake', 'icecream'], name: 'bob'}
  130. queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'});
  131. //=> 'color=chartreuse&color=taupe&id=515'
  132. ```
  133. ## Falsy values
  134. Sometimes 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:
  135. ```js
  136. queryString.stringify({foo: false});
  137. //=> 'foo=false'
  138. queryString.stringify({foo: null});
  139. //=> 'foo'
  140. queryString.stringify({foo: undefined});
  141. //=> ''
  142. ```
  143. ## License
  144. MIT © [Sindre Sorhus](https://sindresorhus.com)