Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 1x 173x 173x 91x 173x 260x 17x 17x 7x 17x 10x 10x 260x 243x 243x 260x 91x 91x | import type { TRec } from './types.js';
/**
* Parses a URL search string into a query object.
*
* Handles duplicate keys by converting them to arrays.
* Empty or missing search strings return an empty object.
*
* @param search - The URL search string (e.g., `?foo=bar&baz=qux`)
* @returns A record of query parameters, with arrays for duplicate keys
*
* @example
* ```ts
* parseQuery('?name=John&age=30');
* // Returns: { name: 'John', age: '30' }
*
* parseQuery('?tags=js&tags=ts');
* // Returns: { tags: ['js', 'ts'] }
*
* parseQuery('');
* // Returns: {}
* ```
*/
export function parseQuery(search: string): TRec {
const query: TRec = {};
if (!search || search === '?') return query;
const params = new URLSearchParams(search);
for (const [key, value] of params) {
if (key in query) {
const existing = query[key];
if (Array.isArray(existing)) {
existing.push(value);
} else {
query[key] = [existing as string, value];
}
} else {
query[key] = value;
}
}
return query;
}
|