Skip to content

Commit

Permalink
experimental: subsume strictNullHandling in emptyValue
Browse files Browse the repository at this point in the history
  • Loading branch information
timhwang21 committed Jul 11, 2020
1 parent ee6ec28 commit 66f1bf6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
12 changes: 4 additions & 8 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ var defaults = {
interpretNumericEntities: false,
parameterLimit: 1000,
parseArrays: true,
plainObjects: false,
strictNullHandling: false
plainObjects: false
};

var interpretNumericEntities = function (str) {
Expand Down Expand Up @@ -83,7 +82,7 @@ var parseValues = function parseQueryStringValues(str, options) {
var key, val;
if (pos === -1) {
key = options.decoder(part, defaults.decoder, charset, 'key');
val = options.strictNullHandling ? null : options.emptyValue;
val = options.emptyValue;
} else {
key = options.decoder(part.slice(0, pos), defaults.decoder, charset, 'key');
val = utils.maybeMap(
Expand Down Expand Up @@ -214,9 +213,7 @@ var normalizeParseOptions = function normalizeParseOptions(opts) {
throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
}

if (opts.strictNullHandling === true && has.call(opts, 'emptyValue')) {
throw new TypeError('Passing true for strictNullHandling will cause emptyValue to be ignored');
}
var defaultEmptyValue = opts.strictNullHandling === true ? null : defaults.emptyValue

var charset = typeof opts.charset === 'undefined' ? defaults.charset : opts.charset;

Expand All @@ -231,13 +228,12 @@ var normalizeParseOptions = function normalizeParseOptions(opts) {
delimiter: typeof opts.delimiter === 'string' || utils.isRegExp(opts.delimiter) ? opts.delimiter : defaults.delimiter,
// eslint-disable-next-line no-implicit-coercion, no-extra-parens
depth: (typeof opts.depth === 'number' || opts.depth === false) ? +opts.depth : defaults.depth,
emptyValue: has.call(opts, 'emptyValue') ? defaults.emptyValue : opts.emptyValue,
emptyValue: has.call(opts, 'emptyValue') ? opts.emptyValue : defaultEmptyValue,
ignoreQueryPrefix: opts.ignoreQueryPrefix === true,
interpretNumericEntities: typeof opts.interpretNumericEntities === 'boolean' ? opts.interpretNumericEntities : defaults.interpretNumericEntities,
parameterLimit: typeof opts.parameterLimit === 'number' ? opts.parameterLimit : defaults.parameterLimit,
parseArrays: opts.parseArrays !== false,
plainObjects: typeof opts.plainObjects === 'boolean' ? opts.plainObjects : defaults.plainObjects,
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling
};
};

Expand Down
8 changes: 4 additions & 4 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ var stringify = function stringify(
}).join(',');
}

if (obj === null || obj === emptyValue) {
if (strictNullHandling || (obj === emptyValue && emptyValue !== null)) {
return encoder && !encodeValuesOnly ? encoder(prefix, defaults.encoder, charset, 'key') : prefix;
}
if ((strictNullHandling && obj === null) || (emptyValue !== null && obj === emptyValue)) {
return encoder && !encodeValuesOnly ? encoder(prefix, defaults.encoder, charset, 'key') : prefix;
}

if (obj === null) {
obj = '';
}

Expand Down

0 comments on commit 66f1bf6

Please sign in to comment.