Skip to content

Commit 3ee0423

Browse files
RaisinTentargos
authored andcommitted
lib: support BigInt in querystring.stringify
Fixes: #36080 PR-URL: #36499 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 3cac041 commit 3ee0423

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

doc/api/querystring.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ The `querystring.stringify()` method produces a URL query string from a
122122
given `obj` by iterating through the object's "own properties".
123123

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

128129
```js
129130
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });

lib/querystring.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ function stringifyPrimitive(v) {
159159
return v;
160160
if (typeof v === 'number' && isFinite(v))
161161
return '' + v;
162+
if (typeof v === 'bigint')
163+
return '' + v;
162164
if (typeof v === 'boolean')
163165
return v ? 'true' : 'false';
164166
return '';
@@ -173,6 +175,8 @@ function encodeStringified(v, encode) {
173175
// escaping due to the inclusion of a '+' in the output
174176
return (MathAbs(v) < 1e21 ? '' + v : encode('' + v));
175177
}
178+
if (typeof v === 'bigint')
179+
return '' + v;
176180
if (typeof v === 'boolean')
177181
return v ? 'true' : 'false';
178182
return '';

test/parallel/test-querystring.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,24 @@ qsWeirdObjects.forEach((testCase) => {
273273
assert.strictEqual(qs.stringify(testCase[0]), testCase[1]);
274274
});
275275

276+
// BigInt values
277+
278+
assert.strictEqual(qs.stringify({ foo: 2n ** 1023n }),
279+
'foo=' + 2n ** 1023n);
280+
assert.strictEqual(qs.stringify([0n, 1n, 2n]),
281+
'0=0&1=1&2=2');
282+
283+
assert.strictEqual(qs.stringify({ foo: 2n ** 1023n },
284+
null,
285+
null,
286+
{ encodeURIComponent: (c) => c }),
287+
'foo=' + 2n ** 1023n);
288+
assert.strictEqual(qs.stringify([0n, 1n, 2n],
289+
null,
290+
null,
291+
{ encodeURIComponent: (c) => c }),
292+
'0=0&1=1&2=2');
293+
276294
// Invalid surrogate pair throws URIError
277295
assert.throws(
278296
() => qs.stringify({ foo: '\udc00' }),

0 commit comments

Comments
 (0)