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

routington

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

routington - npm Package Compare versions

Comparing version 0.0.11 to 0.1.0

benchmark.js

6

lib/define.js

@@ -14,5 +14,3 @@ var Routington = require('./routington')

var frags = route.split('/').slice(1)
if (frags[frags.length - 1] !== '')
frags.push('')
var frags = route.split('/')

@@ -52,3 +50,3 @@ try {

nodes = nodes.map(root.add, root)
nodes = nodes.map(root._add, root)

@@ -55,0 +53,0 @@ return frags.length - 1

@@ -17,2 +17,6 @@ var Routington = require('./routington')

Routington.prototype.match = function (url) {
var root = this
var frags = url.split('/')
var length = frags.length
var match = {

@@ -22,8 +26,2 @@ param: {}

var frags = url.split('/').slice(1)
var length = frags.length
if (frags[length - 1] !== '')
length = frags.push('')
var root = this
var frag, node, nodes, regex, name

@@ -30,0 +28,0 @@

@@ -0,7 +1,29 @@

var inherits = require('util').inherits
module.exports = Routington
Routington.extend = function () {
function Routington(options) {
if (!(this instanceof Routington))
return new Routington(options)
this._init(options)
}
inherits(Routington, this)
Object.keys(this).forEach(function (key) {
Routington[key] = this[key]
}, this)
return Routington
}
function Routington(options) {
if (!(this instanceof Routington))
return new Routington()
return new Routington(options)
this._init(options)
}
Routington.prototype._init = function (options) {
options = options || {}

@@ -25,9 +47,9 @@

// Find || (create && attach) a child node
Routington.prototype.add = function (options) {
return this.find(options)
|| this.attach(options)
Routington.prototype._add = function (options) {
return this._find(options)
|| this._attach(options)
}
// Find a child node based on a bunch of options
Routington.prototype.find = function (options) {
Routington.prototype._find = function (options) {
// Find by string

@@ -49,3 +71,3 @@ if (typeof options.string === 'string')

// Attach a node to this node
Routington.prototype.attach = function (node) {
Routington.prototype._attach = function (node) {
if (!(node instanceof Routington))

@@ -52,0 +74,0 @@ node = new Routington(node)

{
"name": "routington",
"description": "Trie-based URL Routing",
"version": "0.0.11",
"version": "0.1.0",
"devDependencies": {
"mocha": "*",
"should": "*"
"should": "*",
"matcha": "*"
},

@@ -20,6 +21,6 @@ "scripts": {

"type": "git",
"url": "https://github.com/berrington/routington.git"
"url": "https://github.com/jonathanong/routington.git"
},
"bugs": {
"url": "https://github.com/berrington/routington/issues",
"url": "https://github.com/jonathanong/routington/issues",
"email": "me@jongleberry.com"

@@ -26,0 +27,0 @@ },

@@ -13,6 +13,6 @@ ## Routington [![Build Status](https://travis-ci.org/jonathanong/routington.png)](https://travis-ci.org/jonathanong/routington)

For a node.js implementation,
we have built [dispatchington](https://github.com/berrington/dispatchington).
This can be used either as a standalone or as a replacement for Express' router.
Implementations:
- [koa-trie-router](https://github.com/koajs/trie-router) - for [koa](https://github.com/koajs)
### API

@@ -60,3 +60,3 @@

Each `node` of `nodes` will always have `node.string === ''`.
URLs are always treated with a trailing `/` by design.
URLs are always treated with a trailing `/` by design. __Note: this will be changed in the future__.

@@ -89,2 +89,74 @@ You should always name your regular expressions otherwise you can't use the captured value.

### Building a Router on top of Routington
Each URL you define creates a node,
and you are free to do whatever you'd like with each node as long you don't overwrite any prototype properties (basically just `define`, `match`, and `parse`).
Adding any features to routington shouldn't be necessary.
For example, suppose you want to attach callbacks to a node by extending routington:
```js
router.get('/:id/:controller', function (req, res, next) {
console.log('do something')
})
```
You can attach the middleware to a `node.GET` array:
```js
router.get = function (path, handler) {
var node = router.define(path)
node.GET = node.GET || []
node.GET.push(handler)
}
```
Now, dispatching is easy:
```js
function dispatcher(req, res, next) {
var match = router.match(url.parse(req.url).pathname)
if (!match)
// this is a 404
var node = match.node
var callbacks = node[req.method]
if (!callbacks)
// this is a 405
// execute all the callbacks.
// async.series won't actually work here,
// but you get the point.
async.series(callbacks, next)
}
```
Properties attached to the node will be exposed on the match.
For example,
suppose you wanted to label a node:
```js
var node = router.define('/:id/:controller')
node.label = 'controller'
```
When matched, it will be available via `match.node.label`:
```js
var match = router.match('/someid/somecontroller')
assert(match.node.label === 'label')
```
Since reaching into `match.node` is a little inconvenient and you probably don't want your end users to touch it,
you should expose in your dispatcher:
```js
var match = router.match(url.parse(req.url).pathname)
// ...
req.param = match.param
req.label = match.node.label
```
### Browser Support

@@ -91,0 +163,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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