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

Skipping blank values at stringify #307

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -515,6 +515,13 @@ var nullsSkipped = qs.stringify({ a: 'b', c: null}, { skipNulls: true });
assert.equal(nullsSkipped, 'a=b');
```

To completely skip rendering key with `blank` values ('', ' '), use the `skipBlanks` flag:

```javascript
var blanksSkipped = qs.stringify({ a: 'b', c: '', d: ' '}, { skipBlanks: true });
assert.equal(blanksSkipped, 'a=b');
```

If you're communicating with legacy systems, you can switch to `iso-8859-1`
using the `charset` option:

Expand Down
13 changes: 13 additions & 0 deletions lib/stringify.js
Expand Up @@ -42,6 +42,7 @@ var defaults = {
serializeDate: function serializeDate(date) {
return toISO.call(date);
},
skipBlanks: false,
skipNulls: false,
strictNullHandling: false
};
Expand All @@ -59,6 +60,7 @@ var stringify = function stringify(
prefix,
generateArrayPrefix,
strictNullHandling,
skipBlanks,
skipNulls,
encoder,
filter,
Expand Down Expand Up @@ -125,6 +127,10 @@ var stringify = function stringify(
continue;
}

if (skipBlanks && String(obj[key]).trim() === '') {
continue;
}

var keyPrefix = isArray(obj)
? typeof generateArrayPrefix === 'function' ? generateArrayPrefix(prefix, key) : prefix
: prefix + (allowDots ? '.' + key : '[' + key + ']');
Expand All @@ -134,6 +140,7 @@ var stringify = function stringify(
keyPrefix,
generateArrayPrefix,
strictNullHandling,
skipBlanks,
skipNulls,
encoder,
filter,
Expand Down Expand Up @@ -191,6 +198,7 @@ var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
format: format,
formatter: formatter,
serializeDate: typeof opts.serializeDate === 'function' ? opts.serializeDate : defaults.serializeDate,
skipBlanks: typeof opts.skipBlanks === 'boolean' ? opts.skipBlanks : defaults.skipBlanks,
skipNulls: typeof opts.skipNulls === 'boolean' ? opts.skipNulls : defaults.skipNulls,
sort: typeof opts.sort === 'function' ? opts.sort : null,
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling
Expand Down Expand Up @@ -243,11 +251,16 @@ module.exports = function (object, opts) {
if (options.skipNulls && obj[key] === null) {
continue;
}

if (options.skipBlanks && String(obj[key]).trim() === '') {
continue;
}
pushToArray(keys, stringify(
obj[key],
key,
generateArrayPrefix,
options.strictNullHandling,
options.skipBlanks,
options.skipNulls,
options.encode ? options.encoder : null,
options.filter,
Expand Down
10 changes: 10 additions & 0 deletions test/stringify.js
Expand Up @@ -126,6 +126,16 @@ test('stringify()', function (t) {
st.end();
});

t.test('omits blanks when asked', function (st) {
st.equal(qs.stringify({ a: 'b', c: '', d: ' ' }, { skipBlanks: true }), 'a=b');
st.end();
});

t.test('omits nested blanks when asked', function (st) {
st.equal(qs.stringify({ a: { b: 'c', d: '' } }, { skipBlanks: true }), 'a%5Bb%5D=c');
st.end();
});

t.test('omits array indices when asked', function (st) {
st.equal(qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false }), 'a=b&a=c&a=d');
st.end();
Expand Down