* Parse a key/val string. * @param {string} qs * @param {string} sep * @param {string} eq * @param {{ * maxKeys?: number, * decodeURIComponent?: (v: string) => string, * }} [options] * @returns {Record }
(qs, sep, eq, options)
| 315 | * @returns {Record<string, string | string[]>} |
| 316 | */ |
| 317 | function parse(qs, sep, eq, options) { |
| 318 | const obj = { __proto__: null }; |
| 319 | |
| 320 | if (typeof qs !== 'string' || qs.length === 0) { |
| 321 | return obj; |
| 322 | } |
| 323 | |
| 324 | const sepCodes = (!sep ? defSepCodes : charCodes(String(sep))); |
| 325 | const eqCodes = (!eq ? defEqCodes : charCodes(String(eq))); |
| 326 | const sepLen = sepCodes.length; |
| 327 | const eqLen = eqCodes.length; |
| 328 | |
| 329 | let pairs = 1000; |
| 330 | if (options && typeof options.maxKeys === 'number') { |
| 331 | // -1 is used in place of a value like Infinity for meaning |
| 332 | // "unlimited pairs" because of additional checks V8 (at least as of v5.4) |
| 333 | // has to do when using variables that contain values like Infinity. Since |
| 334 | // `pairs` is always decremented and checked explicitly for 0, -1 works |
| 335 | // effectively the same as Infinity, while providing a significant |
| 336 | // performance boost. |
| 337 | pairs = (options.maxKeys > 0 ? options.maxKeys : -1); |
| 338 | } |
| 339 | |
| 340 | let decode = QueryString.unescape; |
| 341 | if (options && typeof options.decodeURIComponent === 'function') { |
| 342 | decode = options.decodeURIComponent; |
| 343 | } |
| 344 | const customDecode = (decode !== qsUnescape); |
| 345 | |
| 346 | let lastPos = 0; |
| 347 | let sepIdx = 0; |
| 348 | let eqIdx = 0; |
| 349 | let key = ''; |
| 350 | let value = ''; |
| 351 | let keyEncoded = customDecode; |
| 352 | let valEncoded = customDecode; |
| 353 | const plusChar = (customDecode ? '%20' : ' '); |
| 354 | let encodeCheck = 0; |
| 355 | for (let i = 0; i < qs.length; ++i) { |
| 356 | const code = StringPrototypeCharCodeAt(qs, i); |
| 357 | |
| 358 | // Try matching key/value pair separator (e.g. '&') |
| 359 | if (code === sepCodes[sepIdx]) { |
| 360 | if (++sepIdx === sepLen) { |
| 361 | // Key/value pair separator match! |
| 362 | const end = i - sepIdx + 1; |
| 363 | if (eqIdx < eqLen) { |
| 364 | // We didn't find the (entire) key/value separator |
| 365 | if (lastPos < end) { |
| 366 | // Treat the substring as part of the key instead of the value |
| 367 | key += StringPrototypeSlice(qs, lastPos, end); |
| 368 | } else if (key.length === 0) { |
| 369 | // We saw an empty substring between separators |
| 370 | if (--pairs === 0) |
| 371 | return obj; |
| 372 | lastPos = i + 1; |
| 373 | sepIdx = eqIdx = 0; |
| 374 | continue; |