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

@m59/qs

v2.0.3

Published

Functions for working with query strings.

Downloads

194

Readme

@m59/qs

Functions for working with query strings.

Build Status

install

$ npm install @m59/qs

example

import { stringify, parse, extract, replace } from '@m59/qs'

stringify({ crust: 'thin', toppings: ['pepperoni', 'pepperoni', 'bacon'] })
// => 'crust=thin&toppings=pepperoni&toppings=pepperoni&toppings=bacon'

parse('crust=thin&toppings=pepperoni&toppings=pepperoni&toppings=bacon')
// => { crust: 'thin', toppings: ['pepperoni', 'pepperoni', 'bacon'] }

extract('http://pizza.pl0x?breadsticks=please#yum')
// => 'breadsticks=please'

replace('http://pizza.pl0x?breadsticks=please#yum', queryString => 'breadsticks=seriously&marinara')
// => 'http://pizza.pl0x?breadsticks=seriously&marinara#yum'

array formats

stringify() can output array values in different formats. parse() will parse all of them.

A key with a bracket will always have a value that is an array, even if the key appears only once.

// "foo" appears once - treat as single value
parse('foo=a') // => { foo: 'a' }

// "foo" appears twice - treat as array
parse('foo=a&foo=b') // => { foo: [ 'a', 'b' ] }

// "foo" appears once, but has a bracket - treat as array
parse('foo[]=a') // => { foo: [ 'a' ] }

duplicate (default)

stringify({ foo: ['a', 'b', 'c'] }, { arrayFormat: 'duplicate' })
// => 'foo=a&foo=b&foo=c'

bracket

stringify({ foo: ['a', 'b', 'c'] }, { arrayFormat: 'bracket' })
// => 'foo[]=a&foo[]=b&foo[]=c'

index

Indices are only aesthetic. They do not necessarily correspond to array index. Items should appear in order.

stringify({ foo: ['a', 'b', 'c'] }, { arrayFormat: 'index' })
// => 'foo[0]=a&foo[1]=b&foo[2]=c'

json

Turn arrays into JSON strings.

stringify({ foo: [true, 123, 'abc', {x: false}] }, { arrayFormat: 'json' })
// => 'foo=%5Btrue%2C123%2C%22abc%22%2C%7B%22x%22%3Afalse%7D%5D'

delimiters

Arrays can be output as a single key=value pair with the items joined by a delimiter.

stringify({ foo: ['a', 'b', 'c'] }, { arrayFormat: { delimiter: ';' } })
// => 'foo=a;b;c'

Values with delimiters can be parsed as arrays.

parse('foo=a;b;c', { delimiters: [';'] })
// => { foo: ['a', 'b', 'c'] }

If parse() is given multiple delimiters, the first given delimiter that exists in the value will be used to split it into an array.

parse('foo=a;b;c&bar=a,b,c&baz=,;,', { delimiters: [';', ','] })
// => { foo: ['a', 'b', 'c'], bar: ['a', 'b', 'c'], baz: [',', ','] }

nesting

Objects and nested arrays will be stringified as JSON. JSON strings are parsed back to objects by default.

+

According to RFC 1738, a space should be encoded as +, and + should be parsed back to a space. That specification was updated in 2005 by RFC 3986, which states that + should be encoded as UTF-8 %20. Query strings ought to adhere to 3986 and therefore the meaning of + in a query string is technically ambiguous. However, many query string encoders still encode spaces to +, so parsers must parse + as a space.

API

parse(queryString, options)

  • queryString: string a query string
  • options: object
    • json: boolean, true when a value is a JSON string, parse it to object
    • delimiters: [...strings], [] parse values as arrays when they contain a delimiter
    • plus: boolean, true when true (default), decode + into a space
  • => object parsed query string

stringify(object, options)

  • object: object object to be stringified into a query string
  • options: object
    • arrayFormat: string | object, 'duplicate'
      • 'duplicate'
      • 'bracket'
      • 'index'
      • { delimiter: string }
    • plus: boolean, false when true, encode spaces as +
  • => string query string

extract(uri)

  • uri: string uri to extract the query string from
  • => string extracted query string

replace(uri, replacer, options)

  • uri: string uri to replace the query string in
  • replacer: string | function
    • string new query string value
    • replacer(queryString)
      • queryString: string the query string from uri or an empty string if uri does not have a query string
      • uri the uri argument passed to replace
      • => newQueryString: string new query string value
  • options: object
    • separator: boolean, false when true, the ? separator will be included in the replacement, so queryString passed to replacer will include it if it exists in uri, and it will be replaced in uri by replacer.
  • => string uri with the query string replaced