Skip to content

Commit

Permalink
[Refactor] stringify: move option-handling code into `normalizeStri…
Browse files Browse the repository at this point in the history
…ngifyOptions`
  • Loading branch information
ljharb committed Sep 15, 2023
1 parent 6c26f8a commit fa1ba30
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var defaultFormat = formats['default'];
var defaults = {
addQueryPrefix: false,
allowDots: false,
arrayFormat: 'indices',
charset: 'utf-8',
charsetSentinel: false,
delimiter: '&',
Expand Down Expand Up @@ -213,11 +214,26 @@ var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
filter = opts.filter;
}

var arrayFormat;
if (opts.arrayFormat in arrayPrefixGenerators) {
arrayFormat = opts.arrayFormat;
} else if ('indices' in opts) {
arrayFormat = opts.indices ? 'indices' : 'repeat';
} else {
arrayFormat = defaults.arrayFormat;
}

if ('commaRoundTrip' in opts && typeof opts.commaRoundTrip !== 'boolean') {
throw new TypeError('`commaRoundTrip` must be a boolean, or absent');
}

return {
addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix,
allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots,
arrayFormat: arrayFormat,
charset: charset,
charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,
commaRoundTrip: opts.commaRoundTrip,
delimiter: typeof opts.delimiter === 'undefined' ? defaults.delimiter : opts.delimiter,
encode: typeof opts.encode === 'boolean' ? opts.encode : defaults.encode,
encoder: typeof opts.encoder === 'function' ? opts.encoder : defaults.encoder,
Expand Down Expand Up @@ -253,20 +269,8 @@ module.exports = function (object, opts) {
return '';
}

var arrayFormat;
if (opts && opts.arrayFormat in arrayPrefixGenerators) {
arrayFormat = opts.arrayFormat;
} else if (opts && 'indices' in opts) {
arrayFormat = opts.indices ? 'indices' : 'repeat';
} else {
arrayFormat = 'indices';
}

var generateArrayPrefix = arrayPrefixGenerators[arrayFormat];
if (opts && 'commaRoundTrip' in opts && typeof opts.commaRoundTrip !== 'boolean') {
throw new TypeError('`commaRoundTrip` must be a boolean, or absent');
}
var commaRoundTrip = generateArrayPrefix === 'comma' && opts && opts.commaRoundTrip;
var generateArrayPrefix = arrayPrefixGenerators[options.arrayFormat];
var commaRoundTrip = generateArrayPrefix === 'comma' && options.commaRoundTrip;

if (!objKeys) {
objKeys = Object.keys(obj);
Expand Down

0 comments on commit fa1ba30

Please sign in to comment.