Skip to content

Instantly share code, notes, and snippets.

@TimurM
Created July 15, 2015 04:56
Show Gist options
  • Save TimurM/1e086decc8a18bf24d3b to your computer and use it in GitHub Desktop.
Save TimurM/1e086decc8a18bf24d3b to your computer and use it in GitHub Desktop.
var parseString = function(/*string*/string) { /* array of strings */
return string.split("&");
}
var parseKeyValueString = function(/*array*/stringsArray) {
return stringsArray.reduce(function(subArray, keyValueString) {
var subArrayKeyValue = keyValueString.split("=")
return subArray.concat([subArrayKeyValue]);
},[])
}
var parseQueryString = function(/*list<pair<string, string>>*/ array) { /* map of key to value */
return array.reduce(function(queryMap, keyValueArray) {
var map = (queryMap[keyValueArray[0]] = keyValueArray[1]);
// console.log(queryMap)
return queryMap[keyValueArray[0]] ? queryMap : map;
}, {})
}
var queryTestString1 = "name=Timur&last_name=Meyster&[email protected]"
var queryTestString2 = "name=Timur&name=Timur&last_name=Meyster&[email protected]"
console.log(parseQueryString(parseKeyValueString(parseString(queryTestString1))));
console.log(parseQueryString(parseKeyValueString(parseString(queryTestString2))));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment