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

unquery

v0.4.3

Published

Build and control queries with confidence.

Downloads

61

Readme

Unquery 🍫

Version npm Dependencies Build Status codecov

Build and control query-strings with confidence.

It helps you to parse and stringify query-strings in a predictable way.

Instalation

To install, use:

Yarn:

yarn add --save unquery

Npm:

npm install --save unquery

Usage

Example 1 - Basic Usage

import { Unquery } from 'unquery'

const querySchema = Unquery('?foo=bar&baz=1', {
  foo: Unquery.string(),
  baz: Unquery.number()
})

console.log(querySchema)
{ "foo": "bar", "baz": 1 }

Example 2 - Different types usage

const querySchema = Unquery('?foo=str&bar=123&baz=1,2,3&date=2020-01-01&num=10&unnecessary=false' {
  foo: Unquery.string(),
  bar: Unquery.number(),
  baz: Unquery.array(Unquery.number()),
  date: Unquery.string(),
  num: Unquery.custom(value => Number(value) * 2)
}, { arrayFormat: "comma" })

console.log(querySchema)
{
  "foo": "str",
  "bar": 123,
  "baz": [ 1, 2, 3 ],
  "date": "2020-01-01",
  "num": 20
}

Example 3 - Stringify usage

import { Unquery, stringify } from "unquery"

const querySchema = Unquery('?foo=str&bar=123&baz=1,2,3&date=2020-01-01&unnecessary=false' {
  foo: Unquery.string(),
  bar: Unquery.number(),
  baz: Unquery.array(Unquery.number()),
  date: Unquery.array()
}, { arrayFormat: "comma" })

const stringified = stringify(querySchema, { arrayFormat: 'bracket' })
console.log(stringified)
"?foo=str&bar=123&baz[]=1&baz[]=2&baz[]=3&date[]=2020-01-01"

API

Unquery(input: string, schema: object, config: object)

@param input: string

Input to be parsed.

@param schema: object

Your query-string schema, that's gonna be parsed.

@param config: object

Options to customize the schema that's gonna be generated.

Options

  • arrayFormat
    • Description Array format to parse query-string. Use the same format used by query-string.
    • Type string
    • Default none
    • Values Each value will parse the following query-strings into an array:
    "bracket": "foo[]=1&foo[]=2&foo[]=3"
    "index": "foo[0]=1&foo[1]=2&foo[3]=3"
    "comma": "foo=1,2,3"
    "none": "foo=1&foo=2&foo=3"
  • skipNull
    • Description Skip null values to be parsed.
    • Type boolean
    • Default false
    Unquery('?value=123', {
      value: Unquery.number(),
      notInQueryValue: Unquery.string()
    }, { skipNull: false })
    
    // { value: 123, notInQueryValue: null }
    Unquery('?value=123', {
      value: Unquery.number(),
      notInQueryValue: Unquery.string()
    }, { skipNull: true })
    
    // { value: 123 }
  • skipUnknown
    • Description Skip unknown values to be parsed.
    • Type boolean
    • Default true
    Unquery('?value=foo&unknown=bar', {
      value: Unquery.string()
    }, { skipUnknown: false })
    
    // { value: "foo", unknown: "bar" }
    Unquery('?value=foo&unknown=bar', {
      value: Unquery.string()
    }, { skipUnknown: true })
    
    // { value: "foo" }

Unquery Methods

When you create an Unquery Object, your query will receive some super powers ⚡️!

const query = Unquery(...) // query is an Unquery Object

Global API

  • addLocationURL

    • Description Add query-string to URL. This keeps all current search.
    • Type (query: object | string, options: StringifyOptions) => void
    • Default null
    • Example
    import { Unquery, addLocationURL } from 'unquery'
    
    // https://yoursite.com/
    const querySchema = Unquery('?foo=bar&baz=42', {
      foo: Unquery.string(),
      baz: Unquery.number()
    })
    
    addLocationURL(querySchema)
    // https://yoursite.com?foo=bar&baz=42
    
    addLocationURL('date=2020-09-01')
    // https://yoursite.com?foo=bar&baz=42&date=2020-09-01
  • replaceLocationURL

    • Description Replace all URL search by the query-string.
    • Type (query: object | string, options: StringifyOptions) => void
    • Default null
    • Example
    import { Unquery, replaceLocationURL } from 'unquery'
    
    // https://yoursite.com/
    const querySchema = Unquery('?foo=bar&baz=42', {
      foo: Unquery.string(),
      baz: Unquery.number()
    })
    
    replaceLocationURL(querySchema)
    // https://yoursite.com?foo=bar&baz=42
    
    replaceLocationURL('date=2020-09-01')
    // https://yoursite.com?date=2020-09-01
  • clearLocationURL

    • Description Clear all query-string from URL without reload the page.
    • Type () => void
    • Default null
    • Example
    import { Unquery, clearLocationURL } from 'unquery'
    
    // https://yoursite.com/?foo=bar&baz=42
    const query = Unquery('?foo=bar&baz=42', {
      foo: Unquery.string(),
      baz: Unquery.number()
    })
    
    clearLocationURL()
    // https://yoursite.com/
  • stringify

    • Description Stringify an object into a query string.
    • Type (queryObject: UnqueryObject, options: StringifyOptions) => string
    • Default
    {
      // You can set your unqueryOptions by calling setOptions(options)
      arrayFormat: unqueryOptions.arrayFormat // default: 'none'
    }
    • Examples
    import { stringify } from 'unquery'
    
    const stringified = stringify({ startDate: '2020-01-01', viewId: 4 })
    // "startDate=2020-01-01&viewId=4"
  • setOptions

    • Description Set default options to use in your entire app.
    • Type (options: UnqueryOptions) => UnqueryOptions
    • Examples
    import { Unquery, stringify, setOptions } from 'unquery'
    
    setOptions({
      arrayFormat: 'comma',
      skipNull: true,
      skipUnknown: false
    })
    
    const query = Unquery('?date=2020-02-01&foo=1,2,3', {
      date: Unquery.string()
    })
    // { date: "2020-02-01", foo: ['1','2','3'] }
    
    const stringified = stringify(Unquery, { arrayFormat: 'index' })
    // 'data=01/02/2020&foo[0]=1&foo[1]=2&foo[2]=3