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

pram

v2.0.0-alpha.0

Published

A small library to help reduce boilerplate code when dealing with storing state in a url's query params.

Downloads

40

Readme

Pram

Pram Logo

A small library to help reduce boilerplate code when dealing with storing state in a url's query params.

Installation

npm install pram

API Documentation

Pram

You can access all of Pram's utilities from a Pram instance. You must provide a history object when creating the instance. If you use React Router you may be familiar with this concept, but if not here's a little snippet that gives you an idea of how to get up and running:


import createHistory from 'history/createBrowserHistory'
import Pram from 'pram'

const history = createHistory()
const pram = new Pram(history)

Methods

All of these utilities can be accessed from an instance of Pram.

getParams()

() => any

This will return you a parsed object of your current url params, according to the parse function from the qs library. For example, if your current query string is ?name=Daniel&country=UK then this function will return to you { name: 'Daniel', country: 'UK' }.

getParam(param)

(param: string) => any

This will return you the value of param from the current url params. This is the same as calling getParams()[param].

pushParams(params)

(params: {}) => void

This will push the params that you supply into the url. This is a push operation, so will create a new entry in history (the user can hit the back button and this action will be undone). The params object you pass will be encoded using the qs library's stringify function, so refer to those docs for more detailed information.

replaceParams(params)

(params: {}) => void

This is the same as pushParams detailed above, except the new query params will replace the old one, and the back button will not undo the action.

pushParam(key, value)

(key: string, value: any) => void

This will push a specific value onto your query params at the given key. This is a push operation, so will create a new entry in history (the user can hit the back button and this action will be undone). The params object you pass will be encoded using the qs library's stringify function, so refer to those docs for more detailed information.

replaceParam(key, value)

(key: string, value: any) => void

This is the same as pushParam detailed above, except the new query param will replace the old one, and the back button will not undo the action.

onChange(callback)

(callback: (value: any)) => UnregisterCallback

onChange(key, callback)

(key: string, callback: (value: any)) => UnregisterCallback

You can use this to listen for changes in query params. You can either call it with one argument; a callback function - or you can use two arguments; a query param key to filter on and a callback. If you don't provide a filter, your callback will be passed all the params. If you filter, then you will only be passed the value of the key you requested.

An example of listening to all changes:


const pram = new Pram(history)

pram.onChange(params => console.log(params))

// log: { foo: 'bar', name: 'Daniel' }

pram.onChange('name', value => console.log(value))

// log: 'Daniel'

This utility will return you a callback to unregister your callback. For instance; if you are using React and you start listening when a component mounts, then you should remember to always 'unlisten':


class View extends React.Component {
  componentDidMount() {
    this.stopListening = pram.onChange('name', console.log)
  }

  componentWillUnmount() {
    this.stopListening()
  }
}
when(key, value, callback)

(key: string, value: any, callback: (value: any) => void) => UnregisterCallback

when(predicate, callback)

(predicate: (params: {}) => boolean, callback: (params: any) => void) => UnregisterCallback

This can be used when you want a callback to fire every time a condition is met in the query params. Note, unlike onChange, when using when your callback will be fired immediately if the condition is met; not just when query params change.

Example:


const pram = new Pram(history)

// this will check that the param 'name' is exactly equals to 'Daniel'
pram.when('name', 'Daniel', () => console.log('Hi, Daniel!'))

// or use your own custom predicate function
pram.when(params => params.name === 'Daniel', () => console.log('Hi, Daniel!'))

Usage with React

React specific methods require a second import from the react submodule, as well as the peer dependency of react itself.

e.g. import { connectParams } from 'pram/react'

connectParams(pram, options)

(pram: Pram, options?: { propName?: string }) => (Component: React.ComponentType) => React.ComponentType

This is higher-order function that returns a higher-order component. Think of it like the connect function from redux, or a function that creates something similar to withRouter from react-router.

You call it with an instance of Pram and some options if you want, and it will return you something that you can compose with other HOCs to enhance your component. In this case, it will provide your component the current query params as a prop, which by default is named 'params'.

Options:

propName default: 'params'

This is the name of the prop that your component will be given.

Example:


import createHistory from 'history/createBrowserHistory'
import Pram from 'pram'
import { connectParams } from 'pram/react'

const history = createHistory()
const pram = new Pram(history)
const withParams = connectParams(pram)

const View = (props) => (
  <div>
    Hello {props.params.name}!
  </div>
)

export default withParams(View)