diff --git a/.js b/.js new file mode 100644 index 0000000..fa7dcb0 --- /dev/null +++ b/.js @@ -0,0 +1,19 @@ +const queryString = require('./index'); + +// Example data that includes null and empty strings +const params = { + list: ['item', '', null, 'last'] +}; + +// Options to reproduce the bug +const options = { + arrayFormat: 'comma', + skipNull: false, + skipEmptyString: false +}; + +// Stringify the parameters with the options +const result = queryString.stringify(params, options); + +// Log the result to console +console.log(result); // Expected to incorrectly skip null and empty strings based on the bug diff --git a/index.js b/index.js index 0480a96..02fe2b6 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,3 @@ import * as queryString from './base.js'; - export default queryString; + diff --git a/test/parse.js b/test/parse.js index 6e2261a..181f2dd 100644 --- a/test/parse.js +++ b/test/parse.js @@ -310,6 +310,33 @@ test('circuit original → parse → stringify with array commas → sorted orig t.deepEqual(queryString.stringify(queryString.parse(original, options), options), sortedOriginal); }); +test('circuit parse → stringify with array commas', t => { + const original = 'c=,a,,&b=&a='; + const sortedOriginal = 'a=&b=&c=,a,,'; + const expected = { + c: ['', 'a', '', ''], + b: '', + a: '' + }; + const options = { + arrayFormat: 'comma' + }; + + t.deepEqual(queryString.parse(original, options), expected); + + t.is(queryString.stringify(expected, options), sortedOriginal); +}); + +test('circuit original → parse → stringify with array commas → sorted original', t => { + const original = 'c=,a,,&b=&a='; + const sortedOriginal = 'a=&b=&c=,a,,'; + const options = { + arrayFormat: 'comma' + }; + + t.deepEqual(queryString.stringify(queryString.parse(original, options), options), sortedOriginal); +}); + test('decode keys and values', t => { t.deepEqual(queryString.parse('st%C3%A5le=foo'), {ståle: 'foo'}); t.deepEqual(queryString.parse('foo=%7B%ab%%7C%de%%7D+%%7Bst%C3%A5le%7D%'), {foo: '{%ab%|%de%} %{ståle}%'}); diff --git a/test/stringify.js b/test/stringify.js index 0cbdb49..cc6928a 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -133,6 +133,18 @@ test('array stringify representation with array commas', t => { }), 'bar=one,two&foo'); }); +test('array stringify representation with array commas and null/empty values', t => { + const result = queryString.stringify({ + list: ['item', null, 'last', ''] + }, { + arrayFormat: 'comma', + skipNull: false, + skipEmptyString: false + }); + + t.is(result, 'list=item,,last,'); +}); + test('array stringify representation with array commas, null & empty string', t => { t.is(queryString.stringify({ c: [null, 'a', '', null],