Skip to content

Commit

Permalink
Fix stringify for bracket mode with arrays containing null (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz authored and sindresorhus committed May 2, 2018
1 parent 3bcd4e8 commit e5a6c1f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -21,7 +21,7 @@ function encoderForArrayFormat(options) {
};
case 'bracket':
return (key, value) => {
return value === null ? encode(key, options) : [
return value === null ? [encode(key, options), '[]'].join('') : [
encode(key, options),
'[]=',
encode(value, options)
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -39,6 +39,8 @@
},
"devDependencies": {
"ava": "*",
"deep-equal": "^1.0.1",
"fast-check": "^0.0.13",
"xo": "*"
}
}
9 changes: 9 additions & 0 deletions test/parse.js
Expand Up @@ -107,6 +107,15 @@ test('query strings having brackets arrays and format option as `bracket`', t =>
}), {foo: ['bar', 'baz']});
});

test('query strings having brackets arrays with null and format option as `bracket`', t => {
t.deepEqual(m.parse('bar[]&foo[]=a&foo[]&foo[]=', {
arrayFormat: 'bracket'
}), {
foo: ['a', null, ''],
bar: [null]
});
});

test('query strings having indexed arrays and format option as `index`', t => {
t.deepEqual(m.parse('foo[0]=bar&foo[1]=baz', {
arrayFormat: 'index'
Expand Down
33 changes: 33 additions & 0 deletions test/properties.js
@@ -0,0 +1,33 @@
import deepEqual from 'deep-equal';
import * as fc from 'fast-check';
import test from 'ava';
import m from '..';

// Valid query parameters must follow:
// - key can be any unicode string (not empty)
// - value must be one of:
// --> any unicode string
// --> null
// --> array containing values defined above (at least two items)
const queryParamsArbitrary = fc.object({
key: fc.fullUnicodeString(1, 10),
values: [
fc.fullUnicodeString(),
fc.constant(null),
fc.array(fc.oneof(fc.fullUnicodeString(), fc.constant(null))).filter(a => a.length >= 2)
],
maxDepth: 0
});

const optionsArbitrary = fc.record({
arrayFormat: fc.constantFrom('bracket', 'index', 'none'),
strict: fc.boolean(),
encode: fc.boolean(),
sort: fc.boolean()
});

test('should read correctly from stringified query params', t => {
t.notThrows(() => fc.assert(
fc.property(queryParamsArbitrary, optionsArbitrary,
(obj, opts) => deepEqual(m.parse(m.stringify(obj, opts), opts), obj))));
});
9 changes: 9 additions & 0 deletions test/stringify.js
Expand Up @@ -108,6 +108,15 @@ test('array stringify representation with array brackets', t => {
}), 'bar[]=one&bar[]=two&foo');
});

test('array stringify representation with array brackets and null value', t => {
t.is(m.stringify({
foo: ['a', null, ''],
bar: [null]
}, {
arrayFormat: 'bracket'
}), 'bar[]&foo[]=a&foo[]&foo[]=');
});

test('array stringify representation with a bad array format', t => {
t.is(m.stringify({
foo: null,
Expand Down

0 comments on commit e5a6c1f

Please sign in to comment.