GithubHelp home page GithubHelp logo

monkey-router's Introduction

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.

monkey-router's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

gdg

monkey-router's Issues

General improvements

  • Deprecate isEntering, replace with array of functions for setup and teardown. one function is fine
  • Remove internalState. useful but only for history api usage
  • Allow disabling of History API in browser, just like in Node. this is pretty necessary actually
  • Lisp rewrite :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.