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 2 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
4 changes: 2 additions & 2 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ test('array stringify representation with array commas and null value', t => {
bar: [null]
}, {
arrayFormat: 'comma'
}), 'foo=a');
}), 'bar=&foo=,a,,');
decimoseptimo marked this conversation as resolved.
Show resolved Hide resolved
});

test('array stringify representation with array commas and 0 value', t => {
Expand All @@ -141,7 +141,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