1. function exportQueryString(queryString) {
  2. /**
  3. * Parses a query string and returns an object with key-value pairs.
  4. * Useful for debugging.
  5. * @param {string} queryString The query string to parse.
  6. * @returns {object} An object representing the query string. Returns an empty object if the input is not a string or is empty.
  7. */
  8. if (typeof queryString !== 'string' || queryString.trim() === "") {
  9. return {};
  10. }
  11. const params = {};
  12. const pairs = queryString.substring(queryString.indexOf('?') + 1).split('&'); // Extract parameters
  13. for (const pair of pairs) {
  14. const [key, value] = pair.split('='); // Split each pair into key-value
  15. params[decodeURIComponent(key)] = decodeURIComponent(value || ''); // Decode and add to object
  16. }
  17. return params;
  18. }
  19. // Example usage (for testing/demonstration)
  20. // const queryString = "name=John&age=30&city=New%20York";
  21. // const params = exportQueryString(queryString);
  22. // console.log(params); // Output: { name: 'John', age: '30', city: 'New York' }
  23. // Export the function (for use in other modules) - although the prompt asks for just the code.
  24. // module.exports = exportQueryString;

Add your comment