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

mini-routerjs

v1.1.5

Published

A basic router concentrating on route parsing and callback registration.

Downloads

8

Readme

mini-routerjs

A very basic router with functionalities of parsing route and registering callbacks for browser and nodejs.

Installing

Use via npm:

npm install mini-routerjs
var Router = require('mini-routerjs');

// Use es6 import
import Router from 'mini-routerjs';

Use in browser:

Scripts for browser is under build directory, use router.js for development (contains inline source maps) or use router.min.js for production. The reference in browser is window.MiniRouter.

Examples

Create a common route:

var Router = require('mini-routerjs'),
    router = new Router();

router.create('/foo/:name/bar', function(request){
  console.log(request);
});

router.match('http://hostname/foo/jimmy/bar', 'some data');
/**
* Console output:
*{ url: 
*   { protocol: 'http',
*     auth: '',
*     host: 'hostname',
*     pathname: '/foo/jimmy/bar',
*     query: {},
*     hash: '' },
*  params: { name: 'jimmy' },
*  data: 'some data' }
*/

router.match('/foo/jimmy/bar?foo=bar', 'some data');
/**
* Console output:
*{ url: 
*   { protocol: '',
*     auth: '',
*     host: '',
*     pathname: '/foo/jimmy/bar',
*     query: {foo: 'bar'},
*     hash: '' },
*  params: { name: 'jimmy' },
*  data: 'some data' }
*/

Create a mismatch route:

var router = require('mini-routerjs'),
  router = new Router();
  
router.createMismatch(function(request){
  console.log(request);
});

router.match('/no-match', 'some data');
/**
* Console output:
*{ url: 
*   { protocol: '',
*     auth: '',
*     host: '',
*     pathname: '/no-match',
*     query: {},
*     hash: '' },
*  params: null,
*  data: 'some data' }
*/

Constructor and methods

Router(options)

| Option | Type | Description | | --- | --- | --- | | strict | Boolean | Enable strict routing. false by default, '/foo' and '/foo/' are treated the same by the router. | | parseQuery | Boolean | Whether parse the query string to object, default true. |

Router.create(route, callback, strict)

| Params | Type | Description | | --- | --- | --- | | route | String | The route pattern. | | callback | Function | The callback which will be called when the route is matched, default function(){} | | strict | Boolean | This will override the constructor's strict option. |

When a route is matched, the corresponding callback will be passed a request param and executed. The request param has a structure like:

{
  url: url, // A parsed url object which matchs this route.
  params: params, // The params extracted from url.
  data: data // The data passed by match method.
}

Url is parsed by simple-url.

The route support following patterns:

  • * will match any none '/' characters no greedy, the matched part will be indexed as the order in all * and **.
  • ** will match any characters, the matched part will be indexed as the order of in all * and **.
  • :name will match a part of none '/' characters, the matched part will be indexed as 'name'.
  • () will make the part between it optional.

| Route | url | params | | --- | --- | --- | | /foo/*.gif | /foo/hello.gif | {'0': 'hello'} | | /foo/** | /foo/bar/hello | {'0': 'bar/hello'} | | /foo/**/*.gif | /foo/bar/hello.gif | {'0': 'bar', '1': 'hello'} | | /foo/:bar/hello | /foo/bar/hello | {'bar': 'bar'} | | /foo/:bar/*.gif | /foo/bar/hello.gif | {'bar': 'bar', '0': 'hello'} | | /foo/(:bar/hello) | /foo/bar/hello | {'bar': 'bar'} | | /foo/(:bar/hello) | /foo/ | {'bar': undefined} |

Router.match(url, data)

Do a match with url and pass data to callback.

Router.createMissmatch(callback);

Create a missmatch, the callback will be called with a request param when any match is failed.

License

MIT