query-string 6.5.0 → 6.8.1

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 +166 -101
  2. package/index.js +15 -2
  3. package/package.json +3 -3
  4. package/readme.md +61 -26
package/index.d.ts CHANGED
@@ -1,52 +1,108 @@
1
1
  export interface ParseOptions {
2
2
  /**
3
- * Decode the keys and values. URI components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
4
- *
5
- * @default true
6
- */
3
+ Decode the keys and values. URI components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
4
+
5
+ @default true
6
+ */
7
7
  readonly decode?: boolean;
8
8
 
9
9
  /**
10
- * @default 'none'
11
- *
12
- * - `bracket`: Parse arrays with bracket representation:
13
- *
14
- *
15
- * queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
16
- * //=> foo: [1, 2, 3]
17
- *
18
- * - `index`: Parse arrays with index representation:
19
- *
20
- *
21
- * queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
22
- * //=> foo: [1, 2, 3]
23
- *
24
- * - `comma`: Parse arrays with elements separated by comma:
25
- *
26
- *
27
- * queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
28
- * //=> foo: [1, 2, 3]
29
- *
30
- * - `none`: Parse arrays with elements using duplicate keys:
31
- *
32
- *
33
- * queryString.parse('foo=1&foo=2&foo=3');
34
- * //=> foo: [1, 2, 3]
35
- */
10
+ @default 'none'
11
+
12
+ - `bracket`: Parse arrays with bracket representation:
13
+
14
+ ```
15
+ queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
16
+ //=> {foo: ['1', '2', '3']}
17
+ ```
18
+
19
+ - `index`: Parse arrays with index representation:
20
+
21
+ ```
22
+ queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
23
+ //=> {foo: ['1', '2', '3']}
24
+ ```
25
+
26
+ - `comma`: Parse arrays with elements separated by comma:
27
+
28
+ ```
29
+ queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
30
+ //=> {foo: ['1', '2', '3']}
31
+ ```
32
+
33
+ - `none`: Parse arrays with elements using duplicate keys:
34
+
35
+ ```
36
+ queryString.parse('foo=1&foo=2&foo=3');
37
+ //=> {foo: ['1', '2', '3']}
38
+ ```
39
+ */
36
40
  readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'none';
41
+
42
+ /**
43
+ Supports both `Function` as a custom sorting function or `false` to disable sorting.
44
+
45
+ If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
46
+
47
+ @default true
48
+
49
+ @example
50
+ ```
51
+ const order = ['c', 'a', 'b'];
52
+
53
+ queryString.parse('?a=one&b=two&c=three', {
54
+ sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
55
+ });
56
+ // => {c: 'three', a: 'one', b: 'two'}
57
+ ```
58
+
59
+ queryString.parse('?a=one&c=three&b=two', {sort: false});
60
+ // => {a: 'one', c: 'three', b: 'two'}
61
+ ```
62
+ */
63
+ readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
64
+
65
+ /**
66
+ Parse the value as a number type instead of string type if it's a number.
67
+
68
+ @default false
69
+
70
+ @example
71
+ ```js
72
+ queryString.parse('foo=1', {parseNumbers: true});
73
+ //=> {foo: 1}
74
+ ```
75
+ */
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;
37
90
  }
38
91
 
39
- export interface ParsedQuery {
40
- readonly [key: string]: string | string[] | null | undefined;
92
+ export interface ParsedQuery<T = string> {
93
+ [key: string]: T | T[] | null | undefined;
41
94
  }
42
95
 
43
96
  /**
44
- * Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.
45
- *
46
- * 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`.
47
- *
48
- * @param query - The query string to parse.
49
- */
97
+ Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.
98
+
99
+ 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`.
100
+
101
+ @param query - The query string to parse.
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>;
50
106
  export function parse(query: string, options?: ParseOptions): ParsedQuery;
51
107
 
52
108
  export interface ParsedUrl {
@@ -55,89 +111,98 @@ export interface ParsedUrl {
55
111
  }
56
112
 
57
113
  /**
58
- * Extract the URL and the query string as an object.
59
- *
60
- * @param url - The URL to parse.
61
- *
62
- * @example
63
- *
64
- * queryString.parseUrl('https://foo.bar?foo=bar');
65
- * //=> {url: 'https://foo.bar', query: {foo: 'bar'}}
66
- */
114
+ Extract the URL and the query string as an object.
115
+
116
+ @param url - The URL to parse.
117
+
118
+ @example
119
+ ```
120
+ queryString.parseUrl('https://foo.bar?foo=bar');
121
+ //=> {url: 'https://foo.bar', query: {foo: 'bar'}}
122
+ ```
123
+ */
67
124
  export function parseUrl(url: string, options?: ParseOptions): ParsedUrl;
68
125
 
69
126
  export interface StringifyOptions {
70
127
  /**
71
- * 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) if set to `false`. You probably [don't care](https://github.com/sindresorhus/query-string/issues/42) about this option.
72
- *
73
- * @default true
74
- */
128
+ 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) if set to `false`. You probably [don't care](https://github.com/sindresorhus/query-string/issues/42) about this option.
129
+
130
+ @default true
131
+ */
75
132
  readonly strict?: boolean;
76
133
 
77
134
  /**
78
- * [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
79
- *
80
- * @default true
81
- */
135
+ [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
136
+
137
+ @default true
138
+ */
82
139
  readonly encode?: boolean;
83
140
 
84
141
  /**
85
- * @default 'none'
86
- *
87
- * - `bracket`: Serialize arrays using bracket representation:
88
- *
89
- *
90
- * queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
91
- * //=> 'foo[]=1&foo[]=2&foo[]=3'
92
- *
93
- * - `index`: Serialize arrays using index representation:
94
- *
95
- *
96
- * queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
97
- * //=> 'foo[0]=1&foo[1]=2&foo[3]=3'
98
- *
99
- * - `comma`: Serialize arrays by separating elements with comma:
100
- *
101
- *
102
- * queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
103
- * //=> 'foo=1,2,3'
104
- *
105
- * - `none`: Serialize arrays by using duplicate keys:
106
- *
107
- *
108
- * queryString.stringify({foo: [1, 2, 3]});
109
- * //=> 'foo=1&foo=2&foo=3'
110
- */
142
+ @default 'none'
143
+
144
+ - `bracket`: Serialize arrays using bracket representation:
145
+
146
+ ```
147
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
148
+ //=> 'foo[]=1&foo[]=2&foo[]=3'
149
+ ```
150
+
151
+ - `index`: Serialize arrays using index representation:
152
+
153
+ ```
154
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
155
+ //=> 'foo[0]=1&foo[1]=2&foo[2]=3'
156
+ ```
157
+
158
+ - `comma`: Serialize arrays by separating elements with comma:
159
+
160
+ ```
161
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
162
+ //=> 'foo=1,2,3'
163
+ ```
164
+
165
+ - `none`: Serialize arrays by using duplicate keys:
166
+
167
+ ```
168
+ queryString.stringify({foo: [1, 2, 3]});
169
+ //=> 'foo=1&foo=2&foo=3'
170
+ ```
171
+ */
111
172
  readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'none';
112
173
 
113
174
  /**
114
- * Supports both `Function` as a custom sorting function or `false` to disable sorting.
115
- *
116
- * If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
117
- *
118
- * @example
119
- *
120
- * const order = ['c', 'a', 'b'];
121
- * queryString.stringify({a: 1, b: 2, c: 3}, {
122
- * sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
123
- * });
124
- * // => 'c=3&a=1&b=2'
125
- *
126
- * queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
127
- * // => 'b=1&c=2&a=3'
128
- */
175
+ Supports both `Function` as a custom sorting function or `false` to disable sorting.
176
+
177
+ If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
178
+
179
+ @default true
180
+
181
+ @example
182
+ ```
183
+ const order = ['c', 'a', 'b'];
184
+
185
+ queryString.stringify({a: 1, b: 2, c: 3}, {
186
+ sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
187
+ });
188
+ // => 'c=3&a=1&b=2'
189
+
190
+ queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
191
+ // => 'b=1&c=2&a=3'
192
+ ```
193
+ */
129
194
  readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
130
195
  }
131
196
 
132
197
  /**
133
- * Stringify an object into a query string and sorting the keys.
134
- */
198
+ Stringify an object into a query string and sort the keys.
199
+ */
135
200
  export function stringify(
136
- object: {[key: string]: unknown},
201
+ object: {[key: string]: any},
137
202
  options?: StringifyOptions
138
203
  ): string;
139
204
 
140
205
  /**
141
- * Extract a query string from a URL that can be passed into `.parse()`.
142
- */
206
+ Extract a query string from a URL that can be passed into `.parse()`.
207
+ */
143
208
  export function extract(url: string): string;
package/index.js CHANGED
@@ -174,7 +174,10 @@ function extract(input) {
174
174
  function parse(input, options) {
175
175
  options = Object.assign({
176
176
  decode: true,
177
- arrayFormat: 'none'
177
+ sort: true,
178
+ arrayFormat: 'none',
179
+ parseNumbers: false,
180
+ parseBooleans: false
178
181
  }, options);
179
182
 
180
183
  const formatter = parserForArrayFormat(options);
@@ -199,10 +202,20 @@ function parse(input, options) {
199
202
  // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
200
203
  value = value === undefined ? null : decode(value, options);
201
204
 
205
+ if (options.parseNumbers && !Number.isNaN(Number(value))) {
206
+ value = Number(value);
207
+ } else if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
208
+ value = value.toLowerCase() === 'true';
209
+ }
210
+
202
211
  formatter(decode(key, options), value, ret);
203
212
  }
204
213
 
205
- return Object.keys(ret).sort().reduce((result, key) => {
214
+ if (options.sort === false) {
215
+ return ret;
216
+ }
217
+
218
+ return (options.sort === true ? Object.keys(ret).sort() : Object.keys(ret).sort(options.sort)).reduce((result, key) => {
206
219
  const value = ret[key];
207
220
  if (Boolean(value) && typeof value === 'object' && !Array.isArray(value)) {
208
221
  // Sort object keys, not values
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-string",
3
- "version": "6.5.0",
3
+ "version": "6.8.1",
4
4
  "description": "Parse and stringify URL query strings",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/query-string",
@@ -40,10 +40,10 @@
40
40
  "strict-uri-encode": "^2.0.0"
41
41
  },
42
42
  "devDependencies": {
43
- "ava": "^1.3.1",
43
+ "ava": "^1.4.1",
44
44
  "deep-equal": "^1.0.1",
45
45
  "fast-check": "^1.5.0",
46
- "tsd": "^0.7.0",
46
+ "tsd": "^0.7.3",
47
47
  "xo": "^0.24.0"
48
48
  }
49
49
  }
package/readme.md CHANGED
@@ -11,10 +11,6 @@ $ npm install query-string
11
11
 
12
12
  This module targets Node.js 6 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, or, [if your project is using create-react-app v1](https://github.com/sindresorhus/query-string/pull/148#issuecomment-399656020), use version 5: `npm install query-string@5`.
13
13
 
14
- <a href="https://www.patreon.com/sindresorhus">
15
- <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
16
- </a>
17
-
18
14
 
19
15
  ## Usage
20
16
 
@@ -50,7 +46,7 @@ console.log(location.search);
50
46
 
51
47
  ## API
52
48
 
53
- ### .parse(string, [options])
49
+ ### .parse(string, options?)
54
50
 
55
51
  Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.
56
52
 
@@ -58,7 +54,7 @@ The returned object is created with [`Object.create(null)`](https://developer.mo
58
54
 
59
55
  #### options
60
56
 
61
- Type: `Object`
57
+ Type: `object`
62
58
 
63
59
  ##### decode
64
60
 
@@ -70,43 +66,74 @@ Decode the keys and values. URL components are decoded with [`decode-uri-compone
70
66
  ##### arrayFormat
71
67
 
72
68
  Type: `string`<br>
73
- Default: `none`
69
+ Default: `'none'`
74
70
 
75
- - `bracket`: Parse arrays with bracket representation:
71
+ - `'bracket'`: Parse arrays with bracket representation:
76
72
 
77
73
  ```js
78
74
  queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
79
- //=> foo: [1, 2, 3]
75
+ //=> {foo: ['1', '2', '3']}
80
76
  ```
81
77
 
82
- - `index`: Parse arrays with index representation:
78
+ - `'index'`: Parse arrays with index representation:
83
79
 
84
80
  ```js
85
81
  queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
86
- //=> foo: [1, 2, 3]
82
+ //=> {foo: ['1', '2', '3']}
87
83
  ```
88
84
 
89
- - `comma`: Parse arrays with elements separated by comma:
85
+ - `'comma'`: Parse arrays with elements separated by comma:
90
86
 
91
87
  ```js
92
88
  queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
93
- //=> foo: [1, 2, 3]
89
+ //=> {foo: ['1', '2', '3']}
94
90
  ```
95
91
 
96
- - `none`: Parse arrays with elements using duplicate keys:
92
+ - `'none'`: Parse arrays with elements using duplicate keys:
97
93
 
98
94
  ```js
99
95
  queryString.parse('foo=1&foo=2&foo=3');
100
- //=> foo: [1, 2, 3]
96
+ //=> {foo: ['1', '2', '3']}
101
97
  ```
102
98
 
99
+ ##### sort
100
+
101
+ Type: `Function | boolean`<br>
102
+ Default: `true`
103
+
104
+ Supports both `Function` as a custom sorting function or `false` to disable sorting.
105
+
106
+ ##### parseNumbers
107
+
108
+ Type: `boolean`<br>
109
+ Default: `false`
110
+
111
+ ```js
112
+ queryString.parse('foo=1', {parseNumbers: true});
113
+ //=> {foo: 1}
114
+ ```
115
+
116
+ Parse the value as a number type instead of string type if it's a number.
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
+
103
130
  ### .stringify(object, [options])
104
131
 
105
132
  Stringify an object into a query string and sorting the keys.
106
133
 
107
134
  #### options
108
135
 
109
- Type: `Object`
136
+ Type: `object`
110
137
 
111
138
  ##### strict
112
139
 
@@ -125,30 +152,30 @@ Default: `true`
125
152
  ##### arrayFormat
126
153
 
127
154
  Type: `string`<br>
128
- Default: `none`
155
+ Default: `'none'`
129
156
 
130
- - `bracket`: Serialize arrays using bracket representation:
157
+ - `'bracket'`: Serialize arrays using bracket representation:
131
158
 
132
159
  ```js
133
160
  queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
134
161
  //=> 'foo[]=1&foo[]=2&foo[]=3'
135
162
  ```
136
163
 
137
- - `index`: Serialize arrays using index representation:
164
+ - `'index'`: Serialize arrays using index representation:
138
165
 
139
166
  ```js
140
167
  queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
141
- //=> 'foo[0]=1&foo[1]=2&foo[3]=3'
168
+ //=> 'foo[0]=1&foo[1]=2&foo[2]=3'
142
169
  ```
143
170
 
144
- - `comma`: Serialize arrays by separating elements with comma:
171
+ - `'comma'`: Serialize arrays by separating elements with comma:
145
172
 
146
173
  ```js
147
174
  queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
148
175
  //=> 'foo=1,2,3'
149
176
  ```
150
177
 
151
- - `none`: Serialize arrays by using duplicate keys:
178
+ - `'none'`: Serialize arrays by using duplicate keys:
152
179
 
153
180
  ```js
154
181
  queryString.stringify({foo: [1, 2, 3]});
@@ -181,7 +208,7 @@ If omitted, keys are sorted using `Array#sort()`, which means, converting them t
181
208
 
182
209
  Extract a query string from a URL that can be passed into `.parse()`.
183
210
 
184
- ### .parseUrl(string, [options])
211
+ ### .parseUrl(string, options?)
185
212
 
186
213
  Extract the URL and the query string as an object.
187
214
 
@@ -218,7 +245,7 @@ queryString.parse('likes=cake&name=bob&likes=icecream');
218
245
  //=> {likes: ['cake', 'icecream'], name: 'bob'}
219
246
 
220
247
  queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'});
221
- //=> 'color=chartreuse&color=taupe&id=515'
248
+ //=> 'color=taupe&color=chartreuse&id=515'
222
249
  ```
223
250
 
224
251
 
@@ -238,6 +265,14 @@ queryString.stringify({foo: undefined});
238
265
  ```
239
266
 
240
267
 
241
- ## License
268
+ ---
242
269
 
243
- MIT © [Sindre Sorhus](https://sindresorhus.com)
270
+ <div align="center">
271
+ <b>
272
+ <a href="https://tidelift.com/subscription/pkg/npm-query-string?utm_source=npm-query-string&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
273
+ </b>
274
+ <br>
275
+ <sub>
276
+ Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
277
+ </sub>
278
+ </div>