diff --git a/benchmark/index.js b/benchmark/index.js index 3b25b26..ddd1835 100644 --- a/benchmark/index.js +++ b/benchmark/index.js @@ -1,7 +1,7 @@ import assert from 'assert' import Benchmark from 'benchmarkjs-pretty' import { koaRouter1, koaRouter2 } from './router_koa.js' -import { bottleRouter1, bottleRouter2 } from './router_bottle.js' +import { flaskaRouter1, flaskaRouter2 } from './router_flaska.js' import { expressRouter1, expressRouter2 } from './router_express.js' import * as consts from './const.js' @@ -25,7 +25,7 @@ function TestSmallStaticRoute() { assert.ok(testData.route) }) .add('bottle-router', function() { - testData = bottleRouter1.match('/api/staff') + testData = flaskaRouter1.match('/api/staff') assert.ok(testData.handler) }) .run() @@ -50,7 +50,7 @@ function TestSmallParamRoute() { assert.ok(testData.route) }) .add('bottle-router', function() { - testData = bottleRouter1.match('/api/staff/justatest') + testData = flaskaRouter1.match('/api/staff/justatest') assert.ok(testData.handler) }) .run() @@ -75,7 +75,7 @@ function TestLargeStaticRoute() { assert.ok(testData.route) }) .add('bottle-router', function() { - testData = bottleRouter2.match('/api/staff') + testData = flaskaRouter2.match('/api/staff') assert.ok(testData.handler) }) .run() @@ -100,7 +100,7 @@ function TestLargeParamRoute() { assert.ok(testData.route) }) .add('bottle-router', function() { - testData = bottleRouter2.match('/api/staff/justatest') + testData = flaskaRouter2.match('/api/staff/justatest') assert.ok(testData.handler) }) .run() diff --git a/benchmark/router_bottle.js b/benchmark/router_flaska.js similarity index 59% rename from benchmark/router_bottle.js rename to benchmark/router_flaska.js index fb94ecf..c114fd6 100644 --- a/benchmark/router_bottle.js +++ b/benchmark/router_flaska.js @@ -1,8 +1,8 @@ -import { BottleRouter } from '../bottle.js' +import { FlaskaRouter } from '../flaska.mjs' import * as consts from './const.js' -const router1 = new BottleRouter() -const router2 = new BottleRouter() +const router1 = new FlaskaRouter() +const router2 = new FlaskaRouter() for (let key in consts.allRoutes) { router1.addRoute(consts.allRoutes[key], consts.dummy) @@ -13,6 +13,6 @@ for (let key in consts.allManyRoutes) { } export { - router1 as bottleRouter1, - router2 as bottleRouter2, + router1 as flaskaRouter1, + router2 as flaskaRouter2, } diff --git a/bottle.js b/flaska.mjs similarity index 94% rename from bottle.js rename to flaska.mjs index 5f9aa6c..572ea90 100644 --- a/bottle.js +++ b/flaska.mjs @@ -11,11 +11,11 @@ function Branch() { const __paramMapName = '__param' -function BottleRouter() { +function FlaskaRouter() { this._root = new Branch() } -BottleRouter.prototype.addRoute = function(route, handler) { +FlaskaRouter.prototype.addRoute = function(route, handler) { if (route[0] !== '/') throw new Error(`route "${route}" must start with forward slash`); @@ -76,7 +76,7 @@ BottleRouter.prototype.addRoute = function(route, handler) { } } -BottleRouter.prototype.match = function(url) { +FlaskaRouter.prototype.match = function(url) { let branch = this._root; let start = 1; let end = 1; @@ -120,5 +120,5 @@ BottleRouter.prototype.match = function(url) { } export { - BottleRouter, + FlaskaRouter, } diff --git a/package.json b/package.json index 09368b1..de9b5bf 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { - "name": "bottle-node", + "name": "flasha-node", "version": "1.0.0", - "description": "Bottle is a micro web-framework for node. It is designed to be fast, simple and lightweight, and is distributed as a single file module with no dependencies.", - "main": "bottle.js", + "description": "Flaska is a micro web-framework for node. It is designed to be fast, simple and lightweight, and is distributed as a single file module with no dependencies.", + "main": "flaska.mjs", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "eltro" }, "type": "module", "repository": { @@ -16,5 +16,8 @@ "bugs": { "url": "https://github.com/nfp-projects/bottle-node/issues" }, - "homepage": "https://github.com/nfp-projects/bottle-node#readme" + "homepage": "https://github.com/nfp-projects/bottle-node#readme", + "devDependencies": { + "eltro": "^0.9.0" + } } diff --git a/test/flaska.test.mjs b/test/flaska.test.mjs new file mode 100644 index 0000000..ebb165f --- /dev/null +++ b/test/flaska.test.mjs @@ -0,0 +1,48 @@ +import { Eltro as t, assert } from 'eltro' +import { FlaskaRouter } from '../flaska.mjs' + +t.describe('FlaskaRouter', function() { + t.describe('#match()', function() { + t.test('should match basic paths', function() { + let assertMatched = false + let router = new FlaskaRouter() + router.addRoute('/test', function() { assertMatched = true }) + let result = router.match('/test') + assert.ok(result.handler) + result.handler() + assert.strictEqual(assertMatched, true) + }) + + t.test('should match variable paths', function() { + const assertParameter = 'bla' + let assertMatched = false + let router = new FlaskaRouter() + router.addRoute('/test/:id', function() { assertMatched = true }) + let result = router.match('/test/' + assertParameter) + assert.ok(result.handler) + result.handler() + assert.strictEqual(assertMatched, true) + assert.strictEqual(result.params.get('id'), assertParameter) + }) + + t.test('should match paths properly', function() { + let assertMatched = true + let router = new FlaskaRouter() + router.addRoute('/test/:id', function() { assertMatched = false }) + router.addRoute('/test/:id/test1', function() { }) + let result = router.match('/test/asdf/test1') + assert.ok(result.handler) + result.handler() + assert.strictEqual(assertMatched, true) + assert.strictEqual(result.params.get('id'), 'asdf') + }) + + t.test('should return null when no match is found', function() { + let router = new FlaskaRouter() + router.addRoute('/test/:id', function() { }) + router.addRoute('/test/:id/test1', function() { }) + let result = router.match('/test/asdf/test2') + assert.notOk(result) + }) + }) +})