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

bloody-router

v0.2.0

Published

[![browser support](https://ci.testling.com/bloodyowl/router.png)](https://ci.testling.com/bloodyowl/router)

Downloads

10

Readme

router

browser support

Install

$ npm install bloody-router

Require

var router = require("bloody-router")

Definition

The router class lets you bind callbacks to string patterns. Its instances contain an .update method to pass strings and make the router check them to execute the matching callback. To make the parsing easier, parsers are available, and extensible.

Methods

router.create() -> router instance

Creates a new class that inherits from router.

router.destroy()

Empties all of router's routes.

router.define(pattern, callback)

Listens to the given pattern and binds callback to it. (see patterns).

router.remove(pattern)

Stops listening to the given pattern.

router.defineParser(name, object)

Creates a custom parser (see parsers). You cannot overwrite a core parser (number, string or boolean)

router.update(string[, forceUpdate=false])

Compares string to the patterns, and asynchronously executes its bound callback. If forceUpdate is true, callback is executed even if there is no change between the actual state and string.

Patterns

Patterns use the given syntax :

  • definition blocks are delimited by { and }
  • they own two parameters, parser:name
  • parser: is optionnal
  • you cannot use twice the same name for a value within the same pattern
"page/{anyString}"
// -> callback gets `[anyString]` as arguments 
"page/{string:name}"
// -> callback gets `[name]` as arguments 
"page/{number:n}/{string:s}"
// -> callback gets `[n, s]` as arguments 
"validate/{boolean:bool}"
// -> callback gets `[bool]` as arguments 

Parsers

Parsers are objects containing a regexp regular expression for matching and a parser method to convert the regexp results. The regular expression must only contain one capturing group. If you need to decompose its contents, you'll need to export it within the parser method. The result of the regexp match is passed as first parser argument when executed. Then, the return value of parser is passed to the callback.

myRouter.defineParser("date", {
  regexp : /(\d{4}\-\d{2}\-\d{2})/,
  parser : function(date){
    return new Date(date).toGMTString()
  }
})

Usage

var appRouter = router.create()

myRouter.defineParser("date", {
  regexp : /(\d{4}\-\d{2}\-\d{2})/,
  parser : function(date){
    return new Date(date).toGMTString()
  }
})

myRouter.define("/", function(){
  // init home view
})

myRouter.define("/post/{number:id}", function(id){
  typeof id // "number"
  // grab post and init view
})

listen(window, "hashchange", function(){
  myRouter.update(location.hash.slice(1))
})

// to force update somewhere else

myRouter.update("url", true)