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

@viscoze/urlsearchparams

v1.2.2

Published

A small library to make working with the URL search string easier

Downloads

5

Readme

UrlSearchParams

A library to make working with a URL search string easier

Motivation

  1. URLSearchParams is not supporeted by EI10
  2. URLSearchParams can't work with nested query params
  3. URLSearchParams can't work with array query params

How to use

To get query param

To get a value of a query param you can specify an exact path to it

UrlSearchParams.fromString('query[obj][field1]=value1').get('query[obj][field1]')
// => value1

You can get velues as an object by a part of the path

UrlSearchParams.fromString('query[obj][field1]=value1&query[obj][field2]=value2').get('query[obj]')
// => {
//      field1: 'value1',
//      field2: 'value2',
//    }

If the query param is an array you will get an array of values

UrlSearchParams.fromString('query[]=valu1&query[]=valu2').get('query')
// => [
//      'value1',
//      'value2',
//    ]

To add query param

To add a query param you should provide an exact path and a value

UrlSearchParams.fromString('query[obj][field1]=value1')
  .set('query[obj][field2]', 'value2')
  .toObject()
// => {
//      query: {
//        obj: {
//          field1: 'value1',
//          field2: 'value2',
//        },
//      },
//    }

You can add another value to an array query param

UrlSearchParams.fromString('query[]=value1&query[]=value2').set('query[]', 'value3').toObject()
// => {
//      query: [
//        'value1',
//        'value2',
//        'value3',
//        },
//      ],
//    }

To update query param

To update a query param you should provide an exact path and a new value

UrlSearchParams.fromString('query[obj][field1]=value1&query[obj][field2]=value2')
  .set('query[obj][field2]', 'newValue2')
  .toObject()
// => {
//      query: {
//        obj: {
//          field1: 'value1',
//          field2: 'newValue2',
//        },
//      },
//    }

You can overwrite a nested query param by a part of the path

UrlSearchParams.fromString('query[obj][field1]=value1&query[obj][field2]=value2')
  .set('query', 'newValue1')
  .toObject()
// => {
//      query: newValue1,
//    }

You can overwrite an array query param by its exact pass without []

UrlSearchParams.fromString('query[]=value1&query[]=value2').set('query', 'value3').toObject()
// => {
//      query: value3,
//    }

To remove query param

To remove a query param you should provide an exact path and either null or undefined

UrlSearchParams.fromString('query[obj][field1]=value1&query[obj][field2]=value2')
  .set('query[obj][field2]', null)
  .toObject()
// => {
//      query: {
//        obj: {
//          field1: 'value1',
//        },
//      },
//    }

You can remove nested query params by a common part of the path

UrlSearchParams.fromString('query1[obj][field1]=value1&query1[obj][field2]=value2&query2=value3')
  .set('query1', null)
  .toObject()
// => {
//      query2: 'value3',
//    }

To convert

You can convert to an object

UrlSearchParams.fromString(
  'query1[obj][field1]=value1&query1[obj][field2]=value2&query2=value3',
).toObject()
// => {
//      query1: {
//        obj: {
//          field1: 'value1',
//          field2: 'value2',
//        },
//      },
//      query2: 'value3',
//    }

You can convert back to a string

UrlSearchParams.fromString('query[obj][field1]=value1')
  .set('query[obj][field1]', 'value2')
  .toString()
// => 'query[obj][field1]=value1&query[obj][field2]=value2'

Additional Info

All of the functions that accept the path to a value in a format like query[obj][field1] also can accept it as an array like ['query', 'obj', 'field1'] so that you can use you own format for the path.