Skip to content

Commit

Permalink
lib: support BigInt in querystring.stringify
Browse files Browse the repository at this point in the history
Fixes: #36080

PR-URL: #36499
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
RaisinTen authored and targos committed May 1, 2021
1 parent 3cac041 commit 3ee0423
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions doc/api/querystring.md
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
Expand Up @@ -159,6 +159,8 @@ function stringifyPrimitive(v) {
return v;
if (typeof v === 'number' && isFinite(v))
return '' + v;
if (typeof v === 'bigint')
return '' + v;
if (typeof v === 'boolean')
return v ? 'true' : 'false';
return '';
Expand All @@ -173,6 +175,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
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

0 comments on commit 3ee0423

Please sign in to comment.