Class that made for easy work with URL search params
49 | 44 | 14 | 36 | 17 |
Using npm:
npm install @troskey/url-params
Using yarn:
yarn add @troskey/url-params
Methods delete, set and append returns this for
call chain, so you should use url or toString() to get
url string
If first argument not defined, will be used window.location.href
urlParams('https://github.com')
.set('hello', 'world')
.append('hello', 'web')
.url // https://github.com?hello=world&hello=web
import { URLParams } from 'url-params'
new URLParams('https://github.com')
.set('hello', 'world')
.get('hello') // "world"
import { urlParams } from 'url-params'
urlParams('https://github.com')
.set('hello', 'world')
.get('hello') // "world"
creates instance on every call, uses window.location.href
import { urlParams } from 'url-params'
// window.location.href = 'https://github.com'
urlParams
.set('hello', 'world')
.get('hello') // "world"
import { URLParams, urlParams } from 'url-params/dist/index.min.js'
new URLParams().url
// ...
urlParams().url
// ...
urlParams.url
saveSate(default false) - using window.history.pushState
instead of window.history.replaceState
replaces window.location.href if none
of methods above is not available
Returns url string
Sets value with the given key
urlParams('https://github.com')
.set('hello', 'world')
.set('hi', 'web')
.url // https://github.com?hello=world&hi=web
Appends value with the given key
urlParams('https://github.com')
.append('hello', 'world')
.append('hello', 'there', true)
.url // https://github.com?hello=world&hello=there
Returns first searched item from left, otherwise null
urlParams('https://github.com?hello=world&hello=there')
.get('hello') // "world"
urlParams('https://github.com')
.get('hi') // null
Returns all values of query param in array
urlParams('https://github.com?hello=world&hello=there')
.getAll('hello') // ["world", "there"]
urlParams('https://github.com')
.getAll('hello') // []
Returns all query params in two-dimensional array
urlParams('https://github.com?hello=world&hello=there&test=123')
.getAllParams() // [["hello", "world"], ["hello", "there"], ["test", "123"]]
Deleting query param from url
urlParams('https://github.com?hello=world')
.delete('hello')
.url // https://github.com
Returns url string, can be used for auto cast to string