Skip to content

Commit

Permalink
Slight structure changes in the code to allow custom encoding of Buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
martinheidegger committed May 8, 2016
1 parent f91a1e5 commit 50c785c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lib/stringify.js
Expand Up @@ -26,8 +26,6 @@ var stringify = function stringify(object, prefix, generateArrayPrefix, strictNu
var obj = object;
if (typeof filter === 'function') {
obj = filter(prefix, obj);
} else if (Utils.isBuffer(obj)) {
obj = String(obj);
} else if (obj instanceof Date) {
obj = obj.toISOString();
} else if (obj === null) {
Expand All @@ -38,11 +36,11 @@ var stringify = function stringify(object, prefix, generateArrayPrefix, strictNu
obj = '';
}

if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean') {
if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || Utils.isBuffer(obj)) {
if (encoder) {
return [encoder(prefix) + '=' + encoder(obj)];
}
return [prefix + '=' + obj];
return [prefix + '=' + String(obj)];
}

var values = [];
Expand Down
14 changes: 14 additions & 0 deletions test/stringify.js
Expand Up @@ -288,4 +288,18 @@ test('stringify()', function (t) {
}, new TypeError('Encoder has to be a function.'));
st.end();
});

t.test('can use custom encoder for a buffer object', {
skip: typeof Buffer === 'undefined'
}, function (st) {
st.equal(qs.stringify({ a: new Buffer([1]) }, {
encoder: function (buffer) {
if (typeof buffer === 'string') {
return buffer;
}
return String.fromCharCode(buffer.readUInt8(0) + 97);
}
}), 'a=b');
st.end();
});
});

0 comments on commit 50c785c

Please sign in to comment.