Skip to content

Commit fd10bca

Browse files
committed
allow for empty strings in arrays, for #7
1 parent b7af068 commit fd10bca

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ Qs.parse('a[1]=b&a[15]=c');
9797
// { a: ['b', 'c'] }
9898
```
9999

100+
Note that an empty string is also a value, and will be preserved:
101+
102+
```javascript
103+
Qs.parse('a[]=&a[]=b');
104+
// { a: ['', 'b'] }
105+
Qs.parse('a[0]=b&a[1]=&a[2]=c');
106+
// { a: ['b', '', 'c'] }
107+
```
108+
100109
**qs** will also limit specifying indices in an array to a maximum index of `20`. Any array members with an index of greater than `20` will
101110
instead be converted to an object with the index as the key:
102111

@@ -117,4 +126,4 @@ You can also create arrays of objects:
117126
```javascript
118127
Qs.parse('a[][b]=c');
119128
// { a: [{ b: 'c' }] }
120-
```
129+
```

lib/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ exports.compact = function (obj) {
115115

116116
for (var i = 0, l = obj[key].length; i < l; i++) {
117117
if (obj[key].hasOwnProperty(i) &&
118-
obj[key][i]) {
118+
obj[key][i] !== null) {
119119

120120
compacted[key].push(obj[key][i]);
121121
}

test/parse.js

+7
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ describe('#parse', function () {
184184
done();
185185
});
186186

187+
it('allows for empty strings in arrays', function (done) {
188+
189+
expect(Qs.parse('a[]=b&a[]=&a[]=c')).to.deep.equal({ a: ['b', '', 'c'] });
190+
expect(Qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]=')).to.deep.equal({ a: ['b', '', 'c', ''] });
191+
done();
192+
});
193+
187194
it('should compact sparse arrays', function (done) {
188195

189196
expect(Qs.parse('a[10]=1&a[2]=2')).to.deep.equal({ a: ['2', '1'] });

0 commit comments

Comments
 (0)