Skip to content

Instantly share code, notes, and snippets.

@nyawach
Created November 16, 2018 02:17
Show Gist options
  • Save nyawach/71782cee5cb4627ca114f4919e916af4 to your computer and use it in GitHub Desktop.
Save nyawach/71782cee5cb4627ca114f4919e916af4 to your computer and use it in GitHub Desktop.
クエリをパース/文字列化するやつ
class QueryString {
parse(search) {
let obj = {}
search = search.replace(/^\?/, "")
const pairs = search.split("&")
pairs.forEach(pair => {
const [key, val] = pair.split("=")
obj[key] = val
})
return obj
}
stringify(obj = {}, { question = false } = {}) {
let str = ""
let keys = Object.keys(obj)
keys.forEach((key, i) => {
if(i > 0) str += "&"
else if(question) str += "?"
str += `${key}=${obj[key]}`
return str
})
return str
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment