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

ko-querystring

v2.0.3

Published

Query(string) Abstraction for KnockoutJS

Downloads

14

Readme

ko-querystring

NPM Version WTFPL Travis Coverage Status Dependency Status Peer Dependency Status NPM Downloads

Easy-peasy Querystrings for Knockout

Installation

$ npm install -S ko-querystring

Basic Usage

import Query from 'ko-querystring'

const query = new Query({ sort: 'alpha' })

query.sort() // alpha

API

new Query([config = {}, name])

Create a new query object using the new keyword, and pass a configuration object, and optionally a name to group the query with. This allows you to create multiple query objects with the same params, and they will not interfere with each other. It also allows you to link queries if they are given the same group name.

The configuration object contains key/value pairs of querystring param names and their config, respectively. A querystring param config may be an object that contains any combination of three props, default, initial, and coerce, or a value which will be used as the default and initial value. The coerce function allows you to transform a value before it is fully set.

const query = new Query({
  // query param named foo
  foo: {
    default: 'foo',
    initial: 'bar',
    coerce: (v) => v === 'baz' ? 'qux' : v
  },

  bar: 'bar'
})

In this case, the foo param will be set to "bar" initially — if not already in the querystring — but call to query.clear() will then set it to "foo". The coerce function disallows setting the param to "baz", and attempting to will cause it to be set to "qux" instead.

NOTE: Params that are equal to their default will not be displayed in the querystring. Less === More.

Query.fromQS([group])

Returns JS object containing current query from URL — for group if any.

Query.setParser({ parse, stringify })

By default, this lib is dumb, and it does not use valid querystrings. Instead, it uses JSON.stringify and encodeURIComponent, and vice versa. This function allows you to define a custom parser.

e.g.

import Query from 'ko-querystring'
import rison from 'rison' // https://github.com/Nanonid/rison

Query.setParser({
  parse: rison.decode_object,
  stringify: rison.encode_object
})

query[param]

Query params are created via super-cool ES6 proxies, so you don't need to explicitly define all the query params you will use. Simply access them, and they are there.

Params will be initialized from the querystring if available, and their default value or undefined if not.

query[param].clear()

Resets param to its default or undefined.

query[param].isDefault

Observable value that is true if the param is its default value, otherwise false.

query.set(default || { default, initial, coerce })

Change the default values for a query.

query.clear()

Reset all the query params to their defined defaults, or undefined.

query.toJS()

Returns unwrapped query object.

query.toString()

Returns stringified query.

query.asObservable()

Return observable query object. i.e. ko.observable({ foo: 'foo' }) instead of { foo: ko.observable('foo') }

query.dispose()

Disposes the query object and cleans the querystring. Don't forget to clean up after 'yo self.

MOAR!

Check the test file or the source. This lib is small enough to understand inside and out.