npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

urlsearchparamsfactory

v1.0.1

Published

A helper factory function to work with query params on front end

Downloads

2

Readme

Url Search Params

A helper function to deal with url search params on front end

Installation

Install urlsearchparamsfactory with npm

  cd my-project
  npm install urlsearchparamsfactory

Usage/Examples

import urlSearchParams from 'urlsearchparams'
// add a param to url search param

const search = window.location.search // "?param1=value1"
const {addParam} = urlSearchParams(window.location.search)
const urlSearchParamsWithAddedParam = addParam({key:"param2",value:"value2"})
console.log(urlSearchParamsWithAddedParam) // "param1=value1&param2=value2"


// Add a list of params to URl search params

const { addParamList } = urlSearchParamsFactory('?key1=value1')
const urlSearchParamsWithAddedParamList = addParamList([{ key:"key2", value:"value2"},{ key:"key3", value:"value3"}])

console.log(urlSearchParamsWithAddedParamList) // "key1=value1&key2=value2&key3=value3"

// Remove param from URl search params
const { removeParam } = urlSearchParamsFactory('?key1=value1&key2=value2&key3=value3&key4=value4')
const urlSearchParamsWithRemovedParam = removeParam("key2")

console.log(urlSearchParamsWithRemovedParam) // "key1=value1&key3=value3&key4=value4"

// Remove a list of params from URl search params
const { removeParamList } = urlSearchParamsFactory('?key1=value1&key2=value2&key3=value3&key4=value4')
const urlSearchParamsWithRemovedParamList = removeParamList(["key2", "key3"])

console.log(urlSearchParamsWithRemovedParamList) // "key1=value1&key4=value4"

// Add or replace param => if param exists it replaces the param, if it does not exists it adds param
const { addOrReplaceParam } = urlSearchParamsFactory('?key1=value1&key2=value2')
const urlSearchParamsWithAddedOrReplacedParam = addOrReplaceParam({key:"key2", value:"replacedValue2"})

console.log(urlSearchParamsWithAddedOrReplacedParam) // "key1=value1&key2=replacedValue2"

// Add or replace param list => if param exists it replaces the param, if it does not exists it adds param
const { addOrReplaceParamList } = urlSearchParamsFactory('?key1=value1&key2=value2')
const urlSearchParamWithAddedAndReplacedParam = addOrReplaceParamList([{key:"key2", value:"replacedValue2"},{key:"key3", value:"addedValue3"}])

console.log(urlSearchParamWithAddedAndReplacedParam) // "key1=value1&key2=replacedValue2&key3=addedValue3"

// get specific param on url search params
const { getParam } = urlSearchParamsFactory('?key1=value1&key2=value2')
const param = getParam("key2")

console.log(param) // "value2"

// get list of params on url search params

const { getParamList } = urlSearchParamsFactory('?key1=value1&key2=value2&key3=value3')
const params = getParamList(["key2","key3"])

console.log(params) // { key2:"value2", key3:"value3"};

// get all params on url search params
const { getAllParams } = urlSearchParamsFactory('?key1=value1&key2=value2&key3=value3')
const params = getAllParams()

console.log(params) // { key1:"value1", key2:"value2", key3:"value3" };

// get url search params

const { getQueryParamUrl } = urlSearchParamsFactory('?key1=value1&key2=value2&key3=value3')
const queryParamUrl = getQueryParamUrl()

console.log(queryParamUrl) // "key1=value1&key2=value2&key3=value3";

// Compose => compose can be used to execute more than one function and returns the result url search params modified

const { compose, addOrReplaceParamList, removeParamList } = urlSearchParamsFactory('?key1=value1&key2=value2&key3=value3&key5=value5')
const modifiedUrlSearchParams = compose(
     () => addOrReplaceParamList([{key:"key1", value:"replacedValue1"},{key:"key4", value:"value4"}]),
     () => removeParamList(["key2", "key5"])
    )
    console.log(modifiedUrlSearchParams) // "?key1=replacedValue1&key3=value3&key4=value4"

Authors