Skip to content

Commit e5a6c1f

Browse files
dubzzzsindresorhus
authored andcommitted
Fix stringify for bracket mode with arrays containing null (#138)
1 parent 3bcd4e8 commit e5a6c1f

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function encoderForArrayFormat(options) {
2121
};
2222
case 'bracket':
2323
return (key, value) => {
24-
return value === null ? encode(key, options) : [
24+
return value === null ? [encode(key, options), '[]'].join('') : [
2525
encode(key, options),
2626
'[]=',
2727
encode(value, options)

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
},
4040
"devDependencies": {
4141
"ava": "*",
42+
"deep-equal": "^1.0.1",
43+
"fast-check": "^0.0.13",
4244
"xo": "*"
4345
}
4446
}

test/parse.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ test('query strings having brackets arrays and format option as `bracket`', t =>
107107
}), {foo: ['bar', 'baz']});
108108
});
109109

110+
test('query strings having brackets arrays with null and format option as `bracket`', t => {
111+
t.deepEqual(m.parse('bar[]&foo[]=a&foo[]&foo[]=', {
112+
arrayFormat: 'bracket'
113+
}), {
114+
foo: ['a', null, ''],
115+
bar: [null]
116+
});
117+
});
118+
110119
test('query strings having indexed arrays and format option as `index`', t => {
111120
t.deepEqual(m.parse('foo[0]=bar&foo[1]=baz', {
112121
arrayFormat: 'index'

test/properties.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import deepEqual from 'deep-equal';
2+
import * as fc from 'fast-check';
3+
import test from 'ava';
4+
import m from '..';
5+
6+
// Valid query parameters must follow:
7+
// - key can be any unicode string (not empty)
8+
// - value must be one of:
9+
// --> any unicode string
10+
// --> null
11+
// --> array containing values defined above (at least two items)
12+
const queryParamsArbitrary = fc.object({
13+
key: fc.fullUnicodeString(1, 10),
14+
values: [
15+
fc.fullUnicodeString(),
16+
fc.constant(null),
17+
fc.array(fc.oneof(fc.fullUnicodeString(), fc.constant(null))).filter(a => a.length >= 2)
18+
],
19+
maxDepth: 0
20+
});
21+
22+
const optionsArbitrary = fc.record({
23+
arrayFormat: fc.constantFrom('bracket', 'index', 'none'),
24+
strict: fc.boolean(),
25+
encode: fc.boolean(),
26+
sort: fc.boolean()
27+
});
28+
29+
test('should read correctly from stringified query params', t => {
30+
t.notThrows(() => fc.assert(
31+
fc.property(queryParamsArbitrary, optionsArbitrary,
32+
(obj, opts) => deepEqual(m.parse(m.stringify(obj, opts), opts), obj))));
33+
});

test/stringify.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ test('array stringify representation with array brackets', t => {
108108
}), 'bar[]=one&bar[]=two&foo');
109109
});
110110

111+
test('array stringify representation with array brackets and null value', t => {
112+
t.is(m.stringify({
113+
foo: ['a', null, ''],
114+
bar: [null]
115+
}, {
116+
arrayFormat: 'bracket'
117+
}), 'bar[]&foo[]=a&foo[]&foo[]=');
118+
});
119+
111120
test('array stringify representation with a bad array format', t => {
112121
t.is(m.stringify({
113122
foo: null,

0 commit comments

Comments
 (0)