Last active
November 30, 2016 15:23
-
-
Save aprilandjan/c1a9f650b1de3917d793a4bac331f9cd 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
function parseQuery () { | |
var search = window.location.search | |
if (search) { | |
search = search.substr(1) | |
let args = search.split('&') | |
let query = {} | |
args.forEach(pair => { | |
let arr = pair.split('=') | |
let key = arr.shift() | |
let value = arr.join('=') | |
query[key] = decodeURIComponent(value) | |
}) | |
return query | |
} | |
return {} | |
} | |
function formQuery (query) { | |
let arr = [] | |
for (let key in query) { | |
let value = query[key] | |
arr.push(`${key}=${encodeURIComponent(value)}`) | |
} | |
let search = arr.join('&') | |
if (search) { | |
return '?' + search | |
} | |
return '' | |
} | |
export default { | |
parseQuery, | |
formQuery | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment