Skip to content

Commit

Permalink
refactor: Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Jordan Harband <ljharb@gmail.com>
  • Loading branch information
IamLizu and ljharb authored Nov 17, 2024
1 parent 9298f8f commit 3e94744
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var parseArrayValue = function (val, options, currentArrayLength) {

// Check if array length exceeds the limit before adding new values
if (options.throwOnLimitExceeded && currentArrayLength >= options.arrayLimit) {
throw new Error('Array limit exceeded. Only ' + options.arrayLimit + ' elements allowed in an array.');
throw new Error('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
}

return val;
Expand Down Expand Up @@ -72,7 +72,7 @@ var parseValues = function parseQueryStringValues(str, options) {

// Check if parts exceed the parameter limit and handle it
if (options.throwOnLimitExceeded && parts.length > limit) {
throw new Error('Parameter limit exceeded. Only ' + limit + ' parameters allowed.');
throw new Error('Parameter limit exceeded. Only ' + limit + ' parameter' + (limit === 1 ? '' : 's') + ' allowed.');
}

var skipIndex = -1; // Keep track of where the utf8 sentinel was found
Expand Down Expand Up @@ -141,7 +141,7 @@ var parseValues = function parseQueryStringValues(str, options) {
var parseObject = function (chain, val, options, valuesParsed) {
var currentArrayLength = 0;
if (chain.length > 0 && chain[chain.length - 1] === '[]') {
var parentKey = chain.slice(0, -1).join(''); // Get the parent key
var parentKey = chain.slice(0, -1).join('');
currentArrayLength = Array.isArray(val) && val[parentKey] ? val[parentKey].length : 0;
}

Expand Down
6 changes: 3 additions & 3 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ test('parse()', function (t) {
t.test('array limit tests', function (st) {
st.test('does not throw error when array is within limit', function (sst) {
var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 5, throwOnLimitExceeded: true });
sst.deepEqual(result, { a: ['1', '2', '3'] }, 'Should parse array without errors');
sst.deepEqual(result, { a: ['1', '2', '3'] }, 'parses array without errors');
sst.end();
});

Expand All @@ -1098,7 +1098,7 @@ test('parse()', function (t) {
qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3, throwOnLimitExceeded: 'true' });
},
new TypeError('`throwOnLimitExceeded` option must be a boolean'),
'Should throw error when throwOnLimitExceeded is present and not boolean for array limit'
'throws error when throwOnLimitExceeded is present and not boolean for array limit'
);
sst.end();
});
Expand All @@ -1109,7 +1109,7 @@ test('parse()', function (t) {
qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3, throwOnLimitExceeded: true });
},
new Error('Array limit exceeded. Only 3 elements allowed in an array.'),
'Should throw error when array limit is exceeded'
'throws error when array limit is exceeded'
);
sst.end();
});
Expand Down

0 comments on commit 3e94744

Please sign in to comment.