Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: support BigInt in querystring.stringify #36499

Closed
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
5 changes: 3 additions & 2 deletions doc/api/querystring.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ The `querystring.stringify()` method produces a URL query string from a
given `obj` by iterating through the object's "own properties".

It serializes the following types of values passed in `obj`:
{string|number|boolean|string[]|number[]|boolean[]}
Any other input values will be coerced to empty strings.
{string|number|bigint|boolean|string[]|number[]|bigint[]|boolean[]}
The numeric values must be finite. Any other input values will be coerced to
empty strings.

```js
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });
Expand Down
4 changes: 4 additions & 0 deletions lib/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ function stringifyPrimitive(v) {
return v;
if (typeof v === 'number' && NumberIsFinite(v))
return '' + v;
if (typeof v === 'bigint')
return '' + v;
joyeecheung marked this conversation as resolved.
Show resolved Hide resolved
if (typeof v === 'boolean')
return v ? 'true' : 'false';
return '';
Expand All @@ -176,6 +178,8 @@ function encodeStringified(v, encode) {
// escaping due to the inclusion of a '+' in the output
return (MathAbs(v) < 1e21 ? '' + v : encode('' + v));
}
if (typeof v === 'bigint')
return '' + v;
if (typeof v === 'boolean')
return v ? 'true' : 'false';
return '';
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,24 @@ qsWeirdObjects.forEach((testCase) => {
assert.strictEqual(qs.stringify(testCase[0]), testCase[1]);
});

// BigInt values

assert.strictEqual(qs.stringify({ foo: 2n ** 1023n }),
'foo=' + 2n ** 1023n);
assert.strictEqual(qs.stringify([0n, 1n, 2n]),
'0=0&1=1&2=2');

assert.strictEqual(qs.stringify({ foo: 2n ** 1023n },
null,
null,
{ encodeURIComponent: (c) => c }),
'foo=' + 2n ** 1023n);
assert.strictEqual(qs.stringify([0n, 1n, 2n],
null,
null,
{ encodeURIComponent: (c) => c }),
'0=0&1=1&2=2');

// Invalid surrogate pair throws URIError
assert.throws(
() => qs.stringify({ foo: '\udc00' }),
Expand Down