Skip to content

Instantly share code, notes, and snippets.

@westc
Last active February 25, 2022 15:22
Show Gist options
  • Save westc/926e337baa74b931a156ab1f1e7f3b14 to your computer and use it in GitHub Desktop.
Save westc/926e337baa74b931a156ab1f1e7f3b14 to your computer and use it in GitHub Desktop.
parseUrlQuery() Snippet - Parse URL query parameters.
function parseUrlQuery(queryString) {
if (queryString.includes("=")) {
return JSON.stringify(
queryString.replace(/\+/g, '%20').split('&').reduce((dict, x) => {
var parts = x.split('=', 2);
dict[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]) || null;
return dict;
}, {}),
null, // No replacer
2 // indent with 2 spaces
);
}
return "";
}
{
// Where the JS code lies
"files": ["parseUrlQuery.js"],
// Name of the function responsable for transforming the input into the output.
"transform": "parseUrlQuery",
// Input setup
"input": { "language": "text", "label": "Query String" },
// Output setup
"output": { "language": "json", "label": "Resulting Parameters" },
// Type of YourJS IO-App
"type": "split"
}
@westc
Copy link
Author

westc commented Feb 25, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment