| 456 | } |
| 457 | |
| 458 | function parseValue(value, options, type) { |
| 459 | if (type === 'string' && typeof value === 'string') { |
| 460 | return value; |
| 461 | } |
| 462 | |
| 463 | if (typeof type === 'function' && typeof value === 'string') { |
| 464 | return type(value); |
| 465 | } |
| 466 | |
| 467 | if (type === 'boolean' && value === null) { |
| 468 | return true; |
| 469 | } |
| 470 | |
| 471 | if (type === 'boolean' && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) { |
| 472 | return value.toLowerCase() === 'true'; |
| 473 | } |
| 474 | |
| 475 | if (type === 'boolean' && value !== null && (value.toLowerCase() === '1' || value.toLowerCase() === '0')) { |
| 476 | return value.toLowerCase() === '1'; |
| 477 | } |
| 478 | |
| 479 | if ((type === 'string[]' || type === 'number[]') && options.arrayFormat !== 'none' && (typeof value === 'string' || value === null)) { |
| 480 | // A single value is still an array. The item is parsed with the element type, so a value that is not a number stays a string. A valueless key is an array with a `null` item, like a valueless item of a repeated key. |
| 481 | return [parseValue(value, options, type.slice(0, -2))]; |
| 482 | } |
| 483 | |
| 484 | if (type === 'number' && typeof value === 'string' && value.trim() !== '' && !Number.isNaN(Number(value))) { |
| 485 | return Number(value); |
| 486 | } |
| 487 | |
| 488 | // An explicit type takes precedence over `parseNumbers` and `parseBooleans`. A value that does not match its type stays a string. With `arrayFormat: 'none'`, a single value with an array type is not an array and stays a string too. |
| 489 | if (['boolean', 'number', 'string[]', 'number[]'].includes(type)) { |
| 490 | return value; |
| 491 | } |
| 492 | |
| 493 | if (value !== null && options.parseBooleans && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) { |
| 494 | return value.toLowerCase() === 'true'; |
| 495 | } |
| 496 | |
| 497 | return typeof value === 'string' && options.parseNumbers && value.trim() !== '' && !Number.isNaN(Number(value)) ? Number(value) : value; |
| 498 | } |
| 499 | |
| 500 | export function extract(input) { |
| 501 | input = removeHash(input); |