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

susanin

v1.0.3

Published

Router system which can be used in any JavaScript environments

Downloads

2,241

Readme

Susanin NPM version Bower version Build status

devDependency status

Susanin is a routing library which can be used in any JavaScript environments.

Demo

Getting Started

  • susanin.js - uncompressed source code with comments
  • susanin.min.js - compressed code

In the browsers just include the file in your document:

<script src="/path/to/susanin.min.js"></script>

You can install Susanin on Node.js using NPM:

npm install susanin

Examples

  • The simplest:
var route = Susanin.Route('/products');

console.log(route.match('/produc'));                  // => null 
console.log(route.match('/products'));                // => {} 
  • More complex, with a param in pattern:
var route = Susanin.Route('/products/<id>');

console.log(route.match('/products'));               // => null
console.log(route.match('/products/321'));           // => { id : '321' }
console.log(route.match('/products/321?id=123'));    // => { id : '321' }
  • With query params in path:
var route = Susanin.Route('/products');
 
console.log(route.match('/products?id=321&category=shoes&category=new'));   
// => { id : '321', category : [ 'shoes', 'new' ] } 
  • With an optional group:
var route = Susanin.Route('/products(/<id>)'));

console.log(route.match('/products'));           // => {}
console.log(route.match('/products/321'));       // => { id : '321' }
  • With a default value of param:
var route = Susanin.Route({ 
    pattern : '/products(/<id>)',
    defaults : {
        id : '123'
    }
});

console.log(route.match('/products'));           // => { id : '123' }
console.log(route.match('/products/321'));       // => { id : '321' }
  • If you want to specify a regexp for param:
var route = Susanin.Route({ 
    pattern : '/products(/<id>)',
    defaults : {
        id : '123'
    },
    conditions : {
        id : '\\d{3,4}'
    }
});

console.log(route.match('/products'));           // => { id : '123' }
console.log(route.match('/products/321'));       // => { id : '321' }
console.log(route.match('/products/a321'));      // => null
console.log(route.match('/products/32'));        // => null
  • The most complex:
var route = Susanin.Route({ 
    pattern : '/products(/<category>(/<id>))(/)',
    defaults : {
        category : 'shoes',
        id : '123'
    },
    conditions : {
        category : [ 'shoes', 'jeans', 'shirt' ],
        id : '\\d{3,4}'
    }
});

console.log(route.match('/prod'));               // => null
console.log(route.match('/products'));           // => { category : 'shoes', id : '123' }
console.log(route.match('/products/'));          // => { category : 'shoes', id : '123' }
console.log(route.match('/products/jeans'));     // => { category : 'jeans', id : '123' }
console.log(route.match('/products/skirt'));     // => null
console.log(route.match('/products/shirt/321')); // => { category : 'shirt', id : '321' }
console.log(route.match('/products/shirt/32'));  // => null
console.log(route.match('/products/shoes/'));    // => { category : 'shoes', id : '123' }
  • You can bind any data with a route and match on these:
var route = Susanin.Route({ 
    pattern : '/products/<category>',
    data : {
        method : 'GET',
        controller : 'products'
    }
});

console.log(route.getData());                     // => { method : 'GET', controller : 'products' }
console.log(route.match({ method : 'POST' });     // => null
console.log(route.match({ method : 'GET' });      // => {}
console.log(route.match({ 
    path : '/products/shoes', 
    method : 'GET' 
});                                               // => { category : 'shoes' }
  • Set of routes:
var susanin = Susanin();

susanin.addRoute('/contacts');
susanin.addRoute('/products/<category>');
susanin.addRoute({
    pattern : '/(<controller>(/<action>(/<id>)))',
    defaults : {
        controller : 'index',
        action : 'build'
    }
});

console.log(susanin.findFirst('/'));                  // => [ #route, { controller : 'index', action : 'build' } ]
console.log(susanin.findFirst('/products'));          // => [ #route, { controller : 'products', action : 'build' } ]
console.log(susanin.findFirst('/products/shoes'));    // => [ #route, { category : 'shoes' } ]
  • Susanin can build a path:
var route = Susanin.Route({ 
    pattern : '/products(/cat_<category>)(/)',
    defaults : { category : 'shoes' }
});

console.log(route.build());                           // => '/products'
console.log(route.build({ category : 'jeans' }));     // => '/products/cat_jeans'
console.log(route.build({ category : 'shoes' }));     // => '/products'