mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
lib: support BigInt in querystring.stringify
Fixes: https://github.com/nodejs/node/issues/36080 PR-URL: https://github.com/nodejs/node/pull/36499 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
@@ -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: '' });
|
||||
|
||||
@@ -162,6 +162,8 @@ function stringifyPrimitive(v) {
|
||||
return v;
|
||||
if (typeof v === 'number' && NumberIsFinite(v))
|
||||
return '' + v;
|
||||
if (typeof v === 'bigint')
|
||||
return '' + v;
|
||||
if (typeof v === 'boolean')
|
||||
return v ? 'true' : 'false';
|
||||
return '';
|
||||
@@ -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 '';
|
||||
|
||||
@@ -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' }),
|
||||
|
||||
Reference in New Issue
Block a user