query-string 6.6.0 → 6.8.2

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 +161 -119
  2. package/index.js +9 -1
  3. package/package.json +2 -2
  4. package/readme.md +30 -6
package/index.d.ts CHANGED
@@ -1,73 +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';
37
41
 
38
42
  /**
39
- * Supports both `Function` as a custom sorting function or `false` to disable sorting.
40
- *
41
- * If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
42
- *
43
- * @default true
44
- *
45
- * @example
46
- *
47
- * const order = ['c', 'a', 'b'];
48
- * queryString.parse('?a=one&b=two&c=three', {
49
- * sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
50
- * });
51
- * // => {c: 'three', a: 'one', b: 'two'}
52
- *
53
- * queryString.parse('?a=one&c=three&b=two', {sort: false});
54
- * // => {a: 'one', c: 'three', b: 'two'}
55
- */
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
+ */
56
63
  readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
57
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;
58
90
  }
59
91
 
60
- export interface ParsedQuery {
61
- readonly [key: string]: string | string[] | null | undefined;
92
+ export interface ParsedQuery<T = string> {
93
+ [key: string]: T | T[] | null | undefined;
62
94
  }
63
95
 
64
96
  /**
65
- * Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.
66
- *
67
- * 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`.
68
- *
69
- * @param query - The query string to parse.
70
- */
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>;
71
106
  export function parse(query: string, options?: ParseOptions): ParsedQuery;
72
107
 
73
108
  export interface ParsedUrl {
@@ -76,91 +111,98 @@ export interface ParsedUrl {
76
111
  }
77
112
 
78
113
  /**
79
- * Extract the URL and the query string as an object.
80
- *
81
- * @param url - The URL to parse.
82
- *
83
- * @example
84
- *
85
- * queryString.parseUrl('https://foo.bar?foo=bar');
86
- * //=> {url: 'https://foo.bar', query: {foo: 'bar'}}
87
- */
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
+ */
88
124
  export function parseUrl(url: string, options?: ParseOptions): ParsedUrl;
89
125
 
90
126
  export interface StringifyOptions {
91
127
  /**
92
- * 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.
93
- *
94
- * @default true
95
- */
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
+ */
96
132
  readonly strict?: boolean;
97
133
 
98
134
  /**
99
- * [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
100
- *
101
- * @default true
102
- */
135
+ [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
136
+
137
+ @default true
138
+ */
103
139
  readonly encode?: boolean;
104
140
 
105
141
  /**
106
- * @default 'none'
107
- *
108
- * - `bracket`: Serialize arrays using bracket representation:
109
- *
110
- *
111
- * queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
112
- * //=> 'foo[]=1&foo[]=2&foo[]=3'
113
- *
114
- * - `index`: Serialize arrays using index representation:
115
- *
116
- *
117
- * queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
118
- * //=> 'foo[0]=1&foo[1]=2&foo[3]=3'
119
- *
120
- * - `comma`: Serialize arrays by separating elements with comma:
121
- *
122
- *
123
- * queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
124
- * //=> 'foo=1,2,3'
125
- *
126
- * - `none`: Serialize arrays by using duplicate keys:
127
- *
128
- *
129
- * queryString.stringify({foo: [1, 2, 3]});
130
- * //=> 'foo=1&foo=2&foo=3'
131
- */
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
+ */
132
172
  readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'none';
133
173
 
134
174
  /**
135
- * Supports both `Function` as a custom sorting function or `false` to disable sorting.
136
- *
137
- * If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
138
- *
139
- * @default true
140
- *
141
- * @example
142
- *
143
- * const order = ['c', 'a', 'b'];
144
- * queryString.stringify({a: 1, b: 2, c: 3}, {
145
- * sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
146
- * });
147
- * // => 'c=3&a=1&b=2'
148
- *
149
- * queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
150
- * // => 'b=1&c=2&a=3'
151
- */
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
+ */
152
194
  readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
153
195
  }
154
196
 
155
197
  /**
156
- * Stringify an object into a query string and sorting the keys.
157
- */
198
+ Stringify an object into a query string and sort the keys.
199
+ */
158
200
  export function stringify(
159
201
  object: {[key: string]: any},
160
202
  options?: StringifyOptions
161
203
  ): string;
162
204
 
163
205
  /**
164
- * Extract a query string from a URL that can be passed into `.parse()`.
165
- */
206
+ Extract a query string from a URL that can be passed into `.parse()`.
207
+ */
166
208
  export function extract(url: string): string;
package/index.js CHANGED
@@ -175,7 +175,9 @@ function parse(input, options) {
175
175
  options = Object.assign({
176
176
  decode: true,
177
177
  sort: true,
178
- arrayFormat: 'none'
178
+ arrayFormat: 'none',
179
+ parseNumbers: false,
180
+ parseBooleans: false
179
181
  }, options);
180
182
 
181
183
  const formatter = parserForArrayFormat(options);
@@ -200,6 +202,12 @@ function parse(input, options) {
200
202
  // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
201
203
  value = value === undefined ? null : decode(value, options);
202
204
 
205
+ if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
206
+ value = Number(value);
207
+ } else if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
208
+ value = value.toLowerCase() === 'true';
209
+ }
210
+
203
211
  formatter(decode(key, options), value, ret);
204
212
  }
205
213
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-string",
3
- "version": "6.6.0",
3
+ "version": "6.8.2",
4
4
  "description": "Parse and stringify URL query strings",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/query-string",
@@ -40,7 +40,7 @@
40
40
  "strict-uri-encode": "^2.0.0"
41
41
  },
42
42
  "devDependencies": {
43
- "ava": "^2.0.0",
43
+ "ava": "^1.4.1",
44
44
  "deep-equal": "^1.0.1",
45
45
  "fast-check": "^1.5.0",
46
46
  "tsd": "^0.7.3",
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
@@ -103,6 +103,30 @@ Default: `true`
103
103
 
104
104
  Supports both `Function` as a custom sorting function or `false` to disable sorting.
105
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
+
106
130
  ### .stringify(object, [options])
107
131
 
108
132
  Stringify an object into a query string and sorting the keys.
@@ -141,7 +165,7 @@ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
141
165
 
142
166
  ```js
143
167
  queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
144
- //=> 'foo[0]=1&foo[1]=2&foo[3]=3'
168
+ //=> 'foo[0]=1&foo[1]=2&foo[2]=3'
145
169
  ```
146
170
 
147
171
  - `'comma'`: Serialize arrays by separating elements with comma:
@@ -221,7 +245,7 @@ queryString.parse('likes=cake&name=bob&likes=icecream');
221
245
  //=> {likes: ['cake', 'icecream'], name: 'bob'}
222
246
 
223
247
  queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'});
224
- //=> 'color=chartreuse&color=taupe&id=515'
248
+ //=> 'color=taupe&color=chartreuse&id=515'
225
249
  ```
226
250
 
227
251