Skip to content

Commit

Permalink
[New] pass default encoder/decoder to custom encoder/decoder functions.
Browse files Browse the repository at this point in the history
Fixes #206.
  • Loading branch information
ljharb committed Apr 13, 2017
1 parent a660179 commit bd5009a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/parse.js
Expand Up @@ -26,11 +26,11 @@ var parseValues = function parseQueryStringValues(str, options) {

var key, val;
if (pos === -1) {
key = options.decoder(part);
key = options.decoder(part, defaults.decoder);
val = options.strictNullHandling ? null : '';
} else {
key = options.decoder(part.slice(0, pos));
val = options.decoder(part.slice(pos + 1));
key = options.decoder(part.slice(0, pos), defaults.decoder);
val = options.decoder(part.slice(pos + 1), defaults.decoder);
}
if (has.call(obj, key)) {
obj[key] = [].concat(obj[key]).concat(val);
Expand Down
6 changes: 3 additions & 3 deletions lib/stringify.js
Expand Up @@ -50,16 +50,16 @@ var stringify = function stringify( // eslint-disable-line func-name-matching
obj = serializeDate(obj);
} else if (obj === null) {
if (strictNullHandling) {
return encoder && !encodeValuesOnly ? encoder(prefix) : prefix;
return encoder && !encodeValuesOnly ? encoder(prefix, defaults.encoder) : prefix;
}

obj = '';
}

if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || utils.isBuffer(obj)) {
if (encoder) {
var keyValue = encodeValuesOnly ? prefix : encoder(prefix);
return [formatter(keyValue) + '=' + formatter(encoder(obj))];
var keyValue = encodeValuesOnly ? prefix : encoder(prefix, defaults.encoder);
return [formatter(keyValue) + '=' + formatter(encoder(obj, defaults.encoder))];
}
return [formatter(prefix) + '=' + formatter(String(obj))];
}
Expand Down
13 changes: 13 additions & 0 deletions test/parse.js
Expand Up @@ -2,6 +2,7 @@

var test = require('tape');
var qs = require('../');
var utils = require('../lib/utils');
var iconv = require('iconv-lite');

test('parse()', function (t) {
Expand Down Expand Up @@ -510,6 +511,16 @@ test('parse()', function (t) {
st.end();
});

t.test('receives the default decoder as a second argument', function (st) {
st.plan(1);
qs.parse('a', {
decoder: function (str, defaultDecoder) {
st.equal(defaultDecoder, utils.decode);
}
});
st.end();
});

t.test('throws error with wrong decoder', function (st) {
st.throws(function () {
qs.parse({}, { decoder: 'string' });
Expand All @@ -523,4 +534,6 @@ test('parse()', function (t) {
st.deepEqual(options, {});
st.end();
});

t.end();
});
13 changes: 13 additions & 0 deletions test/stringify.js
Expand Up @@ -2,6 +2,7 @@

var test = require('tape');
var qs = require('../');
var utils = require('../lib/utils');
var iconv = require('iconv-lite');

test('stringify()', function (t) {
Expand Down Expand Up @@ -452,6 +453,16 @@ test('stringify()', function (t) {
st.end();
});

t.test('receives the default encoder as a second argument', function (st) {
st.plan(2);
qs.stringify({ a: 1 }, {
encoder: function (str, defaultEncoder) {
st.equal(defaultEncoder, utils.encode);
}
});
st.end();
});

t.test('throws error with wrong encoder', function (st) {
st.throws(function () {
qs.stringify({}, { encoder: 'string' });
Expand Down Expand Up @@ -570,4 +581,6 @@ test('stringify()', function (t) {
st.deepEqual(options, {});
st.end();
});

t.end();
});

0 comments on commit bd5009a

Please sign in to comment.