2022-01-08 17:05:05 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var utils = require('./utils');
|
|
|
|
var formats = require('./formats');
|
2022-01-08 17:17:05 +00:00
|
|
|
var has = Object.prototype.hasOwnProperty;
|
2022-01-08 17:05:05 +00:00
|
|
|
|
|
|
|
var arrayPrefixGenerators = {
|
2022-01-08 17:17:05 +00:00
|
|
|
brackets: function brackets(prefix) {
|
2022-01-08 17:05:05 +00:00
|
|
|
return prefix + '[]';
|
|
|
|
},
|
2022-01-08 17:17:05 +00:00
|
|
|
comma: 'comma',
|
|
|
|
indices: function indices(prefix, key) {
|
2022-01-08 17:05:05 +00:00
|
|
|
return prefix + '[' + key + ']';
|
|
|
|
},
|
2022-01-08 17:17:05 +00:00
|
|
|
repeat: function repeat(prefix) {
|
2022-01-08 17:05:05 +00:00
|
|
|
return prefix;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-08 17:17:05 +00:00
|
|
|
var isArray = Array.isArray;
|
|
|
|
var push = Array.prototype.push;
|
|
|
|
var pushToArray = function (arr, valueOrArray) {
|
|
|
|
push.apply(arr, isArray(valueOrArray) ? valueOrArray : [valueOrArray]);
|
|
|
|
};
|
|
|
|
|
2022-01-08 17:05:05 +00:00
|
|
|
var toISO = Date.prototype.toISOString;
|
|
|
|
|
2022-01-08 17:17:05 +00:00
|
|
|
var defaultFormat = formats['default'];
|
2022-01-08 17:05:05 +00:00
|
|
|
var defaults = {
|
2022-01-08 17:17:05 +00:00
|
|
|
addQueryPrefix: false,
|
|
|
|
allowDots: false,
|
|
|
|
charset: 'utf-8',
|
|
|
|
charsetSentinel: false,
|
2022-01-08 17:05:05 +00:00
|
|
|
delimiter: '&',
|
|
|
|
encode: true,
|
|
|
|
encoder: utils.encode,
|
|
|
|
encodeValuesOnly: false,
|
2022-01-08 17:17:05 +00:00
|
|
|
format: defaultFormat,
|
|
|
|
formatter: formats.formatters[defaultFormat],
|
|
|
|
// deprecated
|
|
|
|
indices: false,
|
|
|
|
serializeDate: function serializeDate(date) {
|
2022-01-08 17:05:05 +00:00
|
|
|
return toISO.call(date);
|
|
|
|
},
|
|
|
|
skipNulls: false,
|
|
|
|
strictNullHandling: false
|
|
|
|
};
|
|
|
|
|
2022-01-08 17:17:05 +00:00
|
|
|
var isNonNullishPrimitive = function isNonNullishPrimitive(v) {
|
|
|
|
return typeof v === 'string'
|
|
|
|
|| typeof v === 'number'
|
|
|
|
|| typeof v === 'boolean'
|
|
|
|
|| typeof v === 'symbol'
|
|
|
|
|| typeof v === 'bigint';
|
|
|
|
};
|
|
|
|
|
|
|
|
var stringify = function stringify(
|
2022-01-08 17:05:05 +00:00
|
|
|
object,
|
|
|
|
prefix,
|
|
|
|
generateArrayPrefix,
|
|
|
|
strictNullHandling,
|
|
|
|
skipNulls,
|
|
|
|
encoder,
|
|
|
|
filter,
|
|
|
|
sort,
|
|
|
|
allowDots,
|
|
|
|
serializeDate,
|
2022-01-08 17:17:05 +00:00
|
|
|
format,
|
2022-01-08 17:05:05 +00:00
|
|
|
formatter,
|
2022-01-08 17:17:05 +00:00
|
|
|
encodeValuesOnly,
|
|
|
|
charset
|
2022-01-08 17:05:05 +00:00
|
|
|
) {
|
|
|
|
var obj = object;
|
|
|
|
if (typeof filter === 'function') {
|
|
|
|
obj = filter(prefix, obj);
|
|
|
|
} else if (obj instanceof Date) {
|
|
|
|
obj = serializeDate(obj);
|
2022-01-08 17:17:05 +00:00
|
|
|
} else if (generateArrayPrefix === 'comma' && isArray(obj)) {
|
|
|
|
obj = utils.maybeMap(obj, function (value) {
|
|
|
|
if (value instanceof Date) {
|
|
|
|
return serializeDate(value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj === null) {
|
2022-01-08 17:05:05 +00:00
|
|
|
if (strictNullHandling) {
|
2022-01-08 17:17:05 +00:00
|
|
|
return encoder && !encodeValuesOnly ? encoder(prefix, defaults.encoder, charset, 'key', format) : prefix;
|
2022-01-08 17:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
obj = '';
|
|
|
|
}
|
|
|
|
|
2022-01-08 17:17:05 +00:00
|
|
|
if (isNonNullishPrimitive(obj) || utils.isBuffer(obj)) {
|
2022-01-08 17:05:05 +00:00
|
|
|
if (encoder) {
|
2022-01-08 17:17:05 +00:00
|
|
|
var keyValue = encodeValuesOnly ? prefix : encoder(prefix, defaults.encoder, charset, 'key', format);
|
|
|
|
return [formatter(keyValue) + '=' + formatter(encoder(obj, defaults.encoder, charset, 'value', format))];
|
2022-01-08 17:05:05 +00:00
|
|
|
}
|
|
|
|
return [formatter(prefix) + '=' + formatter(String(obj))];
|
|
|
|
}
|
|
|
|
|
|
|
|
var values = [];
|
|
|
|
|
|
|
|
if (typeof obj === 'undefined') {
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
var objKeys;
|
2022-01-08 17:17:05 +00:00
|
|
|
if (generateArrayPrefix === 'comma' && isArray(obj)) {
|
|
|
|
// we need to join elements in
|
|
|
|
objKeys = [{ value: obj.length > 0 ? obj.join(',') || null : undefined }];
|
|
|
|
} else if (isArray(filter)) {
|
2022-01-08 17:05:05 +00:00
|
|
|
objKeys = filter;
|
|
|
|
} else {
|
|
|
|
var keys = Object.keys(obj);
|
|
|
|
objKeys = sort ? keys.sort(sort) : keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < objKeys.length; ++i) {
|
|
|
|
var key = objKeys[i];
|
2022-01-08 17:17:05 +00:00
|
|
|
var value = typeof key === 'object' && key.value !== undefined ? key.value : obj[key];
|
2022-01-08 17:05:05 +00:00
|
|
|
|
2022-01-08 17:17:05 +00:00
|
|
|
if (skipNulls && value === null) {
|
2022-01-08 17:05:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-08 17:17:05 +00:00
|
|
|
var keyPrefix = isArray(obj)
|
|
|
|
? typeof generateArrayPrefix === 'function' ? generateArrayPrefix(prefix, key) : prefix
|
|
|
|
: prefix + (allowDots ? '.' + key : '[' + key + ']');
|
|
|
|
|
|
|
|
pushToArray(values, stringify(
|
|
|
|
value,
|
|
|
|
keyPrefix,
|
|
|
|
generateArrayPrefix,
|
|
|
|
strictNullHandling,
|
|
|
|
skipNulls,
|
|
|
|
encoder,
|
|
|
|
filter,
|
|
|
|
sort,
|
|
|
|
allowDots,
|
|
|
|
serializeDate,
|
|
|
|
format,
|
|
|
|
formatter,
|
|
|
|
encodeValuesOnly,
|
|
|
|
charset
|
|
|
|
));
|
2022-01-08 17:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return values;
|
|
|
|
};
|
|
|
|
|
2022-01-08 17:17:05 +00:00
|
|
|
var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
|
|
|
|
if (!opts) {
|
|
|
|
return defaults;
|
|
|
|
}
|
2022-01-08 17:05:05 +00:00
|
|
|
|
2022-01-08 17:17:05 +00:00
|
|
|
if (opts.encoder !== null && opts.encoder !== undefined && typeof opts.encoder !== 'function') {
|
2022-01-08 17:05:05 +00:00
|
|
|
throw new TypeError('Encoder has to be a function.');
|
|
|
|
}
|
|
|
|
|
2022-01-08 17:17:05 +00:00
|
|
|
var charset = opts.charset || defaults.charset;
|
|
|
|
if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
|
|
|
|
throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
|
|
|
|
}
|
|
|
|
|
|
|
|
var format = formats['default'];
|
|
|
|
if (typeof opts.format !== 'undefined') {
|
|
|
|
if (!has.call(formats.formatters, opts.format)) {
|
|
|
|
throw new TypeError('Unknown format option provided.');
|
|
|
|
}
|
|
|
|
format = opts.format;
|
|
|
|
}
|
|
|
|
var formatter = formats.formatters[format];
|
|
|
|
|
|
|
|
var filter = defaults.filter;
|
|
|
|
if (typeof opts.filter === 'function' || isArray(opts.filter)) {
|
|
|
|
filter = opts.filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix,
|
|
|
|
allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots,
|
|
|
|
charset: charset,
|
|
|
|
charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,
|
|
|
|
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,
|
|
|
|
encodeValuesOnly: typeof opts.encodeValuesOnly === 'boolean' ? opts.encodeValuesOnly : defaults.encodeValuesOnly,
|
|
|
|
filter: filter,
|
|
|
|
format: format,
|
|
|
|
formatter: formatter,
|
|
|
|
serializeDate: typeof opts.serializeDate === 'function' ? opts.serializeDate : defaults.serializeDate,
|
|
|
|
skipNulls: typeof opts.skipNulls === 'boolean' ? opts.skipNulls : defaults.skipNulls,
|
|
|
|
sort: typeof opts.sort === 'function' ? opts.sort : null,
|
|
|
|
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = function (object, opts) {
|
|
|
|
var obj = object;
|
|
|
|
var options = normalizeStringifyOptions(opts);
|
|
|
|
|
2022-01-08 17:05:05 +00:00
|
|
|
var objKeys;
|
|
|
|
var filter;
|
|
|
|
|
|
|
|
if (typeof options.filter === 'function') {
|
|
|
|
filter = options.filter;
|
|
|
|
obj = filter('', obj);
|
2022-01-08 17:17:05 +00:00
|
|
|
} else if (isArray(options.filter)) {
|
2022-01-08 17:05:05 +00:00
|
|
|
filter = options.filter;
|
|
|
|
objKeys = filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
var keys = [];
|
|
|
|
|
|
|
|
if (typeof obj !== 'object' || obj === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
var arrayFormat;
|
2022-01-08 17:17:05 +00:00
|
|
|
if (opts && opts.arrayFormat in arrayPrefixGenerators) {
|
|
|
|
arrayFormat = opts.arrayFormat;
|
|
|
|
} else if (opts && 'indices' in opts) {
|
|
|
|
arrayFormat = opts.indices ? 'indices' : 'repeat';
|
2022-01-08 17:05:05 +00:00
|
|
|
} else {
|
|
|
|
arrayFormat = 'indices';
|
|
|
|
}
|
|
|
|
|
|
|
|
var generateArrayPrefix = arrayPrefixGenerators[arrayFormat];
|
|
|
|
|
|
|
|
if (!objKeys) {
|
|
|
|
objKeys = Object.keys(obj);
|
|
|
|
}
|
|
|
|
|
2022-01-08 17:17:05 +00:00
|
|
|
if (options.sort) {
|
|
|
|
objKeys.sort(options.sort);
|
2022-01-08 17:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < objKeys.length; ++i) {
|
|
|
|
var key = objKeys[i];
|
|
|
|
|
2022-01-08 17:17:05 +00:00
|
|
|
if (options.skipNulls && obj[key] === null) {
|
2022-01-08 17:05:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-01-08 17:17:05 +00:00
|
|
|
pushToArray(keys, stringify(
|
2022-01-08 17:05:05 +00:00
|
|
|
obj[key],
|
|
|
|
key,
|
|
|
|
generateArrayPrefix,
|
2022-01-08 17:17:05 +00:00
|
|
|
options.strictNullHandling,
|
|
|
|
options.skipNulls,
|
|
|
|
options.encode ? options.encoder : null,
|
|
|
|
options.filter,
|
|
|
|
options.sort,
|
|
|
|
options.allowDots,
|
|
|
|
options.serializeDate,
|
|
|
|
options.format,
|
|
|
|
options.formatter,
|
|
|
|
options.encodeValuesOnly,
|
|
|
|
options.charset
|
2022-01-08 17:05:05 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-01-08 17:17:05 +00:00
|
|
|
var joined = keys.join(options.delimiter);
|
2022-01-08 17:05:05 +00:00
|
|
|
var prefix = options.addQueryPrefix === true ? '?' : '';
|
|
|
|
|
2022-01-08 17:17:05 +00:00
|
|
|
if (options.charsetSentinel) {
|
|
|
|
if (options.charset === 'iso-8859-1') {
|
|
|
|
// encodeURIComponent('✓'), the "numeric entity" representation of a checkmark
|
|
|
|
prefix += 'utf8=%26%2310003%3B&';
|
|
|
|
} else {
|
|
|
|
// encodeURIComponent('✓')
|
|
|
|
prefix += 'utf8=%E2%9C%93&';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 17:05:05 +00:00
|
|
|
return joined.length > 0 ? prefix + joined : '';
|
|
|
|
};
|