Skip to content

A class designed to simplify working with URL search parameters in JavaScript.

License

Notifications You must be signed in to change notification settings

trosck/url-params

Repository files navigation

urlParams

Class that made for easy work with URL search params

ci npm publish

Table of Contents

browser-support

Chrome Firefox Safari Opera Edge
49 44 14 36 17

installing

Using npm:

npm install @troskey/url-params

Using yarn:

yarn add @troskey/url-params

Usage

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

Creating an instance

import { URLParams } from 'url-params'

new URLParams('https://github.com')
  .set('hello', 'world')
  .get('hello') // "world"

Using urlParams function

import { urlParams } from 'url-params'

urlParams('https://github.com')
  .set('hello', 'world')
  .get('hello') // "world"

Using urlParams proxy

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 minified version

import { URLParams, urlParams } from 'url-params/dist/index.min.js'

new URLParams().url

// ...

urlParams().url

// ...

urlParams.url

API

saveSate(default false) - using window.history.pushState
instead of window.history.replaceState

replaces window.location.href if none
of methods above is not available

url

Returns url string

set(name, value[, saveState])

Sets value with the given key

urlParams('https://github.com')
  .set('hello', 'world')
  .set('hi', 'web')
  .url // https://github.com?hello=world&hi=web

append(name, value[, saveState])

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

get(name)

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

getAll(name)

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') // []

getAllParams()

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"]]

delete(name[, saveState])

Deleting query param from url

urlParams('https://github.com?hello=world')
  .delete('hello')
  .url // https://github.com

toString()

Returns url string, can be used for auto cast to string

License

MIT