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

local-router

v2.0.0

Published

A small, hash-based router for the browser

Downloads

10

Readme

local-router

A small, hash-based router for the browser.

Note: Due to an issue in prettydiff, the minified ES6 version of the code resides in a directory. The file you want to use is dist/router.es6.min.js/dist/router.es6.js instead of dist/router.es6.min.js. This issue has been fixed in prettydiff, but hasn't been published to NPM yet.
I have a new, leaner method queued (using escompress, which relies on Babel), but due to an issue in that module (#6), this code fails to minify.

Files

The source code resides in router.js. The build system provides some transformed versions in the dist directory. All these files are wrapped in the UMD module format.
Here's a detailed overview of the different files:

Installation

You can either download the files from GitHub, or use NPM instead:

npm install local-router

Then you can either include local-router in your Browserify or RequireJS bundle, or load it directly in your HTML page, with a <script> tag.

Usage

This module exports a single class, Router. Create an instance of this class, passing in two objects: routes (required) and options (optional).

Supported options are:

routes

Required, object(string -> boolean).
An object mapping route names (e.g. about, result) to booleans, indicating whether that route accepts any arguments, or objects with encode and/or decode methods. The encode method will be passed the data passed to Router.go() and is expected to return a stringified version of it. The decode method will be passed the data string from the URL and is expected to return the data it contains. If either one is not specified (or you pass true), the default encode/decode methods will be used instead (respectively)

options.index

String.
A string representing the default route. If an invalid route is browsed to, the browser will be redirected to this route. If left out, index will be used.

options.prefix

String.
A string that is used to prefix the class names added to the document body, e.g. browsing to #index will add the page-index class to the body. The default is page-.

options.elem

String or Element.
The HTML element or id or CSS selector of the HTMl element to add the classes to. Default is the body.

options.separator

String.
The string separating the name of the route and the data in the URL. Default: -.

Example

Here is an example of how to make a new Router:

const router = new Router({
  input: false,
  result: {
    encode: function(data) {return data;},
    decode: function(str) {return str;}
  }
}, {
  index: "input",
  prefix: "mode-"
});

This router would accepts routes like: #input, #result-abcdef, #result-, #result-Tuur_Dutoit... If an invalid route is browsed to (e.g. #non-existent, or just /), the browser is redirected to #input. The class names added to the body will be: mode-input or mode-result, depending on the current route, of course.

Router.on(string route, function callback)

Adds a callback to the router. When the user browses to #route (substituting route with the route argument, of course), this callback will be called. If the route accepts a data string, the decoded data will be passed in the first argument.
Here's an example:

router.on("result", data => {
  console.log(`result: ${data}`);
});

If the user then browses to #result-hello, the string result: hello will be logged.

Router.off(string route, function callback)

Removes the callback from this router, i.e. does the opposite of router.on.

Router.go(string route, [any data])

Redirect the browser to a route. If this route accepts a data string, the encoded data string will be added after the separator.
Here are a few examples:

router.go("input");
router.go("result", "hello");
router.go("result");

The browser will be redirected to #input, #result-hello and #result- respectively. In the first and last examples, no data will be passed to the callbacks.