MCPcopy
hub / github.com/sindresorhus/query-string / parse

Function parse

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

Source from the content-addressed store, hash-verified

367}
368
369export function parse(query, options) {
370 options = {
371 decode: true,
372 sort: true,
373 arrayFormat: 'none',
374 arrayFormatSeparator: ',',
375 parseNumbers: false,
376 parseBooleans: false,
377 types: Object.create(null),
378 ...options,
379 };
380
381 validateArrayFormatSeparator(options.arrayFormatSeparator);
382
383 const formatter = parserForArrayFormat(options);
384
385 // Create an object with no prototype
386 const returnValue = Object.create(null);
387
388 if (typeof query !== 'string') {
389 return returnValue;
390 }
391
392 query = query.trim().replace(/^[?#&]/, '');
393
394 if (!query) {
395 return returnValue;
396 }
397
398 // Avoid `split('&')` so separator-heavy inputs don't allocate large arrays of empty strings.
399 let parameterStart = 0;
400
401 for (let index = 0; index <= query.length; index++) {
402 if (index < query.length && query[index] !== '&') {
403 continue;
404 }
405
406 if (index === parameterStart) {
407 parameterStart = index + 1;
408 continue;
409 }
410
411 const parameter = query.slice(parameterStart, index);
412 parameterStart = index + 1;
413 const parameter_ = options.decode ? parameter.replaceAll('+', ' ') : parameter;
414
415 let [key, value] = splitOnFirst(parameter_, '=');
416
417 if (key === undefined) {
418 key = parameter_;
419 }
420
421 // Missing `=` should be `null`:
422 // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
423 value = value === undefined ? null : (['comma', 'separator', 'bracket-separator'].includes(options.arrayFormat) ? value : decode(value, options));
424 formatter(decode(key, options), value, returnValue);
425 }
426

Callers 2

parseUrlFunction · 0.85
stringifyUrlFunction · 0.85

Calls 5

parserForArrayFormatFunction · 0.85
decodeFunction · 0.85
parseValueFunction · 0.85
keysSorterFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…