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

browser-router

v0.2.0

Published

A simple router for one page browser apps

Downloads

2,496

Readme

browser-router

A simple router for one page browser apps.

WHY

This is an effort of abstracting the routing such that applications can utilize a similar router on the client as on the server. The server counterpart is yet to be built.

INSTALL

npm install --save browser-router

USE

> var router = require('browser-router');

API

route(path, params, title)

> router.route('/login', { next: '/secret' }, 'Log In');
undefined

Make the router execute a route. This does a browser pushState to change the URL and params, while keeping the client app running.

onRoute(handler)

  router.onRoute(function myHandler(route, params, title) {
  	// handle this change in browser state
  });

Catch an incoming route from the browser. This happens when:

  • The user changes the route
  • Browser back button, or back()
  • Browser forward button, or forward()
  • route()
  • setPath()
  • setParams(), setParam(), removeParam(), toggleParam()

Changes in the title do not cause routes. It is provided to the route handler as a convenience.

offRoute(handler)

> router.offRoute(myHandler);
undefined

Remove a handler that was listening to the router via onRoute.

getPath()

> router.getPath();
'/login'

Grab the current path. Excludes search query and hashstring parameters.

setPath()

> router.setPath('/login/securely');
undefined

Change the current path, causing a route.

getTitle()

> router.getTitle();
'Log In'

Get the title of the current location.

setTitle()

> router.setTitle('Secure Log In');
undefined

Set the title of the current location. This alters the current spot in history and does not cause a route.

getParams()

> router.getParams();
{ next: '/secret' }

Grab the current parameters object from the search query.

getParam(paramName)

> router.getParam('next');
'/secret'

Grab a single search query string parameter.

setParams(params)

> router.setParam({ next: '/secret/extra_secret', rememberMe: null });
undefined

Set a new params object, replacing the query string, causing a route.

This example would route to ?next=/secret/extra_secret&rememberMe

When the values are null, the param will be present but have no value, like the rememberMe param in the example.

hasParam(paramName)

> router.hasParam('rememberMe')
true

Checks for the presence of an item in the query string.

Useful for checking flags in the query, IE when the query string is ?next=/secret&rememberMe

setParam(paramName [, value])

> router.setParam('securityLevel', '3')
undefined

Set a single parameter in the query string without wiping out the rest of the params. Causes a route.

This example would route to ?next=/secret/extra_secret&rememberMe&securityLevel=3

When the value is missing or null, the param will be present but have no value, like the rememberMe param in the example above.

removeParam(paramName)

> router.removeParam('securityLevel')
undefined

This example would route to ?next=/secret/extra_secret&rememberMe

Removes a parameter from the search query string. Mostly the inverse of setParam.

toggleParam(paramName [, value])

> router.toggleParam('rememberMe')
undefined

This example would route to ?next=/secret/extra_secret

Toggles the presence of a param in the query string. If setting the param, use the provided value.