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

Function parse

base.js:506–603  ·  view source on GitHub ↗
(query, options)

Source from the content-addressed store, hash-verified

504}
505
506export function parse(query, options) {
507 options = {
508 decode: true,
509 sort: true,
510 arrayFormat: 'none',
511 arrayFormatSeparator: ',',
512 parseNumbers: false,
513 parseBooleans: false,
514 ...definedOptions(options),
515 types: {__proto__: null, ...options?.types},
516 };
517
518 validateArrayFormatSeparator(options.arrayFormatSeparator);
519
520 const formatter = parserForArrayFormat(options);
521
522 // Create an object with no prototype
523 const returnValue = Object.create(null);
524
525 if (typeof query !== 'string') {
526 return returnValue;
527 }
528
529 query = query.trim().replace(/^[#&?]/v, '');
530
531 if (!query || /^&+$/v.test(query)) {
532 return returnValue;
533 }
534
535 let parameterStart = 0;
536 let firstSeparator = query.indexOf('&');
537 if (firstSeparator === -1) {
538 firstSeparator = query.length;
539 }
540
541 for (let index = firstSeparator; index <= query.length; index++) {
542 if (index < query.length && query[index] !== '&') {
543 continue;
544 }
545
546 if (index === parameterStart) {
547 parameterStart = index + 1;
548 continue;
549 }
550
551 const parameter = query.slice(parameterStart, index);
552 const parameter_ = options.decode ? parameter.replaceAll('+', ' ') : parameter;
553
554 let [key, value] = splitOnFirst(parameter_, '=');
555
556 if (key === undefined) {
557 key = parameter_;
558 }
559
560 // Missing `=` should be `null`:
561 // https://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
562 value = value === undefined ? null : (['comma', 'separator', 'bracket-separator'].includes(options.arrayFormat) ? value : decode(value, options));
563

Callers 3

parseUrlFunction · 0.85
stringifyUrlFunction · 0.85
pickFunction · 0.85

Calls 6

definedOptionsFunction · 0.85
parserForArrayFormatFunction · 0.85
decodeFunction · 0.85
arrayFromIndexesFunction · 0.85
parseValueFunction · 0.85

Tested by

no test coverage detected