Skip to content

Commit

Permalink
refactor: pass currentArrayLength
Browse files Browse the repository at this point in the history
- `parseArrayValue` in `parseObject
  • Loading branch information
IamLizu committed Nov 17, 2024
1 parent 219bfa4 commit c5e0117
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ var parseValues = function parseQueryStringValues(str, options) {
};

var parseObject = function (chain, val, options, valuesParsed) {
var leaf = valuesParsed ? val : parseArrayValue(val, options);
var currentArrayLength = 0;
if (chain.length > 0 && chain[chain.length - 1] === '[]') {
var parentKey = chain.slice(0, -1).join(''); // Get the parent key
currentArrayLength = Array.isArray(val) && val[parentKey] ? val[parentKey].length : 0;
}

var leaf = valuesParsed ? val : parseArrayValue(val, options, currentArrayLength);

for (var i = chain.length - 1; i >= 0; --i) {
var obj;
Expand Down
7 changes: 7 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,13 @@ test('parse()', function (t) {
sst.end();
});

st.test('converts array to object if length is greater than limit', function (sst) {
const result = qs.parse('a[1]=1&a[2]=2&a[3]=3&a[4]=4&a[5]=5&a[6]=6', { arrayLimit: 5 });

sst.deepEqual(result, { a: { 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6' } }, 'Should parse into object if array length is greater than limit');
sst.end();
});

st.end();
});

Expand Down

0 comments on commit c5e0117

Please sign in to comment.