find-my-way
Advanced tools
Comparing version 0.2.4 to 1.0.0
35
index.js
@@ -8,2 +8,9 @@ 'use strict' | ||
matchAll: 2, | ||
Char codes: | ||
'/': 47 | ||
':': 58 | ||
'*': 42 | ||
'?': 63 | ||
'#': 35 | ||
*/ | ||
@@ -41,3 +48,3 @@ | ||
// parametric route | ||
if (path.charCodeAt(i) === 58 /* : */) { | ||
if (path.charCodeAt(i) === 58) { | ||
j = i + 1 | ||
@@ -47,3 +54,3 @@ this._insert(method, path.slice(0, i), 0, null, null, null) | ||
// isolate the parameter name | ||
while (i < len && path.charCodeAt(i) !== 47 /* / */) i++ | ||
while (i < len && path.charCodeAt(i) !== 47) i++ | ||
params.push(path.slice(j, i)) | ||
@@ -62,3 +69,3 @@ | ||
// wildcard route | ||
} else if (path.charCodeAt(i) === 42 /* * */) { | ||
} else if (path.charCodeAt(i) === 42) { | ||
this._insert(method, path.slice(0, i), 0, null, null, null) | ||
@@ -101,3 +108,3 @@ params.push('*') | ||
currentNode.label = currentNode.prefix[0] | ||
currentNode.map = {} | ||
currentNode.map = null | ||
currentNode.kind = 0 | ||
@@ -107,4 +114,4 @@ | ||
// add the handler to the parent node | ||
assert(!currentNode.findHandler(method), `Method '${method}' already declared for route '${path}'`) | ||
currentNode.addHandler(method, handler, params, store) | ||
assert(!currentNode.getHandler(method), `Method '${method}' already declared for route '${path}'`) | ||
currentNode.setHandler(method, handler, params, store) | ||
currentNode.kind = kind | ||
@@ -114,3 +121,3 @@ } else { | ||
node = new Node(path.slice(len), [], kind) | ||
node.addHandler(method, handler, params, store) | ||
node.setHandler(method, handler, params, store) | ||
// add the child to the parent | ||
@@ -129,3 +136,3 @@ currentNode.add(node) | ||
node = new Node(path, [], kind) | ||
node.addHandler(method, handler, params, store) | ||
node.setHandler(method, handler, params, store) | ||
// add the child to the parent | ||
@@ -135,4 +142,4 @@ currentNode.add(node) | ||
// the node already exist | ||
assert(!currentNode.findHandler(method), `Method '${method}' already declared for route '${path}'`) | ||
currentNode.addHandler(method, handler, params, store) | ||
assert(!currentNode.getHandler(method), `Method '${method}' already declared for route '${path}'`) | ||
currentNode.setHandler(method, handler, params, store) | ||
} | ||
@@ -168,3 +175,3 @@ return | ||
if (pathLen === 0 || path === prefix) { | ||
var handle = currentNode.findHandler(method) | ||
var handle = currentNode.getHandler(method) | ||
if (!handle) return null | ||
@@ -204,3 +211,3 @@ | ||
i = 0 | ||
while (i < pathLen && path.charCodeAt(i) !== 47 /* / */) i++ | ||
while (i < pathLen && path.charCodeAt(i) !== 47) i++ | ||
try { | ||
@@ -245,5 +252,5 @@ params[pindex++] = decodeURIComponent(path.slice(0, i)) | ||
function sanitizeUrl (url) { | ||
for (var i = 0; i < url.length; i++) { | ||
for (var i = 0, len = url.length; i < len; i++) { | ||
var charCode = url.charCodeAt(i) | ||
if (charCode === 63 /* ? */ || charCode === 35 /* # */) { | ||
if (charCode === 63 || charCode === 35) { | ||
return url.slice(0, i) | ||
@@ -250,0 +257,0 @@ } |
24
node.js
@@ -16,3 +16,3 @@ 'use strict' | ||
this.kind = kind || 0 | ||
this.map = map || {} | ||
this.map = map || null | ||
} | ||
@@ -27,4 +27,5 @@ | ||
for (var i = 0; i < this.numberOfChildren; i++) { | ||
if (this.children[i].label === label && this.children[i].kind === kind) { | ||
return this.children[i] | ||
var child = this.children[i] | ||
if (child.label === label && child.kind === kind && child.map) { | ||
return child | ||
} | ||
@@ -37,4 +38,5 @@ } | ||
for (var i = 0; i < this.numberOfChildren; i++) { | ||
if (this.children[i].label === label) { | ||
return this.children[i] | ||
var child = this.children[i] | ||
if (child.label === label) { | ||
return child | ||
} | ||
@@ -47,4 +49,5 @@ } | ||
for (var i = 0; i < this.numberOfChildren; i++) { | ||
if (this.children[i].kind === kind) { | ||
return this.children[i] | ||
var child = this.children[i] | ||
if (child.kind === kind && child.map) { | ||
return child | ||
} | ||
@@ -55,3 +58,4 @@ } | ||
Node.prototype.addHandler = function (method, handler, params, store) { | ||
Node.prototype.setHandler = function (method, handler, params, store) { | ||
this.map = this.map || {} | ||
this.map[method] = { | ||
@@ -64,6 +68,6 @@ handler: handler, | ||
Node.prototype.findHandler = function (method) { | ||
return this.map[method] | ||
Node.prototype.getHandler = function (method) { | ||
return this.map ? this.map[method] : null | ||
} | ||
module.exports = Node |
{ | ||
"name": "find-my-way", | ||
"version": "0.2.4", | ||
"version": "1.0.0", | ||
"description": "Crazy fast http radix based router", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,3 +5,3 @@ # find-my-way | ||
A crazy fast HTTP router, internally uses an highly performant [Radix Tree](https://en.wikipedia.com/wiki/Radix_tree) (aka compact [Prefix Tree](https://en.wikipedia.com/wiki/Trie)), supports route params, wildcards, and it's framework independent. | ||
A crazy fast HTTP router, internally uses an highly performant [Radix Tree](https://en.wikipedia.org/wiki/Radix_tree) (aka compact [Prefix Tree](https://en.wikipedia.org/wiki/Trie)), supports route params, wildcards, and it's framework independent. | ||
It is inspired by the [echo](https://github.com/labstack/echo) router, some parts have been extracted from [trekjs](https://github.com/trekjs) router. | ||
@@ -8,0 +8,0 @@ |
@@ -13,2 +13,3 @@ 'use strict' | ||
findMyWay.on(0, '/test', () => {}) | ||
t.fail('method shoukd be a string') | ||
} catch (e) { | ||
@@ -25,2 +26,3 @@ t.is(e.message, 'Method should be a string') | ||
findMyWay.on('GET', 0, () => {}) | ||
t.fail('path should be a string') | ||
} catch (e) { | ||
@@ -37,2 +39,3 @@ t.is(e.message, 'Path should be a string') | ||
findMyWay.on('GET', '/test', 0) | ||
t.fail('handler should be a function') | ||
} catch (e) { | ||
@@ -49,2 +52,3 @@ t.is(e.message, 'Handler should be a function') | ||
findMyWay.on('GETT', '/test', () => {}) | ||
t.fail('method is not a valid http method') | ||
} catch (e) { | ||
@@ -61,2 +65,3 @@ t.is(e.message, 'Method \'GETT\' is not an http method.') | ||
}) | ||
t.fail('default route must be a function') | ||
} catch (e) { | ||
@@ -74,2 +79,3 @@ t.is(e.message, 'The default route must be a function') | ||
findMyWay.on('GET', '/test', () => {}) | ||
t.fail('method already declared') | ||
} catch (e) { | ||
@@ -90,2 +96,3 @@ t.is(e.message, `Method 'GET' already declared for route 'test'`) | ||
findMyWay.on('GET', '/test/hello', () => {}) | ||
t.fail('method already delcared in nested route') | ||
} catch (e) { | ||
@@ -92,0 +99,0 @@ t.is(e.message, `Method 'GET' already declared for route 'hello'`) |
@@ -189,2 +189,16 @@ 'use strict' | ||
test('catch all wildcard', t => { | ||
t.plan(1) | ||
const findMyWay = FindMyWay() | ||
findMyWay.on('GET', '*', (req, res, params) => { | ||
t.is(params['*'], '/test/hello') | ||
}) | ||
findMyWay.lookup( | ||
{ method: 'GET', url: '/test/hello' }, | ||
null | ||
) | ||
}) | ||
test('find should return the route', t => { | ||
@@ -191,0 +205,0 @@ t.plan(1) |
@@ -93,1 +93,23 @@ 'use strict' | ||
}) | ||
test('automatic default route', t => { | ||
t.plan(3) | ||
const findMyWay = FindMyWay() | ||
const server = http.createServer((req, res) => { | ||
findMyWay.lookup(req, res) | ||
}) | ||
server.listen(0, err => { | ||
t.error(err) | ||
server.unref() | ||
request({ | ||
method: 'GET', | ||
uri: 'http://localhost:' + server.address().port | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 404) | ||
}) | ||
}) | ||
}) |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
29073
783
0