Skip to content

avoid browserifying Buffer, for #39 #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ internals.stringify = function (obj, prefix) {

var values = [];

for (var key in obj) {
if (obj.hasOwnProperty(key)) {
values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']'));
}
if (typeof obj === 'undefined') {
return values;
}

var objKeys = Object.keys(obj);
for (var i = 0, il = objKeys.length; i < il; ++i) {
var key = objKeys[i];
values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']'));
}

return values;
Expand All @@ -48,10 +52,10 @@ module.exports = function (obj, options) {

var keys = [];

for (var key in obj) {
if (obj.hasOwnProperty(key)) {
keys = keys.concat(internals.stringify(obj[key], key));
}
var objKeys = Object.keys(obj);
for (var i = 0, il = objKeys.length; i < il; ++i) {
var key = objKeys[i];
keys = keys.concat(internals.stringify(obj[key], key));
}

return keys.join(delimiter);
Expand Down
11 changes: 7 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,13 @@ exports.isRegExp = function (obj) {

exports.isBuffer = function (obj) {

if (typeof Buffer !== 'undefined') {
return Buffer.isBuffer(obj);
}
else {
if (obj === null ||
typeof obj === 'undefined') {

return false;
}

return !!(obj.constructor &&
obj.constructor.isBuffer &&
obj.constructor.isBuffer(obj));
};
19 changes: 19 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ describe('#stringify', function () {
done();
});

it('stringifies an empty object', function (done) {

var obj = Object.create(null);
obj.a = 'b';
expect(Qs.stringify(obj)).to.equal('a=b');
done();
});

it('stringifies an object with an empty object as a child', function (done) {

var obj = {
a: Object.create(null)
};

obj.a.b = 'c';
expect(Qs.stringify(obj)).to.equal('a%5Bb%5D=c');
done();
});

it('drops keys with a value of undefined', function (done) {

expect(Qs.stringify({ a: undefined })).to.equal('');
Expand Down