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

query-string-manipulator

v1.2.2

Published

Manipulate query strings without effort

Downloads

1,592

Readme

Query String Manipulator

CircleCI

Effortlessly manipulate query string parameters into your desired URL. You pass url and a set of actions to be done to QSM and you get you URL string back.

Install

QSM is written for ES modules

npm install query-string-manipulator

Usage

Lets assume that you already have it imported

import qsm from 'query-string-manipulator';

Set parameters

Lets say that you want to add page number to a search result.

qsm('https://www.google.cz/search?q=hello+world', {
  set: {
    num: 20,
  }
});
// https://www.google.cz/search?q=hello+world&num=20

It also works if the page number is already set

qsm('https://www.google.cz/search?q=hello+world&num=20', {
  set: {
    num: 40,
  }
});
// https://www.google.cz/search?q=hello+world&num=40

It also works when passing a parameter as an array.

qsm('https://www.google.cz/search?q=hello+world&num=20', {
  set: {
    num: [20, 40, 60],
  }
});
// https://www.google.cz/search?q=hello+world&num=20&num=40&num=60

Remove parameters

Say that you now want to go back to first page

qsm('https://www.google.cz/search?q=hello+world&num=20', {
  remove: ['num']
});
// https://www.google.cz/search?q=hello+world

Or go to the empty search page

qsm('https://www.google.cz/search?q=hello+world&num=20', {
  remove: ['q', 'num']
});
// https://www.google.cz/search

Toggle parameters

Say that you have a button on your page that enables filter and disables it when you click it again.

qsm('https://www.google.cz/search?q=hello+world&num=20', {
  toggle: {
    tbm: 'isch',
  }
});
// https://www.google.cz/search?q=hello+world&num=20&tbm=isch

qsm('https://www.google.cz/search?q=hello+world&num=20&tbm=isch', {
  toggle: {
    tbm: 'isch',
  }
});
// https://www.google.cz/search?q=hello+world&num=20

Constants

If you like "symbols", you can go like this:

import qsm, {
  URL_REMOVE, // Used for remove
  URL_SET, // Used for set
  URL_TOGGLE, // Used for toggle
} from 'query-string-manipulator';

qsm('http://example.com/', {
  [URL_REMOVE]: ['test'],
  [URL_TOGGLE]: {
    foo: 'bar',
  },
  [URL_SET]: {
    xxx: '123',
  },
})

Support methods

But wait, there is more!

Getting URL params

Method getUrlParams returns list of all parameters in form of array of objects. It cannot be returned in form of key-pair values because there can be multiple same name query params.

getUrlParams('https://example.com/foo?select=users&getId=10')

/* returns
[
  {
    key: 'select',
    value: 'users'
  },
  {
    key: 'getId',
    value: '10',
  }
]
*/

Resolve URL params

Method resolveUrlParams returns parameters after changed by user specified actions.

const urlParams = [
  {
    key: 'select',
    value: 'users'
  },
  {
    key: 'getId',
    value: '10'
  }
];
const paramActions = {
  remove: ['getId'],
  set: {
    select: 'userGroups',
  },
};
resolveUrlParams(urlParams, paramActions)

/* returns
[
  {
    key: 'select',
    value: 'userGroups'
  }
]
*/

Putting params together

Method constructUrlParams returns query string part of the URL from parameters.

constructUrlParams([
  {
    key: 'select',
    value: 'users'
  },
  {
    key: 'getId',
    value: '10'
  }
])

// returns "select=users&getId=10"