Skip to content

Commit

Permalink
remove JSON conversion, explicitly convert buffers, booleans, and tim…
Browse files Browse the repository at this point in the history
…estamps to their string equivalents
  • Loading branch information
nlf committed Aug 5, 2014
1 parent a66f9c7 commit e473156
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ var internals = {};

internals.stringify = function (obj, prefix) {

if (Buffer.isBuffer(obj)) {
obj = obj.toString();
}
else if (obj instanceof Date) {
obj = obj.toISOString();
}

if (typeof obj === 'string' ||
typeof obj === 'number') {
typeof obj === 'number' ||
typeof obj === 'boolean') {

return [prefix + '=' + encodeURIComponent(obj)];
}
Expand All @@ -33,11 +41,10 @@ internals.stringify = function (obj, prefix) {
module.exports = function (obj) {

var keys = [];
var value = JSON.parse(JSON.stringify(obj));

for (var key in value) {
if (value.hasOwnProperty(key)) {
keys = keys.concat(internals.stringify(value[key], encodeURIComponent(key)));
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
keys = keys.concat(internals.stringify(obj[key], encodeURIComponent(key)));
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,20 @@ describe('#stringify', function () {
delete Object.prototype.crash;
done();
});

it('stringifies boolean values', function (done) {

expect(Qs.stringify({ a: true })).to.equal('a=true');
expect(Qs.stringify({ a: { b: true } })).to.equal('a[b]=true');
expect(Qs.stringify({ b: false })).to.equal('b=false');
expect(Qs.stringify({ b: { c: false } })).to.equal('b[c]=false');
done();
});

it('stringifies buffer values', function (done) {

expect(Qs.stringify({ a: new Buffer('test') })).to.equal('a=test');
expect(Qs.stringify({ a: { b: new Buffer('test') } })).to.equal('a[b]=test');
done();
});
});

0 comments on commit e473156

Please sign in to comment.