Created
April 4, 2015 14:06
-
-
Save davydog187/dc5b228991c306261d21 to your computer and use it in GitHub Desktop.
Process url for cip6791
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 exampleInputs = [ | |
"http://example.com/", | |
"http://example.com/#hash", | |
"http://example.com/#hash?something=hello", | |
"http://example.com/#hash?something=hello&else=hi" | |
]; | |
function getHashValueFrom(url, hashIndex, queryStringIndex) { | |
if (hashIndex === -1) { | |
return ""; | |
} | |
if (queryStringIndex === -1) { | |
return url.substr(hashIndex + 1); | |
} | |
return url.substring(hashIndex + 1, queryStringIndex); | |
} | |
function getQueryStringFrom(url, hashIndex, queryStringIndex) { | |
if (queryStringIndex === -1) { | |
return {}; | |
} | |
return url.substr(queryStringIndex + 1).split("&").reduce(function(result, current) { | |
var keyValuePair = current.split("="); | |
result[keyValuePair[0]] = keyValuePair[1]; | |
return result; | |
}, {}); | |
} | |
function getStateFrom(url) { | |
var queryStringIndex = url.lastIndexOf("?"); | |
var hashIndex = url.lastIndexOf("#"); | |
return { | |
hash: getHashValueFrom(url, hashIndex, queryStringIndex), | |
queryString: getQueryStringFrom(url, hashIndex, queryStringIndex) | |
} | |
} | |
exampleInputs.forEach(function(url) { | |
console.log(getStateFrom(url)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: