Hone logo
Hone
Problems

JavaScript Query String Parser

This challenge asks you to build a robust JavaScript function that parses a URL query string into a structured JavaScript object. Understanding how to parse query strings is fundamental for web development, enabling you to extract and process data passed in URLs for dynamic content and API interactions.

Problem Description

You need to create a JavaScript function named parseQueryString that takes a single argument: a string representing a URL query string (e.g., "key1=value1&key2=value2"). The function should return a JavaScript object where keys are the parameter names and values are their corresponding decoded string values.

Key Requirements:

  • Key-Value Pairs: The function must correctly identify and separate key-value pairs separated by the & symbol.
  • Decoding: Both keys and values should be URL-decoded. For example, %20 should become a space.
  • Empty Values: If a parameter has no value (e.g., "key="), its value in the object should be an empty string ("").
  • Parameters without Values: If a parameter has no = sign (e.g., "flag"), its value in the object should be null.
  • Duplicate Keys: If a key appears multiple times, the function should store all its values as an array. The order of values in the array should correspond to their order in the query string.
  • Empty Query String: An empty input string should result in an empty object.
  • Malformed Pairs: Ignore any malformed key-value pairs that do not adhere to the key=value format after splitting by &.

Examples

Example 1:

Input: "name=John%20Doe&age=30&city=New%20York"
Output: {
  name: "John Doe",
  age: "30",
  city: "New York"
}
Explanation: The query string is split into three pairs. Each key and value is URL-decoded.

Example 2:

Input: "category=electronics&sort=price&filter=red&filter=blue"
Output: {
  category: "electronics",
  sort: "price",
  filter: ["red", "blue"]
}
Explanation: The 'filter' key appears twice, so its values are stored in an array.

Example 3:

Input: "user=admin&active&role=&id=123"
Output: {
  user: "admin",
  active: null,
  role: "",
  id: "123"
}
Explanation: "active" has no value assigned, so it's `null`. "role" has an empty value.

Example 4:

Input: ""
Output: {}
Explanation: An empty query string results in an empty object.

Example 5:

Input: "invalid-pair&key1=value1"
Output: {
  key1: "value1"
}
Explanation: The malformed "invalid-pair" is ignored.

Constraints

  • The input query string will be a string.
  • The query string will not contain the ? character at the beginning.
  • The function should handle query strings of varying lengths, from empty to potentially thousands of characters.
  • Performance is a consideration, but correctness is paramount.

Notes

  • Consider using JavaScript's built-in decodeURIComponent() function for URL decoding.
  • Think about how to handle the conversion of single values to arrays when duplicates are encountered.
  • Pay close attention to the distinction between parameters with no value (e.g., flag) and parameters with an empty value (e.g., key=).
Loading editor...
javascript