Skip to content

Commit a16dc73

Browse files
committed
Apply bug fix and resolve cherry-pick
1 parent 7cda957 commit a16dc73

File tree

6 files changed

+86
-9
lines changed

6 files changed

+86
-9
lines changed

.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const queryString = require('./index');
2+
3+
// Example data that includes null and empty strings
4+
const params = {
5+
list: ['item', '', null, 'last']
6+
};
7+
8+
// Options to reproduce the bug
9+
const options = {
10+
arrayFormat: 'comma',
11+
skipNull: false,
12+
skipEmptyString: false
13+
};
14+
15+
// Stringify the parameters with the options
16+
const result = queryString.stringify(params, options);
17+
18+
// Log the result to console
19+
console.log(result); // Expected to incorrectly skip null and empty strings based on the bug

index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ export interface StringifyOptions {
229229
230230
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
231231
//=> 'foo=1,2,3'
232+
233+
queryString.stringify({foo: [1, null, '']}, {arrayFormat: 'comma'});
234+
//=> 'foo=1,,'
235+
// Note that typing information for null values is lost
236+
// and `.parse('foo=1,,')` would return `{foo: [1, '', '']}`.
232237
```
233238
234239
- `separator`: Serialize arrays by separating elements with character:

index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,20 @@ function encoderForArrayFormat(options) {
4949
case 'comma':
5050
case 'separator':
5151
return key => (result, value) => {
52-
if (value === null || value === undefined || value.length === 0) {
52+
if (
53+
value === undefined ||
54+
(options.skipNull && value === null) ||
55+
(options.skipEmptyString && value === '')
56+
) {
5357
return result;
5458
}
5559

5660
if (result.length === 0) {
57-
return [[encode(key, options), '=', encode(value, options)].join('')];
61+
return [[encode(key, options), '=', encode(value === null ? '' : value, options)].join('')];
62+
}
63+
64+
if (value === null || value === '') {
65+
return [[result, ''].join(options.arrayFormatSeparator)];
5866
}
5967

6068
return [[result, encode(value, options)].join(options.arrayFormatSeparator)];

readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ const queryString = require('query-string');
228228

229229
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
230230
//=> 'foo=1,2,3'
231+
232+
queryString.stringify({foo: [1, null, '']}, {arrayFormat: 'comma'});
233+
//=> 'foo=1,,'
234+
// Note that typing information for null values is lost
235+
// and `.parse('foo=1,,')` would return `{foo: [1, '', '']}`.
231236
```
232237

233238
- `'none'`: Serialize arrays by using duplicate keys:

test/parse.js

+29-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ test('query strings having ordered index arrays and format option as `index`', t
213213
}), {bat: 'buz', foo: ['zero', 'two', 'one', 'three']});
214214
});
215215

216-
test('circuit parse -> stringify', t => {
216+
test('circuit parse stringify', t => {
217217
const original = 'foo[3]=foo&foo[2]&foo[1]=one&foo[0]=&bat=buz';
218218
const sortedOriginal = 'bat=buz&foo[0]=&foo[1]=one&foo[2]&foo[3]=foo';
219219
const expected = {bat: 'buz', foo: ['', 'one', null, 'foo']};
@@ -226,7 +226,7 @@ test('circuit parse -> stringify', t => {
226226
t.is(queryString.stringify(expected, options), sortedOriginal);
227227
});
228228

229-
test('circuit original -> parse - > stringify -> sorted original', t => {
229+
test('circuit original parse stringify sorted original', t => {
230230
const original = 'foo[21474836471]=foo&foo[21474836470]&foo[1]=one&foo[0]=&bat=buz';
231231
const sortedOriginal = 'bat=buz&foo[0]=&foo[1]=one&foo[2]&foo[3]=foo';
232232
const options = {
@@ -236,6 +236,33 @@ test('circuit original -> parse - > stringify -> sorted original', t => {
236236
t.deepEqual(queryString.stringify(queryString.parse(original, options), options), sortedOriginal);
237237
});
238238

239+
test('circuit parse → stringify with array commas', t => {
240+
const original = 'c=,a,,&b=&a=';
241+
const sortedOriginal = 'a=&b=&c=,a,,';
242+
const expected = {
243+
c: ['', 'a', '', ''],
244+
b: '',
245+
a: ''
246+
};
247+
const options = {
248+
arrayFormat: 'comma'
249+
};
250+
251+
t.deepEqual(queryString.parse(original, options), expected);
252+
253+
t.is(queryString.stringify(expected, options), sortedOriginal);
254+
});
255+
256+
test('circuit original → parse → stringify with array commas → sorted original', t => {
257+
const original = 'c=,a,,&b=&a=';
258+
const sortedOriginal = 'a=&b=&c=,a,,';
259+
const options = {
260+
arrayFormat: 'comma'
261+
};
262+
263+
t.deepEqual(queryString.stringify(queryString.parse(original, options), options), sortedOriginal);
264+
});
265+
239266
test('decode keys and values', t => {
240267
t.deepEqual(queryString.parse('st%C3%A5le=foo'), {ståle: 'foo'});
241268
t.deepEqual(queryString.parse('foo=%7B%ab%%7C%de%%7D+%%7Bst%C3%A5le%7D%'), {foo: '{%ab%|%de%} %{ståle}%'});

test/stringify.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,26 @@ test('array stringify representation with array commas and null/empty values', t
138138
t.is(result, 'list=item,,last,');
139139
});
140140

141-
test('array stringify representation with array commas and null value', t => {
141+
test('array stringify representation with array commas, null & empty string', t => {
142142
t.is(queryString.stringify({
143-
foo: [null, 'a', null, ''],
144-
bar: [null]
143+
c: [null, 'a', '', null],
144+
b: [null],
145+
a: ['']
146+
}, {
147+
arrayFormat: 'comma'
148+
}), 'a=&b=&c=,a,,');
149+
});
150+
151+
test('array stringify representation with array commas, null & empty string (skip both)', t => {
152+
t.is(queryString.stringify({
153+
c: [null, 'a', '', null],
154+
b: [null],
155+
a: ['']
145156
}, {
157+
skipNull: true,
158+
skipEmptyString: true,
146159
arrayFormat: 'comma'
147-
}), 'foo=a');
160+
}), 'c=a');
148161
});
149162

150163
test('array stringify representation with array commas and 0 value', t => {
@@ -153,7 +166,7 @@ test('array stringify representation with array commas and 0 value', t => {
153166
bar: [null]
154167
}, {
155168
arrayFormat: 'comma'
156-
}), 'foo=a,0');
169+
}), 'bar=&foo=a,,0');
157170
});
158171

159172
test('array stringify representation with a bad array format', t => {

0 commit comments

Comments
 (0)