Skip to content

Commit

Permalink
allow for empty strings in arrays, for ljharb#7
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Aug 6, 2014
1 parent b7af068 commit fd10bca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ Qs.parse('a[1]=b&a[15]=c');
// { a: ['b', 'c'] }
```

Note that an empty string is also a value, and will be preserved:

```javascript
Qs.parse('a[]=&a[]=b');
// { a: ['', 'b'] }
Qs.parse('a[0]=b&a[1]=&a[2]=c');
// { a: ['b', '', 'c'] }
```

**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
instead be converted to an object with the index as the key:

Expand All @@ -117,4 +126,4 @@ You can also create arrays of objects:
```javascript
Qs.parse('a[][b]=c');
// { a: [{ b: 'c' }] }
```
```
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ exports.compact = function (obj) {

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

compacted[key].push(obj[key][i]);
}
Expand Down
7 changes: 7 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ describe('#parse', function () {
done();
});

it('allows for empty strings in arrays', function (done) {

expect(Qs.parse('a[]=b&a[]=&a[]=c')).to.deep.equal({ a: ['b', '', 'c'] });
expect(Qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]=')).to.deep.equal({ a: ['b', '', 'c', ''] });
done();
});

it('should compact sparse arrays', function (done) {

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

0 comments on commit fd10bca

Please sign in to comment.