Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default [
'error',
{ max: 150 },
],
'max-params': ['error', 18],
'max-params': ['error', 19],
'max-statements': ['error', 100],
'multiline-comment-style': 'off',
'no-continue': 'warn',
Expand Down
15 changes: 15 additions & 0 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ var arrayPrefixGenerators = {
return prefix + '[]';
},
comma: 'comma',
dots: function dots(prefix, key) {
return prefix + '.' + key;
},
indices: function indices(prefix, key) {
return prefix + '[' + key + ']';
},
Expand All @@ -30,6 +33,7 @@ var defaultFormat = formats['default'];
var defaults = {
addQueryPrefix: false,
allowDots: false,
allowDotsForIndices: false,
allowEmptyArrays: false,
arrayFormat: 'indices',
charset: 'utf-8',
Expand Down Expand Up @@ -75,6 +79,7 @@ var stringify = function stringify(
filter,
sort,
allowDots,
allowDotsForIndices,
serializeDate,
format,
formatter,
Expand Down Expand Up @@ -191,6 +196,7 @@ var stringify = function stringify(
filter,
sort,
allowDots,
allowDotsForIndices,
serializeDate,
format,
formatter,
Expand Down Expand Up @@ -220,6 +226,10 @@ var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
throw new TypeError('Encoder has to be a function.');
}

if ('allowDots' in opts && opts.allowDots === false && opts.allowDotsForIndices) {
throw new TypeError('allowDots cannot be false and allowDotsForIndices true at the same time');
}

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');
Expand All @@ -244,6 +254,8 @@ var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
arrayFormat = opts.arrayFormat;
} else if ('indices' in opts) {
arrayFormat = opts.indices ? 'indices' : 'repeat';
} else if (opts.allowDotsForIndices) {
arrayFormat = 'dots';
} else {
arrayFormat = defaults.arrayFormat;
}
Expand All @@ -253,10 +265,12 @@ var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
}

var allowDots = typeof opts.allowDots === 'undefined' ? opts.encodeDotInKeys === true ? true : defaults.allowDots : !!opts.allowDots;
var allowDotsForIndices = opts.allowDotsForIndices;

return {
addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix,
allowDots: allowDots,
allowDotsForIndices: allowDotsForIndices,
allowEmptyArrays: typeof opts.allowEmptyArrays === 'boolean' ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays,
arrayFormat: arrayFormat,
charset: charset,
Expand Down Expand Up @@ -330,6 +344,7 @@ module.exports = function (object, opts) {
options.filter,
options.sort,
options.allowDots,
options.allowDotsForIndices,
options.serializeDate,
options.format,
options.formatter,
Expand Down
6 changes: 6 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ test('stringify()', function (t) {
st.end();
});

t.test('`allowDotsForIndices` option: stringifies a nested object with converting indices to dot notation', function (st) {
st.equal(qs.stringify({ a: [1, 2] }, { allowDotsForIndices: true }), 'a.0=1&a.1=2');
st.equal(qs.stringify({ a: [{ b: 'c', d: 'e' }] }, { allowDotsForIndices: true, allowDots: true }), 'a.0.b=c&a.0.d=e');
st.end();
});

t.test('stringifies an array value', function (st) {
st.equal(
qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'indices' }),
Expand Down