Socket
Socket
Sign inDemoInstall

parameller

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    parameller

Easily parse, edit and add url query paramaters with JS, has TypeScript support


Version published
Weekly downloads
195
increased by103.13%
Maintainers
1
Install size
7.67 kB
Created
Weekly downloads
 

Readme

Source

Parameller

CircleCI License Downloads NPM Size

Parameller makes parsing url query strings in JS simple.

Installation

Parameller is easy to install, simply run:

yarn add parameller

or

npm install parameller

Usage

Simply import the functions you wish to use in your project as follows:

import { getParam, getParams, removeParam, setParam, setParams, toQueryString, pushParam, pushParams, popParams } from "parameller";

Types

type Params = { [s: string]: string }

Methods

toQueryString(params: { [s: string]: string }, base: string = ""): string
const obj = {
    "test": "example",
    "name": "Samuel"
}

toQueryString(obj)

Output: test=example&name=Samuel

If the base property is set then it will prepend the base onto the query string.

getParam(param: string, callback?: (v: string) => void): string | undefined

Example url: http://test.com/?test=hello&example=wow

getParam("test") // returns "hello"
getParam("example") // returns "wow"
getParam("something") // returns undefined

getParam() takes an optional callback that is only called if the parameter returns a value.

getParam("test", value => {
    console.log(value) // logs "Hello"
})
getParams(string: string = window.location.search): Params

Example url: http://test.com/?test=hello&example=wow

getParams() // returns { test: "hello", example: "wow" }

Replace State

The following methods will replace the currently url state, history will not be preserved.

setParam(param: string, value: string): Params

Example url: http://test.com/?test=hello&example=wow

setParam("test", "goodbye")

Output: http://test.com/?test=goodbye&example=wow

setParams(params: Params): Params

Example url: http://test.com/?test=hello&example=wow

setParams({ test: "goodbye" })

Output: http://test.com/?test=goodbye

removeParam(param: string): Params

Example url: http://test.com/?test=hello&example=wow

removeParam("test")

Output: http://test.com/?example=wow

Push State

The following methods will push the state onto the browser history stack, useful if you want to maintain back/forwards functionality.

pushParam(param: string, value: string): Params

Example url: http://test.com/?test=hello&example=wow

pushParam("test", "goodbye")

Output: http://test.com/?test=goodbye&example=wow

pushParams(params: Params): Params

Example url: http://test.com/?test=hello&example=wow

pushParams({ test: "goodbye" })

Output: http://test.com/?test=goodbye

popParam(param: string): Params

Example url: http://test.com/?test=hello&example=wow

popParam("test")

Output: http://test.com/?example=wow

FAQs

Last updated on 04 Jun 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc