query-string 6.11.1 → 6.12.0

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
@@ -283,6 +283,33 @@ export interface StringifyOptions {
283
283
  ```
284
284
  */
285
285
  readonly skipNull?: boolean;
286
+
287
+ /**
288
+ Skip keys with an empty string as the value.
289
+
290
+ @default false
291
+
292
+ @example
293
+ ```
294
+ import queryString = require('query-string');
295
+
296
+ queryString.stringify({a: 1, b: '', c: '', d: 4}, {
297
+ skipEmptyString: true
298
+ });
299
+ //=> 'a=1&d=4'
300
+ ```
301
+
302
+ @example
303
+ ```
304
+ import queryString = require('query-string');
305
+
306
+ queryString.stringify({a: '', b: ''}, {
307
+ skipEmptyString: true
308
+ });
309
+ //=> ''
310
+ ```
311
+ */
312
+ readonly skipEmptyString?: boolean;
286
313
  }
287
314
 
288
315
  /**
package/index.js CHANGED
@@ -3,12 +3,19 @@ const strictUriEncode = require('strict-uri-encode');
3
3
  const decodeComponent = require('decode-uri-component');
4
4
  const splitOnFirst = require('split-on-first');
5
5
 
6
+ const isNullOrUndefined = value => value === null || value === undefined;
7
+
6
8
  function encoderForArrayFormat(options) {
7
9
  switch (options.arrayFormat) {
8
10
  case 'index':
9
11
  return key => (result, value) => {
10
12
  const index = result.length;
11
- if (value === undefined || (options.skipNull && value === null)) {
13
+
14
+ if (
15
+ value === undefined ||
16
+ (options.skipNull && value === null) ||
17
+ (options.skipEmptyString && value === '')
18
+ ) {
12
19
  return result;
13
20
  }
14
21
 
@@ -24,7 +31,11 @@ function encoderForArrayFormat(options) {
24
31
 
25
32
  case 'bracket':
26
33
  return key => (result, value) => {
27
- if (value === undefined || (options.skipNull && value === null)) {
34
+ if (
35
+ value === undefined ||
36
+ (options.skipNull && value === null) ||
37
+ (options.skipEmptyString && value === '')
38
+ ) {
28
39
  return result;
29
40
  }
30
41
 
@@ -51,7 +62,11 @@ function encoderForArrayFormat(options) {
51
62
 
52
63
  default:
53
64
  return key => (result, value) => {
54
- if (value === undefined || (options.skipNull && value === null)) {
65
+ if (
66
+ value === undefined ||
67
+ (options.skipNull && value === null) ||
68
+ (options.skipEmptyString && value === '')
69
+ ) {
55
70
  return result;
56
71
  }
57
72
 
@@ -280,14 +295,18 @@ exports.stringify = (object, options) => {
280
295
 
281
296
  validateArrayFormatSeparator(options.arrayFormatSeparator);
282
297
 
298
+ const shouldFilter = key => (
299
+ (options.skipNull && isNullOrUndefined(object[key])) ||
300
+ (options.skipEmptyString && object[key] === '')
301
+ );
302
+
283
303
  const formatter = encoderForArrayFormat(options);
284
304
 
285
- const objectCopy = Object.assign({}, object);
286
- if (options.skipNull) {
287
- for (const key of Object.keys(objectCopy)) {
288
- if (objectCopy[key] === undefined || objectCopy[key] === null) {
289
- delete objectCopy[key];
290
- }
305
+ const objectCopy = {};
306
+
307
+ for (const key of Object.keys(object)) {
308
+ if (!shouldFilter(key)) {
309
+ objectCopy[key] = object[key];
291
310
  }
292
311
  }
293
312
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-string",
3
- "version": "6.11.1",
3
+ "version": "6.12.0",
4
4
  "description": "Parse and stringify URL query strings",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/query-string",
package/readme.md CHANGED
@@ -304,6 +304,31 @@ queryString.stringify({a: undefined, b: null}, {
304
304
  //=> ''
305
305
  ```
306
306
 
307
+ ##### skipEmptyString
308
+
309
+ Skip keys with an empty string as the value.
310
+
311
+ Type: `boolean`\
312
+ Default: `false`
313
+
314
+ ```js
315
+ const queryString = require('query-string');
316
+
317
+ queryString.stringify({a: 1, b: '', c: '', d: 4}, {
318
+ skipEmptyString: true
319
+ });
320
+ //=> 'a=1&d=4'
321
+ ```
322
+
323
+ ```js
324
+ const queryString = require('query-string');
325
+
326
+ queryString.stringify({a: '', b: ''}, {
327
+ skipEmptyString: true
328
+ });
329
+ //=> ''
330
+ ```
331
+
307
332
  ### .extract(string)
308
333
 
309
334
  Extract a query string from a URL that can be passed into `.parse()`.