MCPcopy Create free account
hub / github.com/sindresorhus/query-string / parseValue

Function parseValue

base.js:458–498  ·  view source on GitHub ↗
(value, options, type)

Source from the content-addressed store, hash-verified

456}
457
458function 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
500export function extract(input) {
501 input = removeHash(input);

Callers 1

parseFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected