MCPcopy Create free account
hub / github.com/ljharb/qs / normalizeStringifyOptions

Function normalizeStringifyOptions

lib/stringify.js:208–280  ·  view source on GitHub ↗
(opts)

Source from the content-addressed store, hash-verified

206};
207
208var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
209 if (!opts) {
210 return defaults;
211 }
212
213 if (typeof opts.allowEmptyArrays !== 'undefined' && typeof opts.allowEmptyArrays !== 'boolean') {
214 throw new TypeError('`allowEmptyArrays` option can only be `true` or `false`, when provided');
215 }
216
217 if (typeof opts.encodeDotInKeys !== 'undefined' && typeof opts.encodeDotInKeys !== 'boolean') {
218 throw new TypeError('`encodeDotInKeys` option can only be `true` or `false`, when provided');
219 }
220
221 if (opts.encoder !== null && typeof opts.encoder !== 'undefined' && typeof opts.encoder !== 'function') {
222 throw new TypeError('Encoder has to be a function.');
223 }
224
225 var charset = opts.charset || defaults.charset;
226 if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
227 throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
228 }
229
230 var format = formats['default'];
231 if (typeof opts.format !== 'undefined') {
232 if (!has.call(formats.formatters, opts.format)) {
233 throw new TypeError('Unknown format option provided.');
234 }
235 format = opts.format;
236 }
237 var formatter = formats.formatters[format];
238
239 var filter = defaults.filter;
240 if (typeof opts.filter === 'function' || isArray(opts.filter)) {
241 filter = opts.filter;
242 }
243
244 var arrayFormat;
245 if (opts.arrayFormat in arrayPrefixGenerators) {
246 arrayFormat = opts.arrayFormat;
247 } else if ('indices' in opts) {
248 arrayFormat = opts.indices ? 'indices' : 'repeat';
249 } else {
250 arrayFormat = defaults.arrayFormat;
251 }
252
253 if ('commaRoundTrip' in opts && typeof opts.commaRoundTrip !== 'boolean') {
254 throw new TypeError('`commaRoundTrip` must be a boolean, or absent');
255 }
256
257 var allowDots = typeof opts.allowDots === 'undefined' ? opts.encodeDotInKeys === true ? true : defaults.allowDots : !!opts.allowDots;
258
259 return {
260 addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix,
261 allowDots: allowDots,
262 allowEmptyArrays: typeof opts.allowEmptyArrays === 'boolean' ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays,
263 arrayFormat: arrayFormat,
264 charset: charset,
265 charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,

Callers 1

stringify.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected