Cleanup updateQueryString composable

This commit is contained in:
Kasper Seweryn 2022-04-23 10:09:40 +02:00 committed by Georg Krause
commit 934f802be2
2 changed files with 3 additions and 9 deletions

View file

@ -1,9 +1,5 @@
export default (uri: string, key: string, value: string) => {
const re = new RegExp(`([?&])${key}=.*?(&|$)`, 'i')
if (uri.match(re)) {
return uri.replace(re, `$1${key}=${value}$2`)
} else {
const separator = uri.indexOf('?') !== -1 ? '&' : '?'
return `${uri}${separator}${key}=${value}`
}
const url = new URL(uri)
url.searchParams.set(key, value)
return url.href
}