Skip to content

Commit

Permalink
refactor: rename throwOnParameterLimitExceeded to `throwOnLimitExce…
Browse files Browse the repository at this point in the history
…eded`

- adds clarity for using the same variable (`throwOnLimitExceeded`) for both `parameterLimit` and `arrayLimit`.
  • Loading branch information
IamLizu committed Nov 12, 2024
1 parent affbfc0 commit fc13250
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,17 @@ var limited = qs.parse('a=b&c=d', { parameterLimit: 1 });
assert.deepEqual(limited, { a: 'b' });
```

If you want an error to be thrown whenever the parameter limit is exceeded, set the `throwOnParameterLimitExceeded` option to `true`. This option will generate a descriptive error if the query string exceeds the parameterLimit.
If you want an error to be thrown whenever the parameter limit is exceeded, set the `throwOnLimitExceeded` option to `true`. This option will generate a descriptive error if the query string exceeds the parameterLimit.
```javascript
try {
qs.parse('a=1&b=2&c=3&d=4', { parameterLimit: 3, throwOnParameterLimitExceeded: true });
qs.parse('a=1&b=2&c=3&d=4', { parameterLimit: 3, throwOnLimitExceeded: true });
} catch (err) {
assert(err instanceof Error);
assert.strictEqual(err.message, 'Parameter limit exceeded. Only 3 parameters allowed.');
}
```

When `throwOnParameterLimitExceeded` is set to `false` (default), **qs** will parse up to the specified `parameterLimit` and ignore the rest without throwing an error.
When `throwOnLimitExceeded` is set to `false` (default), **qs** will parse up to the specified `parameterLimit` and ignore the rest without throwing an error.

To bypass the leading question mark, use `ignoreQueryPrefix`:

Expand Down
10 changes: 5 additions & 5 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ var parseValues = function parseQueryStringValues(str, options) {
var limit = options.parameterLimit === Infinity ? undefined : options.parameterLimit;
var parts = cleanStr.split(
options.delimiter,
options.throwOnParameterLimitExceeded ? limit + 1 : limit
options.throwOnLimitExceeded ? limit + 1 : limit
);

// Check if parts exceed the parameter limit and handle it
if (options.throwOnParameterLimitExceeded && parts.length > limit) {
if (options.throwOnLimitExceeded && parts.length > limit) {
throw new Error(`Parameter limit exceeded. Only ${limit} parameters allowed.`);
}

Expand Down Expand Up @@ -247,8 +247,8 @@ var normalizeParseOptions = function normalizeParseOptions(opts) {
throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
}

if (typeof opts.throwOnParameterLimitExceeded !== 'undefined' && typeof opts.throwOnParameterLimitExceeded !== 'boolean') {
throw new TypeError('`throwOnParameterLimitExceeded` option must be a boolean');
if (typeof opts.throwOnLimitExceeded !== 'undefined' && typeof opts.throwOnLimitExceeded !== 'boolean') {
throw new TypeError('`throwOnLimitExceeded` option must be a boolean');
}

var charset = typeof opts.charset === 'undefined' ? defaults.charset : opts.charset;
Expand Down Expand Up @@ -283,7 +283,7 @@ var normalizeParseOptions = function normalizeParseOptions(opts) {
plainObjects: typeof opts.plainObjects === 'boolean' ? opts.plainObjects : defaults.plainObjects,
strictDepth: typeof opts.strictDepth === 'boolean' ? !!opts.strictDepth : defaults.strictDepth,
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling,
throwOnParameterLimitExceeded: typeof opts.throwOnParameterLimitExceeded === 'boolean' ? opts.throwOnParameterLimitExceeded : false
throwOnLimitExceeded: typeof opts.throwOnLimitExceeded === 'boolean' ? opts.throwOnLimitExceeded : false
};
};

Expand Down
16 changes: 8 additions & 8 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1037,41 +1037,41 @@ test('parse()', function (t) {

t.test('parameter limit tests', function (st) {
st.test('does not throw error when within parameter limit', function (sst) {
const result = qs.parse('a=1&b=2&c=3', { parameterLimit: 5, throwOnParameterLimitExceeded: true });
const result = qs.parse('a=1&b=2&c=3', { parameterLimit: 5, throwOnLimitExceeded: true });
sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'Should parse without errors');
sst.end();
});

st.test('throws error when throwOnParameterLimitExceeded is present but not boolean', function (sst) {
st.test('throws error when throwOnLimitExceeded is present but not boolean', function (sst) {
sst.throws(
function () {
qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnParameterLimitExceeded: "true" });
qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnLimitExceeded: "true" });
},
new TypeError('`throwOnParameterLimitExceeded` option must be a boolean'),
'Should throw error when throwOnParameterLimitExceeded is present and not boolean'
new TypeError('`throwOnLimitExceeded` option must be a boolean'),
'Should throw error when throwOnLimitExceeded is present and not boolean'
);
sst.end();
});

st.test('throws error when parameter limit exceeded', function (sst) {
sst.throws(
function () {
qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnParameterLimitExceeded: true });
qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnLimitExceeded: true });
},
new Error('Parameter limit exceeded. Only 3 parameters allowed.'),
'Should throw error when parameter limit is exceeded'
);
sst.end();
});

st.test('silently truncates when throwOnParameterLimitExceeded is not given', function (sst) {
st.test('silently truncates when throwOnLimitExceeded is not given', function (sst) {
const result = qs.parse('a=1&b=2&c=3&d=4&e=5', { parameterLimit: 3 });
sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'Should parse and truncate silently');
sst.end();
});

st.test('silently truncates when parameter limit exceeded without error', function (sst) {
const result = qs.parse('a=1&b=2&c=3&d=4&e=5', { parameterLimit: 3, throwOnParameterLimitExceeded: false });
const result = qs.parse('a=1&b=2&c=3&d=4&e=5', { parameterLimit: 3, throwOnLimitExceeded: false });
sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'Should parse and truncate silently');
sst.end();
});
Expand Down

0 comments on commit fc13250

Please sign in to comment.