Skip to content

Instantly share code, notes, and snippets.

@cplpearce
Created August 11, 2020 20:23
Show Gist options
  • Save cplpearce/10d205c53a7714eee351274155295b05 to your computer and use it in GitHub Desktop.
Save cplpearce/10d205c53a7714eee351274155295b05 to your computer and use it in GitHub Desktop.
Kata 14 - JS Object From URL Encoded String
const urlDecode = function(text) {
text = text.replace(/%20/g, ' ');
let strArray = text.split(/[(=)|(&)]+/);
let results = {};
for (let i = 0; i < strArray.length; i += 2)
results[strArray[i]] = strArray[i + 1];
return results
};
console.log(urlDecode("duck=rubber"));
console.log(urlDecode("bootcamp=Lighthouse%20Labs"));
console.log(urlDecode("city=Vancouver&weather=lots%20of%20rain"));
console.log(urlDecode("city=Vancouver&weather=lots%20of%20rain").weather);
/*
{duck: "rubber"}
{bootcamp: "Lighthouse Labs"}
{city: "Vancouver", weather: "lots of rain"}
"lots of rain"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment