Skip to content

Commit

Permalink
[Fix] do not mutate options argument.
Browse files Browse the repository at this point in the history
Fixes #207.
  • Loading branch information
ljharb committed Apr 11, 2017
1 parent 5ebf8b4 commit 0d93016
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/parse.js
Expand Up @@ -130,7 +130,7 @@ var parseKeys = function parseQueryStringKeys(givenKey, val, options) {
};

module.exports = function (str, opts) {
var options = opts || {};
var options = opts ? utils.assign({}, opts) : {};

if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') {
throw new TypeError('Decoder has to be a function.');
Expand Down
2 changes: 1 addition & 1 deletion lib/stringify.js
Expand Up @@ -123,7 +123,7 @@ var stringify = function stringify( // eslint-disable-line func-name-matching

module.exports = function (object, opts) {
var obj = object;
var options = opts || {};
var options = opts ? utils.assign({}, opts) : {};

if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') {
throw new TypeError('Encoder has to be a function.');
Expand Down
7 changes: 7 additions & 0 deletions test/parse.js
Expand Up @@ -516,4 +516,11 @@ test('parse()', function (t) {
}, new TypeError('Decoder has to be a function.'));
st.end();
});

t.test('does not mutate the options argument', function (st) {
var options = {};
qs.parse('a[b]=true', options);
st.deepEqual(options, {});
st.end();
});
});
6 changes: 6 additions & 0 deletions test/stringify.js
Expand Up @@ -564,4 +564,10 @@ test('stringify()', function (t) {
st.end();
});

t.test('does not mutate the options argument', function (st) {
var options = {};
qs.stringify({}, options);
st.deepEqual(options, {});
st.end();
});
});

0 comments on commit 0d93016

Please sign in to comment.