Created
July 15, 2015 04:56
-
-
Save TimurM/1e086decc8a18bf24d3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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