lib/index.js
function(patterns, options) {
	var self = this;
	if (!(typeof patterns=='object')){throw new Error('ObsRouter constructor expects patterns object as first argument')}
	options = options || {};
	EventEmitter.call(self);
	if (process.browser){
		//bind to window by default
		self._bindToWindow = 'bindToWindow' in options ? options.bindToWindow : true;
		if (self._bindToWindow){
			// add to module-scoped list of routers
			routers.push(self);
			// override any url input with window location
			options.url = window.document.location.pathname + window.document.location.search;
		}
	}
	// initialise state
	self.patterns = patterns;
	self.name = null;
	self.params = {};
	self.routes = {};
	_.forEach(_.keys(self.patterns), function(route){
		self.routes[route] = null;
	});
	// normalise state
	self._update(options.url || '', false);
	// implement initialEmit option
	if (options.initialEmit){
		var cancel = cancellableNextTick(function(){
			self._emit();
		});
		self.once('url', cancel);
	}
}
function(patterns, options) {
	var self = this;
	if (!(typeof patterns=='object')){throw new Error('ObsRouter constructor expects patterns object as first argument')}
	options = options || {};
	EventEmitter.call(self);
	if (process.browser){
		//bind to window by default
		self._bindToWindow = 'bindToWindow' in options ? options.bindToWindow : true;
		if (self._bindToWindow){
			// add to module-scoped list of routers
			routers.push(self);
			// override any url input with window location
			options.url = window.document.location.pathname + window.document.location.search;
		}
	}
	// initialise state
	self.patterns = patterns;
	self.name = null;
	self.params = {};
	self.routes = {};
	_.forEach(_.keys(self.patterns), function(route){
		self.routes[route] = null;
	});
	// normalise state
	self._update(options.url || '', false);
	// implement initialEmit option
	if (options.initialEmit){
		var cancel = cancellableNextTick(function(){
			self._emit();
		});
		self.once('url', cancel);
	}
}
function ( patterns , options ) { var self = this ; if ( ! ( typeof patterns == 'object' ) ) { throw new Error ( 'ObsRouter constructor expects patterns object as first argument' ) } options = options || { } ; EventEmitter . call ( self ) ; if ( process . browser ) { //bind to window by default
 self . _bindToWindow = 'bindToWindow' in options ? options . bindToWindow : true ; if ( self . _bindToWindow ) { // add to module-scoped list of routers
 routers . push ( self ) ; // override any url input with window location
 options . url = window . document . location . pathname + window . document . location . search ; } } // initialise state
 self . patterns = patterns ; self . name = null ; self . params = { } ; self . routes = { } ; _ . forEach ( _ . keys ( self . patterns ) , function ( route ) { self . routes [ route ] = null ; } ) ; // normalise state
 self . _update ( options . url || '' , false ) ; // implement initialEmit option
 if ( options . initialEmit ) { var cancel = cancellableNextTick ( function ( ) { self . _emit ( ) ; } ) ; self . once ( 'url' , cancel ) ; } }
Mutable observable abstraction of url as route with parameters
@example
var router = new ObsRouter({
home: '/',
blog: '/blog(/tag/:tag)(/:slug)',
contact: '/contact'
}, {
initialEmit: true,
bindToWindow: false,
url: '/?foo=bar'
});
console.log(router.url, router.name, router.params, router.routes);
-> '/?foo=bar' 'home' {foo: 'bar'} {home: {foo: 'bar'}, blog: null, contact: null}
@class ObsRouter
@param {Object} patterns Pathname patterns keyed by route names.
@param {Object} [options] Options
@param {Boolean} [options.initialEmit=false] If true, events will be emitted after nextTick, unless emitted earlier due to changes, ensuring that events are emitted at least once.
@param {Boolean} [options.bindToWindow=true] Bind to document location if running in browser
@param {string} [options.url=""] Initial url. If binding to document loctation then this is ignored.
Mutable observable abstraction of url as route with parameters