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

url-and-query

v3.1.1

Published

Utility functions for handling query parameters in Urls

Downloads

68

Readme

Url and Query

This package provides utility functions for handling query parameters in Urls. It is particularly useful for updating, extracting, and manipulating query parameters in a Url.

Installation

Install the package using npm:

npm install url-and-query

Install your favorite query parser:

npm install qs

Usage

import qs from 'qs';
import { defineUrl } from 'url-and-query';

const url = defineUrl(qs);

Examples

parse

const { baseUrl, queryParams } = url.parse('/example/path?param1=value1&param2=value2');
console.log(baseUrl, queryParams);
// Output: '/example/path', { param1: 'value1', param2: 'value2' }

stringify

const newUrl = url.stringify('/example/path', { param1: 'value1', param2: 'value2' });
console.log(newUrl);
// Output: '/example/path?param1=value1&param2=value2'

update

const updatedUrl = url.update('/example/path?param1=old', {
  param1: 'new'
});
console.log(updatedUrl);
// Output: { baseUrl: '/example/path', queryParams: { param1: 'new' } }

Customization

The queryString library that you choose empowers you with the flexibility to customize the parsing and stringifying of Urls to suit your specific needs. By inheriting the options of the chosen parser, this library allows you to conveniently set it once using defineUrl and apply it consistently whenever you parse or stringify your Urls.

defineUrl(parser, options)

Easily construct Urls with customizable options for the stringify() and parse() methods using defineUrl() factory.

Parameters

  • parser: An object representing the used parser for parsing query parameters.
  • options: An optional object with the following properties:
    • stringifyOptions: An array of options to pass into the stringify() method.
    • parseOptions: An array of options to pass into the parse() method.

Example

const url = defineUrl(qs, {
  stringifyOptions: [{ skipNulls: true }],
  parseOptions: [{ allowDots: true }]
});

Parse url with option { allowDots: true }

url.parse('myUrl.com?color.is=red&test=passed');
//Output: 'myUrl.com', { 'color.is': 'red', test: 'passed' }

Stringify url with option { skipNulls: true }

url.stringify('myUrl.com', { a: 1, b: null });
//Output: 'myUrl.com?a=1'

Override default settings

You have the flexibility to override the default settings for your parser or stringifier by providing a configuration to the respective parse/stringify method.

Example

Consider example defineUrl from above with parseOptions: [{ allowDots: true }].

You are able to override parseOptions on the spot by supplying additional parameter to parse method

// Overriding parseOptions on the spot to disallow dots
url.parse('myUrl.com?color.is=red&test=passed', {
  parseOptions: [{ allowDots: false }]
});
//Output: 'myUrl.com', { 'color.is': 'red', test: 'passed' }