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

hash-brown-router

v3.4.1

Published

A client-side router that only cares about the bits after the #

Downloads

927

Readme

hash-brown-router

A router that is only concerned with single-page apps that want to change state based on the bits of the url after the hash.

Want to use this router, but with go translated to pushState? Use this library with sausage-router!

Why another client-side routing library?

This library:

  1. uses a path-parsing library that lets you generate links programmatically
  2. comes with a handy stub for testing - any library that takes hash-brown-router can use the included stub in unit tests.

Example

var router = require('hash-brown-router')()

var mainElement = document.getElementById('main')

router.add('/', function() {
	mainElement.innerHTML = 'Thank you for visiting my site! This is the home screen.'
})

router.add('/blog-post/:blogPostName', function (parameters) {
	mainElement.innerHTML = getHtmlForBlogPost(parameters.blogPostName)
})

router.on('not found', function (path, querystringParameters) {
	alert('Couldn\'t find that page, sorry! Redirecting you to the home screen.')
	router.location.replace('/')
})

router.evaluateCurrent('/')

API

Construction

var makeRouter = require('hash-brown-router')

var router = makeRouter(options, [location object])
  • options: an object of options
    • reverse: By default, routes are matched from oldest to newest. So if there are multiple matching routes for the current url, the first one that was added is used. If reverse is set to true, then the most recently added match is used.
  • location, an abstraction of the browser's location matching this interface

The router is an event emitter that emits:

  • not found: whenever the route is evaluated and there is no matching handler for that route. It is passed two arguments: the path (a string) and the querystring parameters (an object).

router.add(routeString, cb) - add routes

router.add('/page/:pageName', function(parameters) {
	console.log(parameters.pageName)
})

Parses express-style route paths, using a fork of path-to-regexp.

router.location.go(newPath) - navigate to a new path

router.location.go('/some-other/path')

Changes the current location hash.

router.location.replace(newPath) - replace the current route in the browser history

router.add('/page/:pageName', function(parameters) {
	if (doesNotExistInTheDatabase(parameters.pageName)) {
		router.location.replace('/pageNotFound')
	}
})

Changes the current location hash, replacing the last location in the browser history, i.e. location.replace(location.origin + location.pathname + '#' + newPath).

router.location.get() - get the current path, without a leading hash

router.location.get() // => '/page/home'

router.evaluateCurrent(defaultPath) - evaluate the current url

Forces the library to evaluate the current route from location.hash. Probably best do do once the dom is ready.

router.evaluateCurrent('/home')

If location.hash is currently empty, it changes the path to the default path value you pass in.

router.stop()

If for some reason you want the router to start ignoring hash change events. you can call router.stop().

Testability

Want to use a stub of this library that works in node? Just require('hash-brown-router/mock') for all your automated testing needs.

Browser support

Build Status

Automated testing in Chrome, Firefox, Safari, and IE9+ provided by Browserstack.

License

WTFPL