query-string 6.9.0 → 6.11.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.
package/index.d.ts CHANGED
@@ -36,6 +36,15 @@ export interface ParseOptions {
36
36
  //=> {foo: ['1', '2', '3']}
37
37
  ```
38
38
 
39
+ - `separator`: Parse arrays with elements separated by a custom character:
40
+
41
+ ```
42
+ import queryString = require('query-string');
43
+
44
+ queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'});
45
+ //=> {foo: ['1', '2', '3']}
46
+ ```
47
+
39
48
  - `none`: Parse arrays with elements using duplicate keys:
40
49
 
41
50
  ```
@@ -45,7 +54,14 @@ export interface ParseOptions {
45
54
  //=> {foo: ['1', '2', '3']}
46
55
  ```
47
56
  */
48
- readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'none';
57
+ readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'none';
58
+
59
+ /**
60
+ The character used to separate array elements when using `{arrayFormat: 'separator'}`.
61
+
62
+ @default ,
63
+ */
64
+ readonly arrayFormatSeparator?: string;
49
65
 
50
66
  /**
51
67
  Supports both `Function` as a custom sorting function or `false` to disable sorting.
@@ -188,6 +204,15 @@ export interface StringifyOptions {
188
204
  //=> 'foo=1,2,3'
189
205
  ```
190
206
 
207
+ - `separator`: Serialize arrays by separating elements with character:
208
+
209
+ ```
210
+ import queryString = require('query-string');
211
+
212
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'separator', arrayFormatSeparator: '|'});
213
+ //=> 'foo=1|2|3'
214
+ ```
215
+
191
216
  - `none`: Serialize arrays by using duplicate keys:
192
217
 
193
218
  ```
@@ -197,7 +222,14 @@ export interface StringifyOptions {
197
222
  //=> 'foo=1&foo=2&foo=3'
198
223
  ```
199
224
  */
200
- readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'none';
225
+ readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'none';
226
+
227
+ /**
228
+ The character used to separate array elements when using `{arrayFormat: 'separator'}`.
229
+
230
+ @default ,
231
+ */
232
+ readonly arrayFormatSeparator?: string;
201
233
 
202
234
  /**
203
235
  Supports both `Function` as a custom sorting function or `false` to disable sorting.
@@ -267,3 +299,22 @@ Extract a query string from a URL that can be passed into `.parse()`.
267
299
  Note: This behaviour can be changed with the `skipNull` option.
268
300
  */
269
301
  export function extract(url: string): string;
302
+
303
+ /**
304
+ Stringify an object into a URL with a query string and sorting the keys. The inverse of [`.parseUrl()`](https://github.com/sindresorhus/query-string#parseurlstring-options)
305
+
306
+ Query items in the `query` property overrides queries in the `url` property.
307
+
308
+ @example
309
+ ```
310
+ queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
311
+ //=> 'https://foo.bar?foo=bar'
312
+
313
+ queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}});
314
+ //=> 'https://foo.bar?foo=bar'
315
+ ```
316
+ */
317
+ export function stringifyUrl(
318
+ object: ParsedUrl,
319
+ options?: StringifyOptions
320
+ ): string;
package/index.js CHANGED
@@ -36,6 +36,7 @@ function encoderForArrayFormat(options) {
36
36
  };
37
37
 
38
38
  case 'comma':
39
+ case 'separator':
39
40
  return key => (result, value) => {
40
41
  if (value === null || value === undefined || value.length === 0) {
41
42
  return result;
@@ -45,7 +46,7 @@ function encoderForArrayFormat(options) {
45
46
  return [[encode(key, options), '=', encode(value, options)].join('')];
46
47
  }
47
48
 
48
- return [[result, encode(value, options)].join(',')];
49
+ return [[result, encode(value, options)].join(options.arrayFormatSeparator)];
49
50
  };
50
51
 
51
52
  default:
@@ -104,9 +105,10 @@ function parserForArrayFormat(options) {
104
105
  };
105
106
 
106
107
  case 'comma':
108
+ case 'separator':
107
109
  return (key, value, accumulator) => {
108
- const isArray = typeof value === 'string' && value.split('').indexOf(',') > -1;
109
- const newValue = isArray ? value.split(',') : value;
110
+ const isArray = typeof value === 'string' && value.split('').indexOf(options.arrayFormatSeparator) > -1;
111
+ const newValue = isArray ? value.split(options.arrayFormatSeparator).map(item => decode(item, options)) : value === null ? value : decode(value, options);
110
112
  accumulator[key] = newValue;
111
113
  };
112
114
 
@@ -122,6 +124,12 @@ function parserForArrayFormat(options) {
122
124
  }
123
125
  }
124
126
 
127
+ function validateArrayFormatSeparator(value) {
128
+ if (typeof value !== 'string' || value.length !== 1) {
129
+ throw new TypeError('arrayFormatSeparator must be single character string');
130
+ }
131
+ }
132
+
125
133
  function encode(value, options) {
126
134
  if (options.encode) {
127
135
  return options.strict ? strictUriEncode(value) : encodeURIComponent(value);
@@ -161,6 +169,16 @@ function removeHash(input) {
161
169
  return input;
162
170
  }
163
171
 
172
+ function getHash(url) {
173
+ let hash = '';
174
+ const hashStart = url.indexOf('#');
175
+ if (hashStart !== -1) {
176
+ hash = url.slice(hashStart);
177
+ }
178
+
179
+ return hash;
180
+ }
181
+
164
182
  function extract(input) {
165
183
  input = removeHash(input);
166
184
  const queryStart = input.indexOf('?');
@@ -186,10 +204,13 @@ function parse(input, options) {
186
204
  decode: true,
187
205
  sort: true,
188
206
  arrayFormat: 'none',
207
+ arrayFormatSeparator: ',',
189
208
  parseNumbers: false,
190
209
  parseBooleans: false
191
210
  }, options);
192
211
 
212
+ validateArrayFormatSeparator(options.arrayFormatSeparator);
213
+
193
214
  const formatter = parserForArrayFormat(options);
194
215
 
195
216
  // Create an object with no prototype
@@ -210,7 +231,7 @@ function parse(input, options) {
210
231
 
211
232
  // Missing `=` should be `null`:
212
233
  // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
213
- value = value === undefined ? null : decode(value, options);
234
+ value = value === undefined ? null : options.arrayFormat === 'comma' ? value : decode(value, options);
214
235
  formatter(decode(key, options), value, ret);
215
236
  }
216
237
 
@@ -253,9 +274,12 @@ exports.stringify = (object, options) => {
253
274
  options = Object.assign({
254
275
  encode: true,
255
276
  strict: true,
256
- arrayFormat: 'none'
277
+ arrayFormat: 'none',
278
+ arrayFormatSeparator: ','
257
279
  }, options);
258
280
 
281
+ validateArrayFormatSeparator(options.arrayFormatSeparator);
282
+
259
283
  const formatter = encoderForArrayFormat(options);
260
284
 
261
285
  const objectCopy = Object.assign({}, object);
@@ -300,3 +324,17 @@ exports.parseUrl = (input, options) => {
300
324
  query: parse(extract(input), options)
301
325
  };
302
326
  };
327
+
328
+ exports.stringifyUrl = (input, options) => {
329
+ const url = removeHash(input.url).split('?')[0] || '';
330
+ const queryFromUrl = exports.extract(input.url);
331
+ const parsedQueryFromUrl = exports.parse(queryFromUrl);
332
+ const hash = getHash(input.url);
333
+ const query = Object.assign(parsedQueryFromUrl, input.query);
334
+ let queryString = exports.stringify(query, options);
335
+ if (queryString) {
336
+ queryString = `?${queryString}`;
337
+ }
338
+
339
+ return `${url}${queryString}${hash}`;
340
+ };
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "query-string",
3
- "version": "6.9.0",
3
+ "version": "6.11.1",
4
4
  "description": "Parse and stringify URL query strings",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/query-string",
7
+ "funding": "https://github.com/sponsors/sindresorhus",
7
8
  "author": {
8
9
  "name": "Sindre Sorhus",
9
10
  "email": "sindresorhus@gmail.com",
package/readme.md CHANGED
@@ -2,6 +2,34 @@
2
2
 
3
3
  > Parse and stringify URL [query strings](https://en.wikipedia.org/wiki/Query_string)
4
4
 
5
+ <br>
6
+
7
+ ---
8
+
9
+ <div align="center">
10
+ <p>
11
+ <p>
12
+ <sup>
13
+ <a href="https://github.com/sponsors/sindresorhus">My open source work is supported by the community</a>
14
+ </sup>
15
+ </p>
16
+ <sup>Special thanks to:</sup>
17
+ <br>
18
+ <br>
19
+ <a href="https://github.com/botpress/botpress">
20
+ <img src="https://sindresorhus.com/assets/thanks/botpress-logo.svg" width="260" alt="Botpress">
21
+ </a>
22
+ <br>
23
+ <sub><b>Botpress is an open-source conversational assistant creation platform.</b></sub>
24
+ <br>
25
+ <sub>They <a href="https://github.com/botpress/botpress/blob/master/.github/CONTRIBUTING.md">welcome contributions</a> from anyone, whether you're into machine learning,<br>want to get started in open-source, or just have an improvement idea.</sub>
26
+ <br>
27
+ </p>
28
+ </div>
29
+
30
+ ---
31
+
32
+ <br>
5
33
 
6
34
  ## Install
7
35
 
@@ -9,8 +37,7 @@
9
37
  $ npm install query-string
10
38
  ```
11
39
 
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
-
40
+ 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, use version 5: `npm install query-string@5`.
14
41
 
15
42
  ## Usage
16
43
 
@@ -43,7 +70,6 @@ console.log(location.search);
43
70
  //=> '?foo=unicorn&ilike=pizza'
44
71
  ```
45
72
 
46
-
47
73
  ## API
48
74
 
49
75
  ### .parse(string, options?)
@@ -95,6 +121,15 @@ queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
95
121
  //=> {foo: ['1', '2', '3']}
96
122
  ```
97
123
 
124
+ - `'separator'`: Parse arrays with elements separated by a custom character:
125
+
126
+ ```js
127
+ const queryString = require('query-string');
128
+
129
+ queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'});
130
+ //=> {foo: ['1', '2', '3']}
131
+ ```
132
+
98
133
  - `'none'`: Parse arrays with elements using duplicate keys:
99
134
 
100
135
  ```js
@@ -104,6 +139,13 @@ queryString.parse('foo=1&foo=2&foo=3');
104
139
  //=> {foo: ['1', '2', '3']}
105
140
  ```
106
141
 
142
+ ##### arrayFormatSeparator
143
+
144
+ Type: `string`\
145
+ Default: `','`
146
+
147
+ The character used to separate array elements when using `{arrayFormat: 'separator'}`.
148
+
107
149
  ##### sort
108
150
 
109
151
  Type: `Function | boolean`\
@@ -202,6 +244,13 @@ queryString.stringify({foo: [1, 2, 3]});
202
244
  //=> 'foo=1&foo=2&foo=3'
203
245
  ```
204
246
 
247
+ ##### arrayFormatSeparator
248
+
249
+ Type: `string`\
250
+ Default: `','`
251
+
252
+ The character used to separate array elements when using `{arrayFormat: 'separator'}`.
253
+
205
254
  ##### sort
206
255
 
207
256
  Type: `Function | boolean`
@@ -276,6 +325,39 @@ queryString.parseUrl('https://foo.bar?foo=bar');
276
325
  //=> {url: 'https://foo.bar', query: {foo: 'bar'}}
277
326
  ```
278
327
 
328
+ ### .stringifyUrl(object, options?)
329
+
330
+ Stringify an object into a URL with a query string and sorting the keys. The inverse of [`.parseUrl()`](https://github.com/sindresorhus/query-string#parseurlstring-options)
331
+
332
+ The `options` are the same as for `.stringify()`.
333
+
334
+ Returns a string with the URL and a query string.
335
+
336
+ Query items in the `query` property overrides queries in the `url` property.
337
+
338
+ ```js
339
+ queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
340
+ //=> 'https://foo.bar?foo=bar'
341
+
342
+ queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}});
343
+ //=> 'https://foo.bar?foo=bar'
344
+ ```
345
+
346
+ #### object
347
+
348
+ Type: `object`
349
+
350
+ ##### url
351
+
352
+ Type: `string`
353
+
354
+ The URL to stringify.
355
+
356
+ ##### query
357
+
358
+ Type: `object`
359
+
360
+ Query items to add to the URL.
279
361
 
280
362
  ## Nesting
281
363
 
@@ -307,7 +389,6 @@ queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'});
307
389
  //=> 'color=taupe&color=chartreuse&id=515'
308
390
  ```
309
391
 
310
-
311
392
  ## Falsy values
312
393
 
313
394
  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:
@@ -325,7 +406,6 @@ queryString.stringify({foo: undefined});
325
406
  //=> ''
326
407
  ```
327
408
 
328
-
329
409
  ## query-string for enterprise
330
410
 
331
411
  Available as part of the Tidelift Subscription.