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

anyroute

v1.3.4

Published

A flexable lightweight router you can use in nodejs and browser. No dependency.

Downloads

119

Readme

Build Status Quality Gate Status Open Source Helpers FOSSA Status

npm version install size npm downloads GitHub license

anyroute - lightweight router works anywhere

A flexible lightweight router you can use in nodejs and browser. No dependency.

INSTALL

npm i anyroute

Or find help from:

  • https://www.npmjs.com/package/anyroute
  • https://github.com/BlueT/anyroute.js

SYNOPSIS

const {Anyroute, MatchResult} = require('anyroute');
const anyroute = new Anyroute;

anyroute.set('/happy/:foo/and/:bar', (params) => { console.log("Happy " + params.foo + " and " + params.bar ); return params.foo + params.bar; });

let foobar = anyroute.get('/happy/trees/and/kitties').run();
// Happy trees and kitties
// foobar: treeskitties

anyroute.set('/:aaa/:bbb', (match) => {return match;})
        .get('/doraemon/superman')
        .run({'c':'c'}, (result) => console.log(result));
// { 'aaa': 'doraemon', 'bbb': 'superman', 'c': 'c' }

anyroute.notfound(function (matchResult) {
        // call when NO exact match found
        // matchResult is an MatchResult Object
        return matchResult.payload.foo + matchResult.payload.and;
});

Setter

Define a route and placeholder. Returns error (if any), the handler been set, and an empty payload.

function handler () {};
function handler_post () {};

anyroute.set('/collection/:cid/tab/:tabID', handler);
// If no feat (a tag) has been set, means 'default'.

var ret = anyroute.set('/collection/:cid/tab/:tabID', handler, 'default');
// When a handler of a feat has previously been set,
// it'll overwrite with the new, and return a message in err.
// Returns modified anyroute itself, too.

var ret = anyroute.set('/collection/:cid/tab/:tabID/', handler_post, 'post')
// feat can be anything, just like a tag

Getter

var ret = anyroute.get('/collection/123/tab/456');
// If no feat (a tag) has been set, means 'default'.
// Returns MatchResult object

var ret = anyroute.get('/collection/ccc/tab/ttt', {user: 'keroro'});
// You can put user's payload, and they'll be merged into one in return.
// Custom payload with the same name will be override by get().
// ret.payload: { user: 'keroro', cid: 'ccc', tabID: 'ttt' }

var ret = anyroute.get('/collection/foo/tab/bar', {cid: 'admin'}, 'all');
// Also the 'all' means return all handlers from all feats.
// ret.handler: 
//    { default: [Function: handler],
//      post: [Function: handler_post] }

var ret = anyroute.get('/collection/abc/tab/xyz', {}, 'head');
// Getting handler of a feat you've never set before,
// will return the default handler, with a error message.
// So you can have the fallback if you want.

run() shortcut

Call with run([object?: payload, function?: callback])

run() is a shortcut of MatchResult.handler( MatchResult.payload ).

run(callback) is a shortcut of callback( MatchResult.run() );.

Also can do run(additionalParams) and run(additionalParams, callback).

var result = ar.get("/collection/:cid/tab/:tabID").run();

var ret = ar.get("/collection/:cid/tab/:tabID", {}, "default");
let returnedByHandler = ret.run(req.body.data);
// also can have custom payload here as first argument
// similar to ret.handler(ret.payload)

let cid = ret.run({foo: bar}, (x) => x.cid);
let tab = ret.run((x) => x.tabID);
// additional processing on data returned by pre-set handler

Before calling run(), you can set .notfound(handleNotFound) handler, which will be called if err occurred (path not found).

Function handleNotFound will be called with MatchResult as input parameter. handleNotFound(MatchResult).

// setup `notfound` handler
anyroute.notfound(function (matchResult) {
        // call when NO exact match found
        // matchResult is an MatchResult Object
        return matchResult.payload.foo + matchResult.payload.bar;
});

MatchResult

MatchResult {
  err: undefined,
  handler: [Function: handler],
  payload: { foo: 'forty', bar: 'bobs', and: 'adam' },
  default: undefined }

License

FOSSA Status