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