Skip to content

Commit 599434a

Browse files
Skn0tttargos
authored andcommitted
typings: add JSDoc Types to lib/querystring
PR-URL: #38185 Reviewed-By: Michaël Zasso <[email protected]>
1 parent 0544410 commit 599434a

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed

lib/internal/querystring.js

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ const isHexTable = new Int8Array([
3030
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // ... 256
3131
]);
3232

33+
/**
34+
* @param {string} str
35+
* @param {Int8Array} noEscapeTable
36+
* @param {string[]} hexTable
37+
* @returns {string}
38+
*/
3339
function encodeStr(str, noEscapeTable, hexTable) {
3440
const len = str.length;
3541
if (len === 0)

lib/querystring.js

+63-11
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ const unhexTable = new Int8Array([
7272
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
7373
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // ... 255
7474
]);
75-
// A safe fast alternative to decodeURIComponent
75+
/**
76+
* A safe fast alternative to decodeURIComponent
77+
* @param {string} s
78+
* @param {boolean} decodeSpaces
79+
* @returns {string}
80+
*/
7681
function unescapeBuffer(s, decodeSpaces) {
7782
const out = Buffer.allocUnsafe(s.length);
7883
let index = 0;
@@ -115,7 +120,11 @@ function unescapeBuffer(s, decodeSpaces) {
115120
return hasHex ? out.slice(0, outIndex) : out;
116121
}
117122

118-
123+
/**
124+
* @param {string} s
125+
* @param {boolean} decodeSpaces
126+
* @returns {string}
127+
*/
119128
function qsUnescape(s, decodeSpaces) {
120129
try {
121130
return decodeURIComponent(s);
@@ -141,8 +150,13 @@ const noEscape = new Int8Array([
141150
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
142151
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0 // 112 - 127
143152
]);
144-
// QueryString.escape() replaces encodeURIComponent()
145-
// https://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.4
153+
154+
/**
155+
* QueryString.escape() replaces encodeURIComponent()
156+
* @see https://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.4
157+
* @param {any} str
158+
* @returns {string}
159+
*/
146160
function qsEscape(str) {
147161
if (typeof str !== 'string') {
148162
if (typeof str === 'object')
@@ -154,6 +168,10 @@ function qsEscape(str) {
154168
return encodeStr(str, noEscape, hexTable);
155169
}
156170

171+
/**
172+
* @param {string | number | bigint | boolean | symbol | undefined | null} v
173+
* @returns {string}
174+
*/
157175
function stringifyPrimitive(v) {
158176
if (typeof v === 'string')
159177
return v;
@@ -166,7 +184,11 @@ function stringifyPrimitive(v) {
166184
return '';
167185
}
168186

169-
187+
/**
188+
* @param {string | number | bigint | boolean} v
189+
* @param {(v: string) => string} encode
190+
* @returns
191+
*/
170192
function encodeStringified(v, encode) {
171193
if (typeof v === 'string')
172194
return (v.length ? encode(v) : '');
@@ -182,12 +204,23 @@ function encodeStringified(v, encode) {
182204
return '';
183205
}
184206

185-
207+
/**
208+
* @param {string | number | boolean | null} v
209+
* @param {(v: string) => string} encode
210+
* @returns {string}
211+
*/
186212
function encodeStringifiedCustom(v, encode) {
187213
return encode(stringifyPrimitive(v));
188214
}
189215

190-
216+
/**
217+
* @param {Record<string, string | number | boolean
218+
* | ReadonlyArray<string | number | boolean> | null>} obj
219+
* @param {string} [sep]
220+
* @param {string} [eq]
221+
* @param {{ encodeURIComponent?: (v: string) => string }} [options]
222+
* @returns {string}
223+
*/
191224
function stringify(obj, sep, eq, options) {
192225
sep = sep || '&';
193226
eq = eq || '=';
@@ -232,6 +265,10 @@ function stringify(obj, sep, eq, options) {
232265
return '';
233266
}
234267

268+
/**
269+
* @param {string} str
270+
* @returns {number[]}
271+
*/
235272
function charCodes(str) {
236273
if (str.length === 0) return [];
237274
if (str.length === 1) return [str.charCodeAt(0)];
@@ -263,7 +300,17 @@ function addKeyVal(obj, key, value, keyEncoded, valEncoded, decode) {
263300
}
264301
}
265302

266-
// Parse a key/val string.
303+
/**
304+
* Parse a key/val string.
305+
* @param {string} qs
306+
* @param {string} sep
307+
* @param {string} eq
308+
* @param {{
309+
* maxKeys?: number;
310+
* decodeURIComponent?(v: string): string;
311+
* }} [options]
312+
* @returns {Record<string, string | string[]>}
313+
*/
267314
function parse(qs, sep, eq, options) {
268315
const obj = ObjectCreate(null);
269316

@@ -417,9 +464,14 @@ function parse(qs, sep, eq, options) {
417464
}
418465

419466

420-
// v8 does not optimize functions with try-catch blocks, so we isolate them here
421-
// to minimize the damage (Note: no longer true as of V8 5.4 -- but still will
422-
// not be inlined).
467+
/**
468+
* V8 does not optimize functions with try-catch blocks, so we isolate them here
469+
* to minimize the damage (Note: no longer true as of V8 5.4 -- but still will
470+
* not be inlined).
471+
* @param {string} s
472+
* @param {(v: string) => string} decoder
473+
* @returns {string}
474+
*/
423475
function decodeStr(s, decoder) {
424476
try {
425477
return decoder(s);

0 commit comments

Comments
 (0)