Skip to content

Instantly share code, notes, and snippets.

@aprilandjan
Last active November 30, 2016 15:23
Show Gist options
  • Save aprilandjan/c1a9f650b1de3917d793a4bac331f9cd to your computer and use it in GitHub Desktop.
Save aprilandjan/c1a9f650b1de3917d793a4bac331f9cd to your computer and use it in GitHub Desktop.
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