Comparing version 1.3.1 to 2.0.0
111
index.js
@@ -1,92 +0,49 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
var router = require('routington'); | ||
var assert = require('assert'); | ||
var router = require('routington') | ||
var assert = require('assert') | ||
/** | ||
* Wayfarer prototype | ||
*/ | ||
var wayfarer = Wayfarer.prototype | ||
var wayfarer = Wayfarer.prototype; | ||
module.exports = Wayfarer | ||
/** | ||
* Expose 'router' | ||
*/ | ||
module.exports = Wayfarer; | ||
/** | ||
* Create a new router | ||
* | ||
* @return {Wayfarer} | ||
* @api public | ||
*/ | ||
// Create a new router. | ||
// @param {Object} opts | ||
// @prop {String} default | ||
// @return {Object} | ||
function Wayfarer(opts) { | ||
if (!(this instanceof Wayfarer)) return new Wayfarer(opts); | ||
assert(!opts || 'object' == typeof opts, 'wayfarer: pts should be an object'); | ||
this.router = router(); | ||
this.defaultPath = ''; | ||
this._opts = opts || {}; | ||
if (!(this instanceof Wayfarer)) return new Wayfarer(opts) | ||
opts = opts || {} | ||
this.router = router() | ||
this.defaultPath = opts.default || '' | ||
} | ||
/** | ||
* Define a new path. | ||
* | ||
* @param {String} path | ||
* @param {Function} cb | ||
* @return {Wayfarer} | ||
* @api public | ||
*/ | ||
// Define a new path. | ||
// @param {String} path | ||
// @param {Function} cb | ||
// @return {Wayfarer} | ||
wayfarer.on = function(path, cb) { | ||
assert('string' == typeof path, 'Path should be a string') | ||
assert('function' == typeof cb, 'Callback should be a function') | ||
var node = this.router.define(path)[0] | ||
node.cb = cb | ||
wayfarer.path = function(path, cb) { | ||
assert('string' == typeof path, 'Path should be a string'); | ||
assert('function' == typeof cb, 'Callback should be a function'); | ||
var node = this.router.define(path)[0]; | ||
node.cb = cb; | ||
return this; | ||
return this | ||
} | ||
/** | ||
* Define the default path. | ||
* | ||
* @param {String} path | ||
* @return {Wayfarer} | ||
* @api public | ||
*/ | ||
wayfarer.default = function(path) { | ||
assert('string' == typeof path, 'Path should be a string'); | ||
this.defaultPath = path; | ||
return this; | ||
} | ||
/** | ||
* Math a route against the paths. | ||
* | ||
* @param {String} path | ||
* @return {Any} | ||
* @api public | ||
*/ | ||
// Math a route against the paths. | ||
// @param {String} path | ||
// @return {Any} | ||
// @api public | ||
wayfarer.match = function(path) { | ||
assert('string' == typeof path, 'Path should be a string'); | ||
assert('string' == typeof path, 'Path should be a string') | ||
var nwPath = path; | ||
if (!this._opts.qs) nwPath = path.split('?')[0]; | ||
var nw = path.split('?')[0] | ||
var match = this.router.match(nw) | ||
if (!match) match = this.router.match(this.defaultPath) | ||
var match = this.router.match(path); | ||
if (!match) match = this.router.match(this.defaultPath); | ||
return match.node.cb(); | ||
return match.node.cb() | ||
} | ||
/** | ||
* Aliases. | ||
*/ | ||
wayfarer.route = wayfarer.path; | ||
// Aliases. | ||
wayfarer.path = wayfarer.on | ||
wayfarer.route = wayfarer.on |
{ | ||
"name": "wayfarer", | ||
"version": "1.3.1", | ||
"version": "2.0.0", | ||
"description": "A simple router built for minimalism and speed", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,4 +7,3 @@ # wayfarer | ||
A simple [trie based](https://github.com/jonathanong/routington/) | ||
router built for minimalism and speed. Works best with | ||
Modular [trie based](https://github.com/jonathanong/routington/) based router. Works best with | ||
[Browserify](github.com/substack/browserify). | ||
@@ -14,19 +13,17 @@ | ||
```bash | ||
npm install wayfarer | ||
$ npm install wayfarer | ||
``` | ||
## Overview | ||
## Usage | ||
```js | ||
var router = require('wayfarer'); | ||
const wayfarer = require('wayfarer') | ||
// Register routes. | ||
const router = wayfarer({ default: '/404' }) | ||
router({qs: false}) | ||
.default('/404') | ||
.route('/', function() {console.log('/')}) | ||
.route('/home', function() {console.log('/home')}) | ||
.route('/404', function() {console.log('/404')}) | ||
.route('/:user', function() {console.log('/user')}) | ||
.match('/tobi'); | ||
// => '/user' | ||
router.on('/', () => console.log('/')) | ||
router.on('/404', () => console.log('/404')) | ||
router.on('/:user', () => console.log('/user')) | ||
router.match('/tobi') | ||
// => '/:user' | ||
``` | ||
@@ -36,25 +33,17 @@ | ||
#### wayfarer(opts) | ||
Initialize wayfarer with options. Setting `qs` to false stops wayfarer from | ||
triggering on changes to the querystring. | ||
Initialize wayfarer with options. `default` allows setting a default path | ||
to match if none other match. Ignores query strings. | ||
```js | ||
var router = wayfarer({qs: false}); | ||
const router = wayfarer({ default: '/404' }) | ||
``` | ||
#### .route(path, cb) | ||
#### .on(path, cb) | ||
Register a new path. Partial paths are supported through the `/:` operator. | ||
Wayfarer uses a trie to match routes, so the order in which routes are | ||
Wayfarer uses a trie structure to match routes, so the order in which routes are | ||
registered does not matter. | ||
```js | ||
router.route('/', function() {console.log('do stuff')}); | ||
router.route('/:user', function() {console.log('do user stuff')}); | ||
router.on('/', () => console.log('do stuff')) | ||
router.on('/:user', () => console.log('do user stuff')) | ||
``` | ||
#### .default(defaultPath) | ||
Sets a default path to match. This is particularly | ||
useful for setting 404 pages. | ||
```js | ||
router.default('/404'); | ||
router.route('/404', function() {console.log('sunglasses not found')}); | ||
``` | ||
#### .match(path) | ||
@@ -64,6 +53,9 @@ Match a path against the saved paths in the router. If a match is | ||
```js | ||
router.match('/tobi'); | ||
router.match('/tobi') | ||
// => 'do user stuff' | ||
``` | ||
## See Also | ||
- [hash-match](https://github.com/sethvincent/hash-match) - easy `window.location.hash` matching | ||
## License | ||
@@ -70,0 +62,0 @@ [MIT](https://tldrlegal.com/license/mit-license) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6100
39
68