Created
July 12, 2016 09:11
-
-
Save thesabbir/71c642e54714134a07b77813e34ad58c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Takes a raw param strings then converts it to int if integer | |
* If separated with , then return an array | |
* @param rawParams | |
* @returns {Params Item Array} || { Param Item} | |
*/ | |
function paramsMake(rawParams) { | |
const param = rawParams.split(',').map(item => { | |
if (Number.isNaN(parseInt(item, 8))) return item; | |
return parseInt(item, 8); | |
}); | |
return (param.length <= 1) ? param[0] : param; | |
} | |
/** | |
* @param paramString | |
* @returns { Params } | |
*/ | |
function transformToAssocArray(paramString) { | |
const params = {}; | |
const paramsArray = paramString.split('&'); | |
for (let i = 0; i < paramsArray.length; i++) { | |
const tmp = paramsArray[i].split('='); | |
params[tmp[0]] = paramsMake(tmp[1]); | |
} | |
return params; | |
} | |
/** | |
* gets get parameters | |
* @returns { Void } | |
*/ | |
function getSearchParameters() { | |
const paramString = window.location.search.substr(1); | |
if (paramString !== null && paramString !== '') { | |
return transformToAssocArray(paramString); | |
} | |
return {}; | |
} | |
export default getSearchParameters; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment