Currently, stringification of an empty array outputs a single
separator character. This commit causes an empty array to output
the empty string.
Signed-off-by: Fedor Indutny <fedor@indutny.com>
QueryString.stringify = QueryString.encode = function(obj, sep, eq, options) {
sep = sep || '&';
eq = eq || '=';
QueryString.stringify = QueryString.encode = function(obj, sep, eq, options) {
sep = sep || '&';
eq = eq || '=';
- if (util.isNull(obj)) {
- obj = undefined;
- }
var encode = QueryString.escape;
if (options && typeof options.encodeURIComponent === 'function') {
var encode = QueryString.escape;
if (options && typeof options.encodeURIComponent === 'function') {
}
if (util.isObject(obj)) {
}
if (util.isObject(obj)) {
- return Object.keys(obj).map(function(k) {
+ var keys = Object.keys(obj);
+ var fields = [];
+
+ for (var i = 0; i < keys.length; i++) {
+ var k = keys[i];
+ var v = obj[k];
var ks = encode(stringifyPrimitive(k)) + eq;
var ks = encode(stringifyPrimitive(k)) + eq;
- if (util.isArray(obj[k])) {
- return obj[k].map(function(v) {
- return ks + encode(stringifyPrimitive(v));
- }).join(sep);
+
+ if (util.isArray(v)) {
+ for (var j = 0; j < v.length; j++)
+ fields.push(ks + encode(stringifyPrimitive(v[j])));
- return ks + encode(stringifyPrimitive(obj[k]));
+ fields.push(ks + encode(stringifyPrimitive(v)));
+ }
+ return fields.join(sep);
valueOf: 'bar',
__defineGetter__: 'baz' }],
// See: https://github.com/joyent/node/issues/3058
valueOf: 'bar',
__defineGetter__: 'baz' }],
// See: https://github.com/joyent/node/issues/3058
- ['foo&bar=baz', 'foo=&bar=baz', { foo: '', bar: 'baz' }]
+ ['foo&bar=baz', 'foo=&bar=baz', { foo: '', bar: 'baz' }],
+ [null, '', {}],
+ [undefined, '', {}]
];
// [ wonkyQS, canonicalQS, obj ]
];
// [ wonkyQS, canonicalQS, obj ]
[{f: false, t: true}, 'f=false&t=true', {'f': 'false', 't': 'true'}],
[{n: null}, 'n=', {'n': ''}],
[{nan: NaN}, 'nan=', {'nan': ''}],
[{f: false, t: true}, 'f=false&t=true', {'f': 'false', 't': 'true'}],
[{n: null}, 'n=', {'n': ''}],
[{nan: NaN}, 'nan=', {'nan': ''}],
- [{inf: Infinity}, 'inf=', {'inf': ''}]
+ [{inf: Infinity}, 'inf=', {'inf': ''}],
+ [{a: [], b: []}, '', {}]