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

monkey-router

v0.2.1

Published

Web application routing with tree traversal.

Downloads

4

Readme

Monkey Router

Build Status npm Version License

Monkey router is a tree-structured router that traverses between the current route and the route to transition to, and invokes each of the intermediate route handlers. It works equally well in web browsers and Node.js, and the browser version adds History API support.

$ npm i monkey-router --save

Usage

It accepts routes as an object, with wildcard routes defined as *:

var router = require('monkey-router')

var go = router({
  // route functions should handle setup and teardown.
  // they will be invoked with three arguments:
  // `isEntering`, `parameters`, and `state`.
  'products': function () { ... },
  'products/*': function () { ... },
  'products/*/pictures': function () { ... },
  'pictures/*': function () { ... },

  // empty string is the index route.
  '': function () { ... }
}/*, prefix */)

// go to the route, without leading or trailing slashes, which invokes
// all of the intermediate route handlers to get to that route.
go('products/123/pictures', { /* state */ })

The second argument to router prefix is an optional string that determines what goes in front of the path, without the domain. This will automatically be applied when transitioning to a route. It must not contain leading or trailing slashes.

The functions defined as the route handlers will be invoked with three arguments:

  • isEntering: a boolean value indicating whether it is a transition to (true) or away (false) from the route relative to the current route.
  • parameters: an array containing the wildcard routes in order of appearance.
  • state: history state which is passed as the second argument from the go function.

How It Works

A tree structure is constructed internally based on the routes given. On going to a route, it traverses between the current and future branches of the tree and invokes the function defined at that branch. For example, if the route is products/1/pictures and the future route is pictures, then the functions for products/*/pictures, products/*, products, and pictures will be invoked in that order.

Function scope is preserved. For example, these work:

var scope = { foo: 'bar' }
var go = router.call(scope, {
  'route': function () { console.log(this.foo) }
})

go('route') // logs 'bar'
go.call({ foo: 'baz' }, 'route') // logs 'baz'

If a missing route is traversed to, no functions will be invoked and an error will be thrown.

License

This software is licensed under the MIT License.