diff --git a/lib/parse.js b/lib/parse.js index 23f1677e..3a0e185f 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -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); diff --git a/lib/stringify.js b/lib/stringify.js index cbac7069..8fbbe4ba 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -50,7 +50,7 @@ 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 = ''; @@ -58,8 +58,8 @@ var stringify = function stringify( // eslint-disable-line func-name-matching 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))]; } diff --git a/test/parse.js b/test/parse.js index c8093e97..a92ea15c 100644 --- a/test/parse.js +++ b/test/parse.js @@ -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) { @@ -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' }); @@ -523,4 +534,6 @@ test('parse()', function (t) { st.deepEqual(options, {}); st.end(); }); + + t.end(); }); diff --git a/test/stringify.js b/test/stringify.js index 168359c5..b06446c8 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -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) { @@ -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' }); @@ -570,4 +581,6 @@ test('stringify()', function (t) { st.deepEqual(options, {}); st.end(); }); + + t.end(); });