Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

wayfarer

Package Overview
Dependencies
Maintainers
4
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wayfarer - npm Package Compare versions

Comparing version 5.0.1 to 6.0.0

CHANGELOG.md

117

index.js

@@ -1,113 +0,52 @@

const routington = require('routington')
const symbol = require('es6-symbol')
const assert = require('assert')
const xtend = require('xtend')
const trie = require('./trie')
const sym = symbol('wayfarer')
module.exports = Wayfarer
module.exports = wayfarer
// create a router
// str -> obj
function wayfarer (dft) {
dft = sanitizeUri(dft) || ''
const routes = routington()
const subrouters = routington()
function Wayfarer (dft) {
if (!(this instanceof Wayfarer)) return new Wayfarer(dft)
emit._subrouters = subrouters
emit._default = defaultFn
emit._routes = routes
emit[sym] = true
emit._sym = sym
const _default = (dft || '').replace(/^\//, '')
const _trie = trie()
emit._trie = _trie
emit.emit = emit
emit.on = on
emit._wayfarer = true
return emit
// define a path
// define a route
// (str, fn) -> obj
function on (path, cb) {
assert.equal(typeof path, 'string')
function on (route, cb) {
assert.equal(typeof route, 'string')
assert.equal(typeof cb, 'function')
path = sanitizeUri(path) || ''
const node = cb[sym] ? subrouters.define(path)[0] : routes.define(path)[0]
if (Array.isArray(node.cb)) node.cb.push(cb)
else node.cb = [cb]
return emit
}
// match and call a route
// str -> null
function emit (path, params, parentDefault) {
path = sanitizeUri(path) || ''
params = params || {}
route = route || '/'
const sub = matchSub(path)
if (sub) path = sub.path
if (cb && cb._wayfarer && cb._trie) {
_trie.mount(route, cb._trie.trie)
} else {
const node = _trie.create(route)
node.cb = cb
}
const localDft = routes.match(dft) || parentDefault
const match = sub ? sub.matched : routes.match(path) || localDft
assert.ok(match, 'path ' + path + ' did not match')
params = xtend(params, match.param)
// only nested routers need a path
match.node.cb.forEach(function (cb) {
sub ? cb(path, params, localDft) : cb(params)
})
return emit
}
// match the default route
// obj? -> null
function defaultFn (params) {
emit(dft, params)
}
// match and call a route
// (str, obj?) -> null
function emit (route) {
assert.notEqual(route, undefined, "'route' must be defined")
// match a mounted router
// str -> obj|null
function matchSub (path) {
var match = null
const split = path.split('/')
var count = split.length
const node = _trie.match(route)
if (node && node.cb) return node.cb(node.params)
var n = 0
while (n < split.length) {
var arr = []
var ln = split.length - n
var cnt = -1
const dft = _trie.match(_default)
if (dft && dft.cb) return dft.cb(dft.params)
while (++cnt < ln) {
arr.push(split[cnt])
}
var nw = arr.join('/')
var imatch = subrouters.match(nw)
if (imatch) {
match = imatch
count = arr.length
break
}
n++
}
if (!match) return
while (count--) {
split.shift()
}
path = split.join('/')
const ret = {
matched: match,
path: path
}
return ret
throw new Error("route '" + route + "' did not match")
}
}
// strip leading `/`
// str -> str
function sanitizeUri (str) {
str = str || ''
return str.replace(/^\//, '')
}
{
"name": "wayfarer",
"version": "5.0.1",
"version": "6.0.0",
"description": "Composable trie based router",

@@ -28,5 +28,3 @@ "main": "index.js",

"dependencies": {
"es6-symbol": "^2.0.1",
"routington": "^1.0.2",
"xtend": "^4.0.0"
"xtend": "^4.0.1"
},

@@ -36,2 +34,3 @@ "devDependencies": {

"mocha": "^1.20.1",
"noop2": "^2.0.0",
"standard": "^4.0.1",

@@ -43,4 +42,5 @@ "tape": "^3.0.3"

"index.js",
"trie.js",
"README.md"
]
}

@@ -1,14 +0,17 @@

# wayfarer
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![Downloads][downloads-image]][downloads-url]
[![js-standard-style][standard-image]][standard-url]
# wayfarer [![stability][0]][1]
[![npm version][2]][3] [![build status][4]][5] [![test coverage][6]][7]
[![downloads][8]][9] [![js-standard-style][10]][11]
Composable [trie based](https://github.com/jonathanong/routington/) router.
It is faster than traditional, linear, regular expression-matching routers,
It's faster than traditional, linear, regular expression-matching routers,
although insignficantly, and scales with the number of routes.
### features
- works with any framework
- built for speed
- minimal dependencies
- extensible
## Installation
```bash
```sh
$ npm install wayfarer

@@ -25,5 +28,5 @@ ```

router.on('/404', () => console.log('404 not found'))
router.on('/:user', params => console.log('user is %s', params.user))
router.on('/:user', ctx => console.log('user is %s', ctx.params.user))
router('/tobi')
router('tobi')
// => 'user is tobi'

@@ -43,4 +46,4 @@

r2.on('/child', () => console.log('subrouter trix!'))
r1.on('/:parent', r2)
r2.on('/child', () => console.log('subrouter trix!'))

@@ -53,21 +56,43 @@ r1('/dada/child')

### router = wayfarer(default)
Initialize a router with a default route. Doesn't ignore querystrings and hashes.
Initialize a router with a default route. Doesn't ignore querystrings and
hashes.
### router.on(route, cb(params))
### el = router.on(route, cb(ctx, arguments...))
Register a new route. The order in which routes are registered does not matter.
Routes can register multiple callbacks. See
[`routington.define()`](https://github.com/pillarjs/routington#nodes-node--routerdefineroute)
for all route options.
for all route options. When called, the callback is passed a context object and
any additional arguments from `router()`. The context object has the following
properties:
- __route__: the remaining route that wasn't matched
### router(route)
### router(route|context, arguments...)
Match a route and execute the corresponding callback. Alias: `router.emit()`.
Accepts either a `route` string, or a context object with the following
possible properties:
- __route__: the route to be matched
- __params__: an parameters object
Any arguments passed after the `route/context` are passed as-is into the
function handler.
## Events
### router.on('error', cb(err, params, route, arguments...))
Aside from having a default path, a listener can be attached to handle errors.
If no listener is attached, an error will be thrown.
## Internals
__Warning__: these methods are considered internal and should only be used when
extending wayfarer.
- `router._default(params)`: Trigger the default route. Useful to propagate
error states.
- `routes = router._routes`: Expose the mounted routes.
- `subrouters = router._subrouters`: Expose the mounted subrouters.
Wayfarer is built on an internal trie structure. If you want to build a router
using this trie structure it can be accessed through
`require('wayfarer/trie')`. It exposes the following methods:
- `trie = Trie()` - create a new trie
- `node = trie.create(route)` - create a node at a path, and return a node
- `node = trie.match(route)` - match a route on the the trie and return a node
- `trie.mount(path, trie)` - mount a trie onto a node at route
## Known issues
### multiple nested partials don't match
E.g. `/:foo/:bar/:baz` won't work. This is due `Trie.mount()` overriding child
partial paths when mounted. I'll get around to fixing this at some point in the
future, but if anyone wants to contribute a patch it'd most appreciated.
## FAQ

@@ -86,5 +111,8 @@ ### Why did you build this?

## See Also
- [hash-match](https://github.com/sethvincent/hash-match) - easy `window.location.hash` matching
- [pathname-match](https://github.com/yoshuawuyts/pathname-match) - strip querystrings and hashes from a url
- [wayfarer-to-server](https://github.com/yoshuawuyts/wayfarer-to-server) - Wrap wayfarer to provide HTTP method matching and `req, res` delegation
- [hash-match](https://github.com/sethvincent/hash-match) - easy
`window.location.hash` matching
- [pathname-match](https://github.com/yoshuawuyts/pathname-match) - strip
querystrings and hashes from a url
- [wayfarer-to-server](https://github.com/yoshuawuyts/wayfarer-to-server) -
Wrap wayfarer to provide HTTP method matching and `req, res` delegation

@@ -94,11 +122,15 @@ ## License

[npm-image]: https://img.shields.io/npm/v/wayfarer.svg?style=flat-square
[npm-url]: https://npmjs.org/package/wayfarer
[travis-image]: https://img.shields.io/travis/yoshuawuyts/wayfarer/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/yoshuawuyts/wayfarer
[coveralls-image]: https://img.shields.io/coveralls/yoshuawuyts/wayfarer.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/yoshuawuyts/wayfarer?branch=master
[downloads-image]: http://img.shields.io/npm/dm/wayfarer.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/wayfarer
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[standard-url]: https://github.com/feross/standard
[0]: https://img.shields.io/badge/stability-2%20stable-brightgreen.svg?style=flat-square
[1]: https://nodejs.org/api/documentation.html#documentation_stability_index
[2]: https://img.shields.io/npm/v/wayfarer.svg?style=flat-square
[3]: https://npmjs.org/package/wayfarer
[4]: https://img.shields.io/travis/yoshuawuyts/wayfarer/master.svg?style=flat-square
[5]: https://travis-ci.org/yoshuawuyts/wayfarer
[6]: https://img.shields.io/codecov/c/github/yoshuawuyts/wayfarer/master.svg?style=flat-square
[7]: https://codecov.io/github/yoshuawuyts/wayfarer
[8]: http://img.shields.io/npm/dm/wayfarer.svg?style=flat-square
[9]: https://npmjs.org/package/wayfarer
[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[11]: https://github.com/feross/standard
[12]: http://github.com/raynos/mercury
[13]: http://github.com/raynos/virtual-dom
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc