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-search-utils

v0.2.2

Published

Utils for manipulate search params in query string.

Downloads

123

Readme

NPM

url-search-utils

A simple set of utils for manipulate search params in query string. Supports the hash and browser histories.

Installation

npm install url-search-utils --save

Api docs

Parsing query

import {parseSearchParams, parseHashParams, parseHashLocationQuery} from 'url-search-utils';

...

// parse get arguments
// e.g. http://127.0.0.1:8080/page/?foo=5&bar=baz
parseSearchParams(config)

// parse get-like arguments in hash
// e.g. http://127.0.0.1:8080/page/#foo=5&bar=baz
parseHashParams(config)

// parse location-query-like arguments in hash
// e.g. http://127.0.0.1:8080/#/page/?foo=5&bar=baz
parseHashLocationQuery(config)

config is an object with keys is get-params names and values is one of:

  • 'number' for numeric params.
  • 'array-of-strings' for arrays of strings.
  • 'array-of-numbers' for arrays of numbers.
  • 'exclude' for exclude param from result.
  • function(value, accumulator = initialValue) {...} where value is current value of found get-param and accumulator is value from previous function call if param with current name occurs several times (undefined by default).

For example:

// current query is '?param1=value1&numberparam=2&strarray=str1&strarray=str2&strarray=str3&date=2017-09-06'

const parsed = parseSearchParams({
  numberparam: 'number',
  strarray: 'array-of-strings',
  date: (dateStr) => moment(dateStr, 'YYYY-MM-DD'),
})

//  returns
//  {
//    param1: 'value1',
//    numberparam: 2,
//    strarray: ['str1', 'str2', 'str3'],
//    date: moment('2017-09-06', 'YYYY-MM-DD'),
//  }

Setting query

import {setSearchParams, setHashParams, setHashLocationQuery} from 'url-search-utils';

...

// set get arguments
// e.g. http://127.0.0.1:8080/page/?foo=5&bar=baz
setSearchParams(values, mapParamsNames, config)

// set get-like arguments in hash
// e.g. http://127.0.0.1:8080/page/#foo=5&bar=baz
setHashParams(values, mapParamsNames, config)

// set location-query-like arguments in hash
// e.g. http://127.0.0.1:8080/#/page/?foo=5&bar=baz
setHashLocationQuery(values, mapParamsNames, config)

values is an object that should be setted to query.

mapParamsNames is an object with keys is specific keys from values and values is params names for set to query.

config is an object with keys is specific keys from values (or values from mapParamsNames) and values:

  • 'include-if-falsy' for include params with falsy values as empty strings to query.
  • 'exclude' for exclude param from result.
  • function(value) {...} where value is value for serialize. If returns null, param won't included to search.

For example:

setSearchParams({
  page: 3,
  perPage: 20,
  date: moment(),
  filters: [1, 2, 3],
}, {
  perPage: 'page_size',
}, {
  date: (value) => value.format('YYYY-MM-DD'),
})

//  sets query
//  '?page=3&page_size=20&date=2017-09-06&filters=1&filters=2&filters=3'