Skip to content

Commit

Permalink
Fixed stringify()ing numbers. Closes tj#23
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jan 26, 2012
1 parent 3059db3 commit 37416fc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ var stringify = exports.stringify = function(obj, prefix) {
} else if ('string' == typeof obj) {
return stringifyString(obj, prefix);
} else {
return prefix;
return prefix + '=' + obj;
}
};

Expand Down Expand Up @@ -210,12 +210,14 @@ function stringifyObject(obj, prefix) {
var ret = []
, keys = Object.keys(obj)
, key;

for (var i = 0, len = keys.length; i < len; ++i) {
key = keys[i];
ret.push(stringify(obj[key], prefix
? prefix + '[' + encodeURIComponent(key) + ']'
: encodeURIComponent(key)));
}

return ret.join('&');
}

Expand Down
8 changes: 8 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ var qs = require('../')
'errors': [
{ parsed: 'foo=bar', message: 'stringify expects an object' },
{ parsed: ['foo', 'bar'], message: 'stringify expects an object' }
],
'numbers': [
{ query_string: 'limit[]=1&limit[]=2&limit[]=3', parsed: { limit: [1, 2, '3'] }},
{ query_string: 'limit=1', parsed: { limit: 1 }}
]
};

Expand Down Expand Up @@ -83,6 +87,10 @@ module.exports = {
'test nested': function() {
test('nested');
},

'test numbers': function(){
test('numbers');
},

'test errors': function() {
var parsed, message;
Expand Down

0 comments on commit 37416fc

Please sign in to comment.