1.38.0parseQuery
Parses a URL query string into a record of string key-value pairs.
parseQuery(query):Record<string, string>Behavior
The function creates a URLSearchParams instance from the provided query. If the query starts with ?, that first character is removed before parsing.
Parsing
Each entry from URLSearchParams is copied into a plain Record<string, string> object.
Duplicate Keys
If the same key appears multiple times, assigning each entry to the result object causes later values to overwrite earlier values.
URL Encoding
Query parsing and decoding follow the native behavior of URLSearchParams.
Empty Query
An empty query produces an empty object.
Parameters
querystringrequiredThe query string to parse. A leading question mark is optional.
Returns
Record<string, string>Returns an object containing the parsed query parameters as string key-value pairs.
Examples
Basic usage
Parse a query string into an object.
import { parseQuery } from "@cyref/js";
parseQuery("page=2&limit=10");
// {
// page: "2",
// limit: "10"
// }With a question mark
A leading question mark is automatically removed before parsing.
parseQuery("?page=2&sort=latest");
// {
// page: "2",
// sort: "latest"
// }Encoded values
Values are decoded according to URLSearchParams behavior.
parseQuery("search=hello+world");
// {
// search: "hello world"
// }Repeated keys
When a key appears multiple times, the last value assigned to that key remains in the returned object.
parseQuery("tag=javascript&tag=typescript");
// {
// tag: "typescript"
// }