Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement skips for stringify array format comma #304

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,20 @@ function encoderForArrayFormat(options) {
case 'comma':
case 'separator':
return key => (result, value) => {
if (value === null || value === undefined || value.length === 0) {
if (
value === undefined ||
(options.skipNull && value === null) ||
(options.skipEmptyString && value === '')
) {
return result;
}

if (result.length === 0) {
return [[encode(key, options), '=', encode(value, options)].join('')];
return [[encode(key, options), '=', encode(value === null ? '' : value, options)].join('')];
}

if (value === null || value === '') {
return [[result, ''].join(options.arrayFormatSeparator)];
}

return [[result, encode(value, options)].join(options.arrayFormatSeparator)];
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ const queryString = require('query-string');

queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
//=> 'foo=1,2,3'

queryString.stringify({foo: [1, null, '']}, {arrayFormat: 'comma'});
//=> 'foo=1,,'
// Note that typing information for null values are lost
// and `.parse('foo=1,,')` would return `{foo: [1, '', '']}`.
decimoseptimo marked this conversation as resolved.
Show resolved Hide resolved
```

- `'none'`: Serialize arrays by using duplicate keys:
Expand Down
27 changes: 27 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,33 @@ test('circuit original -> parse - > stringify -> sorted original', t => {
t.deepEqual(queryString.stringify(queryString.parse(original, options), options), sortedOriginal);
});

test('circuit parse -> stringify with array commas', t => {
decimoseptimo marked this conversation as resolved.
Show resolved Hide resolved
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}%'});
Expand Down
23 changes: 18 additions & 5 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,26 @@ test('array stringify representation with array commas', t => {
}), 'bar=one,two&foo');
});

test('array stringify representation with array commas and null value', t => {
test('array stringify representation with array commas, null & empty string', t => {
decimoseptimo marked this conversation as resolved.
Show resolved Hide resolved
t.is(queryString.stringify({
foo: [null, 'a', null, ''],
bar: [null]
c: [null, 'a', '', null],
b: [null],
a: ['']
}, {
arrayFormat: 'comma'
}), 'a=&b=&c=,a,,');
});

test('array stringify representation with array commas, null & empty string (skip both)', t => {
t.is(queryString.stringify({
c: [null, 'a', '', null],
b: [null],
a: ['']
}, {
skipNull: true,
skipEmptyString: true,
arrayFormat: 'comma'
}), 'foo=a');
}), 'c=a');
});

test('array stringify representation with array commas and 0 value', t => {
Expand All @@ -141,7 +154,7 @@ test('array stringify representation with array commas and 0 value', t => {
bar: [null]
}, {
arrayFormat: 'comma'
}), 'foo=a,0');
}), 'bar=&foo=a,,0');
});

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