Skip to content

Commit 3ddf5e4

Browse files
authored
Merge pull request #229 from jgonggrijp/toquery-fix
2 parents 2c27a46 + dc0b553 commit 3ddf5e4

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

test/util.strings.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ $(document).ready(function() {
4343
var obj = {'foo&bar': 'baz', 'test': 'total success', 'nested': {'works': 'too'}, 'isn\'t': ['that', 'cool?']};
4444
assert.equal(_.toQuery(obj), 'foo%26bar=baz&test=total%20success&nested%5Bworks%5D=too&isn\'t%5B%5D=that&isn\'t%5B%5D=cool%3F', 'can convert a hash to a query string');
4545
assert.equal(_.toQuery(obj), jQuery.param(obj), 'query serialization matchs jQuery.param()');
46+
assert.equal(_.toQuery({a: []}), '', 'empty array params produce the empty string');
47+
assert.equal(_.toQuery({a: [], b: []}), '', 'multiple empty array params do not lead to spurious ampersands');
48+
assert.equal(_.toQuery({a: null, b: undefined}), 'a=null&b=undefined', 'respects null and undefined');
4649
});
4750

4851
QUnit.test('strContains', function(assert) {

underscore.util.strings.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
var buildParams = function(prefix, val, top) {
3030
if (_.isUndefined(top)) top = true;
3131
if (_.isArray(val)) {
32-
return _.map(val, function(value, key) {
32+
return _.compact(_.map(val, function(value, key) {
3333
return buildParams(top ? key : prefix + '[]', value, false);
34-
}).join('&');
34+
})).join('&');
3535
} else if (_.isObject(val)) {
36-
return _.map(val, function(value, key) {
36+
return _.compact(_.map(val, function(value, key) {
3737
return buildParams(top ? key : prefix + '[' + key + ']', value, false);
38-
}).join('&');
38+
})).join('&');
3939
} else {
4040
return urlEncode(prefix) + '=' + urlEncode(val);
4141
}

0 commit comments

Comments
 (0)