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

@billjs/query-string

v1.0.2

Published

A simple and lightweight QueryString by TypeScript for Node.js or Browsers.

Downloads

591

Readme

query-string Travis-ci Status GitHub license typescript | javascript | node.js Npm Version

A simple and lightweight QueryString by TypeScript for Node.js or Browsers.

Installation

Installation is done using the npm install command:

npm install -S @billjs/query-string

Overview

API

parse

Parse the string to an object. Support the parsing of multiple keys with the same name, and parse them into arrays. The parsed keys and values are decoded by decodeURIComponent function. If you specified the fn argument, that means you can to use it for customize value.

  • str (string) source string
  • [sep] (string | null optional) group separator, default &
  • [eq] (string | null optional) key-value separator, default =
  • [fn] (ParseFunction) a function, it can be used to customize return values.
  • return (object)

Parse a normal query-string.

const data = parse('a=1&b=s');
console.log(data); // ==> { a: '1', b: 's' }

Parse the search from URL.

const data = parse('http://foo.com?a=1&b=s');
console.log(data); // ==> { a: '1', b: 's' }

Parse it when if key or value is encoded.

const data = parse('test%3D=test%20%26*%20test');
console.log(data); // ==> { 'test=': 'test &* test' }

Parse it when if sep and eq are specified.

const data = parse('a#1|b#s|b#s2', '|', '#');
console.log(data); // ==> { a: '1', b: ['s', 's2'] }

Parse the same name key as an array.

const data = parse('a=1&b=s1&b=s2');
console.log(data); // ==> { a: '1', b: ['s1', 's2'] }

Parse it for customize value.

const fn = (key: string, value: string) => {
  if (key === 'b') return +value;
  if (key === 'c') return { on: true, off: false }[value];
  return value;
};
const data = parse('a=test&b=1&c=on', null, null, fn);
console.log(data); // ==> { a: 'test', b: 1, c: true }

stringify

Stringify the object to a string. Support the stringifying of arrays into keys of the same name. The stringified keys and values are encoded by encodeURIComponent function. If you specified the fn argument, that means you can to use it for customize value.

  • obj (object) source object
  • [sep] (string | null optional) group separator, default &
  • [eq] (string | null optional) key-value separator, default =
  • [fn] (StringifyFunction) a function, it can be used to customize return values.
  • return (string)

Stringify an object.

const query = stringify({ a: '1', b: 's' });
console.log(query); // ==> 'a=1&b=s'

Stringify the array to same name key.

const query = stringify({ a: '1', b: ['s1', 's2'] });
console.log(query); // ==> 'a=1&b=s1&b=s2'

Stringify the boolean value.

const query = stringify({ a: '1', b: 's', c: false });
console.log(query); // ==> 'a=1&b=s&c=false'

Stringify it when if the key or value need to encoded.

const query = stringify({ 'test=': 'test &* test' });
console.log(query); // ==> 'test%3D=test%20%26*%20test'

Stringify it when if sep and eq are specified.

const query = stringify({ a: '1', b: ['s', 's2'] }, '|', '#');
console.log(query); // ==> 'a#1|b#s|b#s2'

Stringify it for customize value.

const fn = (key, value) => {
  if (key === 'c') return value ? 'on' : 'off';
  return value;
};
const obj = { a: 'test', b: 1, c: true };
const query = stringify(obj, null, null, fn);
console.log(query); // ==> 'a=test&b=1&c=on'

ParseFunction

A function for customize parse.

The declaration is like this:

type ParseFunction = (key: string, value: string) => any;

StringifyFunction

A function for customize stringify.

The declaration is like this:

type StringifyFunction = (key: string, value: any) => any;

License

MIT License