项目原始demo,不改动
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

4 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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](http://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. You might also like his <a href="https://ReactForBeginners.com/friend/AWESOME">React course</a>.</p>
  5. ---
  6. ## Install
  7. ```
  8. $ npm install --save query-string
  9. ```
  10. ## Usage
  11. ```js
  12. const queryString = require('query-string');
  13. console.log(location.search);
  14. //=> '?foo=bar'
  15. const parsed = queryString.parse(location.search);
  16. console.log(parsed);
  17. //=> {foo: 'bar'}
  18. console.log(location.hash);
  19. //=> '#token=bada55cafe'
  20. const parsedHash = queryString.parse(location.hash);
  21. console.log(parsedHash);
  22. //=> {token: 'bada55cafe'}
  23. parsed.foo = 'unicorn';
  24. parsed.ilike = 'pizza';
  25. const stringified = queryString.stringify(parsed);
  26. //=> 'foo=unicorn&ilike=pizza'
  27. location.search = stringified;
  28. // note that `location.search` automatically prepends a question mark
  29. console.log(location.search);
  30. //=> '?foo=unicorn&ilike=pizza'
  31. ```
  32. ## API
  33. ### .parse(*string*, *[options]*)
  34. Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.
  35. 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`.
  36. #### arrayFormat
  37. Type: `string`<br>
  38. Default: `'none'`
  39. Supports both `index` for an indexed array representation or `bracket` for a *bracketed* array representation.
  40. - `bracket`: stands for parsing correctly arrays with bracket representation on the query string, such as:
  41. ```js
  42. queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
  43. //=> foo: [1,2,3]
  44. ```
  45. - `index`: stands for parsing taking the index into account, such as:
  46. ```js
  47. queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
  48. //=> foo: [1,2,3]
  49. ```
  50. - `none`: is the **default** option and removes any bracket representation, such as:
  51. ```js
  52. queryString.parse('foo=1&foo=2&foo=3');
  53. //=> foo: [1,2,3]
  54. ```
  55. ### .stringify(*object*, *[options]*)
  56. Stringify an object into a query string, sorting the keys.
  57. #### strict
  58. Type: `boolean`<br>
  59. Default: `true`
  60. 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)
  61. if set to false. You probably [don't care](https://github.com/sindresorhus/query-string/issues/42) about this option.
  62. #### encode
  63. Type: `boolean`<br>
  64. Default: `true`
  65. [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
  66. #### arrayFormat
  67. Type: `string`<br>
  68. Default: `'none'`
  69. Supports both `index` for an indexed array representation or `bracket` for a *bracketed* array representation.
  70. - `bracket`: stands for parsing correctly arrays with bracket representation on the query string, such as:
  71. ```js
  72. queryString.stringify({foo: [1,2,3]}, {arrayFormat: 'bracket'});
  73. // => foo[]=1&foo[]=2&foo[]=3
  74. ```
  75. - `index`: stands for parsing taking the index into account, such as:
  76. ```js
  77. queryString.stringify({foo: [1,2,3]}, {arrayFormat: 'index'});
  78. // => foo[0]=1&foo[1]=2&foo[3]=3
  79. ```
  80. - `none`: is the __default__ option and removes any bracket representation, such as:
  81. ```js
  82. queryString.stringify({foo: [1,2,3]});
  83. // => foo=1&foo=2&foo=3
  84. ```
  85. ### .extract(*string*)
  86. Extract a query string from a URL that can be passed into `.parse()`.
  87. ## Nesting
  88. 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).
  89. You're much better off just converting the object to a JSON string:
  90. ```js
  91. queryString.stringify({
  92. foo: 'bar',
  93. nested: JSON.stringify({
  94. unicorn: 'cake'
  95. })
  96. });
  97. //=> 'foo=bar&nested=%7B%22unicorn%22%3A%22cake%22%7D'
  98. ```
  99. However, there is support for multiple instances of the same key:
  100. ```js
  101. queryString.parse('likes=cake&name=bob&likes=icecream');
  102. //=> {likes: ['cake', 'icecream'], name: 'bob'}
  103. queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'});
  104. //=> 'color=chartreuse&color=taupe&id=515'
  105. ```
  106. ## Falsy values
  107. 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:
  108. ```js
  109. queryString.stringify({foo: false});
  110. //=> 'foo=false'
  111. queryString.stringify({foo: null});
  112. //=> 'foo'
  113. queryString.stringify({foo: undefined});
  114. //=> ''
  115. ```
  116. ## License
  117. MIT © [Sindre Sorhus](https://sindresorhus.com)