1. /**
  2. * Initializes query string components for testing.
  3. *
  4. * @param {string} queryString The query string to parse.
  5. * @returns {object} An object containing the parsed query string components.
  6. */
  7. function initQueryString(queryString) {
  8. const params = {};
  9. if (queryString) {
  10. const pairs = queryString.startsWith('?') ? queryString.slice(1).split('&') : queryString.split('&');
  11. for (const pair of pairs) {
  12. if (pair) {
  13. const [key, value] = pair.split('=');
  14. params[key] = value || true; // Default to true if no value is provided.
  15. }
  16. }
  17. }
  18. return params;
  19. }
  20. // Example usage (for testing purposes)
  21. // const queryString = "param1=value1&param2=value2&param3";
  22. // const params = initQueryString(queryString);
  23. // console.log(params); // Output: { param1: 'value1', param2: 'value2', param3: true }
  24. // Support for older browsers that might not have startsWith
  25. function initQueryStringOldBrowser(queryString) {
  26. const params = {};
  27. if (queryString) {
  28. const pairs = queryString.split('&');
  29. for (const pair of pairs) {
  30. if (pair) {
  31. const [key, value] = pair.split('=');
  32. params[key] = value || true;
  33. }
  34. }
  35. }
  36. return params;
  37. }

Add your comment