query-string 6.7.0 → 6.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/index.d.ts +30 -14
  2. package/index.js +22 -5
  3. package/package.json +1 -1
  4. package/readme.md +20 -8
package/index.d.ts CHANGED
@@ -13,28 +13,28 @@ export interface ParseOptions {
13
13
 
14
14
  ```
15
15
  queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
16
- //=> foo: ['1', '2', '3']
16
+ //=> {foo: ['1', '2', '3']}
17
17
  ```
18
18
 
19
19
  - `index`: Parse arrays with index representation:
20
20
 
21
21
  ```
22
22
  queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
23
- //=> foo: ['1', '2', '3']
23
+ //=> {foo: ['1', '2', '3']}
24
24
  ```
25
25
 
26
26
  - `comma`: Parse arrays with elements separated by comma:
27
27
 
28
28
  ```
29
29
  queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
30
- //=> foo: ['1', '2', '3']
30
+ //=> {foo: ['1', '2', '3']}
31
31
  ```
32
32
 
33
33
  - `none`: Parse arrays with elements using duplicate keys:
34
34
 
35
35
  ```
36
36
  queryString.parse('foo=1&foo=2&foo=3');
37
- //=> foo: ['1', '2', '3']
37
+ //=> {foo: ['1', '2', '3']}
38
38
  ```
39
39
  */
40
40
  readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'none';
@@ -53,11 +53,11 @@ export interface ParseOptions {
53
53
  queryString.parse('?a=one&b=two&c=three', {
54
54
  sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
55
55
  });
56
- // => {c: 'three', a: 'one', b: 'two'}
56
+ //=> {c: 'three', a: 'one', b: 'two'}
57
57
  ```
58
58
 
59
59
  queryString.parse('?a=one&c=three&b=two', {sort: false});
60
- // => {a: 'one', c: 'three', b: 'two'}
60
+ //=> {a: 'one', c: 'three', b: 'two'}
61
61
  ```
62
62
  */
63
63
  readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
@@ -68,16 +68,29 @@ export interface ParseOptions {
68
68
  @default false
69
69
 
70
70
  @example
71
- ```
72
- queryString.parse('foo[]=1&foo[]=2&foo[]=3', {parseNumbers: true});
73
- //=> foo: [1, 2, 3]
71
+ ```js
72
+ queryString.parse('foo=1', {parseNumbers: true});
73
+ //=> {foo: 1}
74
74
  ```
75
75
  */
76
76
  readonly parseNumbers?: boolean;
77
+
78
+ /**
79
+ Parse the value as a boolean type instead of string type if it's a boolean.
80
+
81
+ @default false
82
+
83
+ @example
84
+ ```
85
+ queryString.parse('foo=true', {parseBooleans: true});
86
+ //=> {foo: true}
87
+ ```
88
+ */
89
+ readonly parseBooleans?: boolean;
77
90
  }
78
91
 
79
- export interface ParsedQuery {
80
- readonly [key: string]: string | number | Array<string | number> | null | undefined;
92
+ export interface ParsedQuery<T = string> {
93
+ [key: string]: T | T[] | null | undefined;
81
94
  }
82
95
 
83
96
  /**
@@ -87,6 +100,9 @@ The returned object is created with [`Object.create(null)`](https://developer.mo
87
100
 
88
101
  @param query - The query string to parse.
89
102
  */
103
+ export function parse(query: string, options: {parseBooleans: true, parseNumbers: true} & ParseOptions): ParsedQuery<string | boolean | number>;
104
+ export function parse(query: string, options: {parseBooleans: true} & ParseOptions): ParsedQuery<string | boolean>;
105
+ export function parse(query: string, options: {parseNumbers: true} & ParseOptions): ParsedQuery<string | number>;
90
106
  export function parse(query: string, options?: ParseOptions): ParsedQuery;
91
107
 
92
108
  export interface ParsedUrl {
@@ -136,7 +152,7 @@ export interface StringifyOptions {
136
152
 
137
153
  ```
138
154
  queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
139
- //=> 'foo[0]=1&foo[1]=2&foo[3]=3'
155
+ //=> 'foo[0]=1&foo[1]=2&foo[2]=3'
140
156
  ```
141
157
 
142
158
  - `comma`: Serialize arrays by separating elements with comma:
@@ -169,10 +185,10 @@ export interface StringifyOptions {
169
185
  queryString.stringify({a: 1, b: 2, c: 3}, {
170
186
  sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
171
187
  });
172
- // => 'c=3&a=1&b=2'
188
+ //=> 'c=3&a=1&b=2'
173
189
 
174
190
  queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
175
- // => 'b=1&c=2&a=3'
191
+ //=> 'b=1&c=2&a=3'
176
192
  ```
177
193
  */
178
194
  readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
package/index.js CHANGED
@@ -171,12 +171,23 @@ function extract(input) {
171
171
  return input.slice(queryStart + 1);
172
172
  }
173
173
 
174
+ function parseValue(value, options) {
175
+ if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
176
+ value = Number(value);
177
+ } else if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
178
+ value = value.toLowerCase() === 'true';
179
+ }
180
+
181
+ return value;
182
+ }
183
+
174
184
  function parse(input, options) {
175
185
  options = Object.assign({
176
186
  decode: true,
177
187
  sort: true,
178
188
  arrayFormat: 'none',
179
- parseNumbers: false
189
+ parseNumbers: false,
190
+ parseBooleans: false
180
191
  }, options);
181
192
 
182
193
  const formatter = parserForArrayFormat(options);
@@ -200,12 +211,18 @@ function parse(input, options) {
200
211
  // Missing `=` should be `null`:
201
212
  // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
202
213
  value = value === undefined ? null : decode(value, options);
214
+ formatter(decode(key, options), value, ret);
215
+ }
203
216
 
204
- if (options.parseNumbers && !Number.isNaN(Number(value))) {
205
- value = Number(value);
217
+ for (const key of Object.keys(ret)) {
218
+ const value = ret[key];
219
+ if (typeof value === 'object' && value !== null) {
220
+ for (const k of Object.keys(value)) {
221
+ value[k] = parseValue(value[k], options);
222
+ }
223
+ } else {
224
+ ret[key] = parseValue(value, options);
206
225
  }
207
-
208
- formatter(decode(key, options), value, ret);
209
226
  }
210
227
 
211
228
  if (options.sort === false) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-string",
3
- "version": "6.7.0",
3
+ "version": "6.8.3",
4
4
  "description": "Parse and stringify URL query strings",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/query-string",
package/readme.md CHANGED
@@ -72,28 +72,28 @@ Default: `'none'`
72
72
 
73
73
  ```js
74
74
  queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
75
- //=> foo: ['1', '2', '3']
75
+ //=> {foo: ['1', '2', '3']}
76
76
  ```
77
77
 
78
78
  - `'index'`: Parse arrays with index representation:
79
79
 
80
80
  ```js
81
81
  queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
82
- //=> foo: ['1', '2', '3']
82
+ //=> {foo: ['1', '2', '3']}
83
83
  ```
84
84
 
85
85
  - `'comma'`: Parse arrays with elements separated by comma:
86
86
 
87
87
  ```js
88
88
  queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
89
- //=> foo: ['1', '2', '3']
89
+ //=> {foo: ['1', '2', '3']}
90
90
  ```
91
91
 
92
92
  - `'none'`: Parse arrays with elements using duplicate keys:
93
93
 
94
94
  ```js
95
95
  queryString.parse('foo=1&foo=2&foo=3');
96
- //=> foo: ['1', '2', '3']
96
+ //=> {foo: ['1', '2', '3']}
97
97
  ```
98
98
 
99
99
  ##### sort
@@ -109,12 +109,24 @@ Type: `boolean`<br>
109
109
  Default: `false`
110
110
 
111
111
  ```js
112
- queryString.parse('foo[]=1&foo[]=2&foo[]=3', {parseNumbers: true});
113
- //=> foo: [1, 2, 3]
112
+ queryString.parse('foo=1', {parseNumbers: true});
113
+ //=> {foo: 1}
114
114
  ```
115
115
 
116
116
  Parse the value as a number type instead of string type if it's a number.
117
117
 
118
+ ##### parseBooleans
119
+
120
+ Type: `boolean`<br>
121
+ Default: `false`
122
+
123
+ ```js
124
+ queryString.parse('foo=true', {parseBooleans: true});
125
+ //=> {foo: true}
126
+ ```
127
+
128
+ Parse the value as a boolean type instead of string type if it's a boolean.
129
+
118
130
  ### .stringify(object, [options])
119
131
 
120
132
  Stringify an object into a query string and sorting the keys.
@@ -153,7 +165,7 @@ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
153
165
 
154
166
  ```js
155
167
  queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
156
- //=> 'foo[0]=1&foo[1]=2&foo[3]=3'
168
+ //=> 'foo[0]=1&foo[1]=2&foo[2]=3'
157
169
  ```
158
170
 
159
171
  - `'comma'`: Serialize arrays by separating elements with comma:
@@ -233,7 +245,7 @@ queryString.parse('likes=cake&name=bob&likes=icecream');
233
245
  //=> {likes: ['cake', 'icecream'], name: 'bob'}
234
246
 
235
247
  queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'});
236
- //=> 'color=chartreuse&color=taupe&id=515'
248
+ //=> 'color=taupe&color=chartreuse&id=515'
237
249
  ```
238
250
 
239
251