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

qs-like

v1.0.1

Published

A tiny query string parsing and stringifying library.

Downloads

267

Readme

qs-like

npm package

A tiny query string parsing and stringifying library.

NPM version NPM Downloads

Installation

In a browser

1.x

<script src="//cdn.jsdelivr.net/npm/[email protected]/qs-like.umd.min.js"></script>
<script>
  // window.qsLike
  qsLike.escape
  qsLike.parse
  qsLike.prefix
  qsLike.stringify
  qsLike.unescape
</script> 

2.x

<script src="//cdn.jsdelivr.net/npm/qs-like/umd.min.js"></script>
<!-- <script src="//cdn.jsdelivr.net/npm/qs-like/iife.min.js"></script> -->
<script>
  // window.qsLike
  qsLike.append;
  qsLike.decode;
  qsLike.parse;
  qsLike.encode;
  qsLike.stringify;
  qsLike.escape;
  qsLike.unescape;
</script>

Using npm

npm install qs-like --save
var append = require('qs-like/append');
var decode = require('qs-like/decode');
var encode = require('qs-like/encode');
var escape = require('qs-like/escape');
var parse = require('qs-like/parse');
var stringify = require('qs-like/stringify');
var unescape = require('qs-like/unescape');
export { 
  append,
  decode,
  encode,
  escape,
  parse,
  stringify,
  unescape,
} from 'qs-like/es';

API

append(url, query[, opts])

  • url <string> a string URL to append to.
  • query <string|object> a string or object containing query params to append.
  • options [object]
    • encodeURIComponent [Function] The function to use when converting URL-unsafe characters to percent-encoding in the query string. Default: escape().
    • decodeURIComponent [Function] The function to use when decoding percent-encoded characters in the query string. Default: unescape().
    • filter [Function] The function to use when filtering query.

For example:

append(null);
// ''

append('http://demo.com')
// 'http://demo.com'

append('http://demo.com', 123)
// 'http://demo.com'

append('http://demo.com', 'a=1&b=1&c=1')
// 'http://demo.com?a=1&b=1&c=1'

append('http://demo.com?test=1#hash', 'a=1&b=1&c=1')
// 'http://demo.com?test=1&a=1&b=1&c=1#hash'

append('http://demo.com', { a: 1, b: 1, c: 1 })
// 'http://demo.com?a=1&b=1&c=1'

append('http://demo.com?test=1#hash', { a: 1, b: 1, c: 1 })
// 'http://demo.com?test=1&a=1&b=1&c=1#hash'

append('http://demo.com', 'a=1&b=1&c=1&hideTopbar=1&hideSidebar=1', {
  filter(key) {
    return key !== 'hideTopbar' && key !== 'hideSidebar';
  }
})
// 'http://demo.com?a=1&b=1&c=1'

append('http://demo.com', { a: 1, b: 1, c: 1, hideTopbar: 1, hideSidebar: 1 }, {
  filter(key) {
    return key !== 'hideTopbar' && key !== 'hideSidebar';
  }
})
// 'http://demo.com?a=1&b=1&c=1'

decode()

The decode() method is an alias for parse().

encode()

The encode() method is an alias for stringify().

escape(str)

The escape() method performs URL percent-encoding on the given str in a manner that is optimized for the specific requirements of URL query strings.

The escape() method is used by stringify() and is generally not expected to be used directly. It is exported primarily to allow application code to provide a replacement percent-encoding implementation if necessary by assigning escape() to an alternative function.

parse(str[, sep[, eq[, options]]])

  • str <string> The URL query string to parse.
  • sep [string] The substring used to delimit key and value pairs in the query string. Default: '&'.
  • eq [string] The substring used to delimit keys and values in the query string. Default: '='.
  • options [object]
    • decodeURIComponent [Function] The function to use when decoding percent-encoded characters in the query string. Default: unescape().

For example:

parse(null)
// => {}

parse('12342343')
// => {}

parse('a=1&b=2&c=3&d=&f=')
// => { a: '1', b: '2', c: '3', d: '', f: '' }

parse('&a=1&b=2&c=3&d=&f=')
// => { a: '1', b: '2', c: '3', d: '', f: '' }

parse('abcd1234&a=1&b=2&c=3&d=&f=')
// => { a: '1', b: '2', c: '3', d: '', f: '' }

parse('?a=1&a=2&a=3&d=&f=')
// => { a: ['1', '2', '3'], d: '', f: '' }

parse('https://www.npmjs.com/search?q=qs#hash')
// => { q: 'qs' }

stringify(obj[, sep[, eq[, options]]])

  • obj <object> The object to serialize into a URL query string.
  • sep [string] The substring used to delimit key and value pairs in the query string. Default: '&'.
  • eq [string]. The substring used to delimit keys and values in the query string. Default: '='.
  • options [object]
    • encodeURIComponent [Function] The function to use when converting URL-unsafe characters to percent-encoding in the query string. Default: escape().

The stringify() method produces a URL query string from a given obj by iterating through the object's "own properties".

For example:

stringify(null)
// => {}
    
stringify({ a: 1, b: null, c: undefined, d: NaN, e: '' })
// => 'a=1&b=&c=&d=&e='

stringify({ a: '', c: ['\'1\'', '2', '3', NaN, undefined], f: null, '': 'null' })
// => 'a=&c=%271%27&c=2&c=3&c=&c=&f='

stringify({ a: { key: 'value', 'key2': 'value2' }, d: undefined, f: '' })
// => 'a=%7B%22key%22%3A%22value%22%2C%22key2%22%3A%22value2%22%7D&d=&f='

stringify({ a: () => { } })
// => 'a=%28%29%20%3D%3E%20%7B%7D'

unescape(str)

  • str <string> The unescape() method performs decoding of URL percent-encoded characters on the given str.

The unescape() method is used by parse() and is generally not expected to be used directly. It is exported primarily to allow application code to provide a replacement decoding implementation if necessary by assigning querystring.unescape to an alternative function.

By default, the unescape() method will attempt to use the JavaScript built-in decodeURIComponent() method to decode. If that fails, a safer equivalent that does not throw on malformed URLs will be used.

Benchmark

parse

【querystringify.parse】 x 70,472 ops/sec ±0.41% (93 runs sampled)

【query-string.parse】 x 40,702 ops/sec ±0.54% (95 runs sampled)

【qs.parse】 x 38,423 ops/sec ±0.20% (92 runs sampled)

【parse】 x 201,042 ops/sec ±0.32% (93 runs sampled)

The fastest is 【parse】

stringify

【querystringify.stringify】 x 47,602 ops/sec ±0.37% (92 runs sampled)

【query-string.stringify】: // what?

【qs.stringify】 x 75,845 ops/sec ±0.61% (91 runs sampled)

【stringify】 x 143,289 ops/sec ±0.50% (95 runs sampled)

The fastest is 【stringify】