Skip to content

Commit 5481be8

Browse files
RaisinTentargos
authored andcommitted
lib: support BigInt in querystring.stringify
Fixes: #36080 PR-URL: #36499 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent e30af7b commit 5481be8

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

doc/api/querystring.md

+3-2
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

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ function stringifyPrimitive(v) {
162162
return v;
163163
if (typeof v === 'number' && NumberIsFinite(v))
164164
return '' + v;
165+
if (typeof v === 'bigint')
166+
return '' + v;
165167
if (typeof v === 'boolean')
166168
return v ? 'true' : 'false';
167169
return '';
@@ -176,6 +178,8 @@ function encodeStringified(v, encode) {
176178
// escaping due to the inclusion of a '+' in the output
177179
return (MathAbs(v) < 1e21 ? '' + v : encode('' + v));
178180
}
181+
if (typeof v === 'bigint')
182+
return '' + v;
179183
if (typeof v === 'boolean')
180184
return v ? 'true' : 'false';
181185
return '';

test/parallel/test-querystring.js

+18
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)