Skip to content

Commit

Permalink
mutil param names
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Apr 7, 2015
1 parent 5dcb8c5 commit e813249
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 269 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ branches:
- master

script: "npm run test-ci"
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
after_script:
- "npm run benchmark"
- "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A fast HTTP router, inspired by [Echo](https://github.com/labstack/echo)'s Route

See [benchmarks](benchmarks), use GitHub API Routes.

**trek-router** VS
**VS**

* [path-to-regexp][]
* [route-recognizer][]
Expand Down Expand Up @@ -53,6 +53,7 @@ let result = r.find('GET', '/users/233')
// => [handler, params]
// => [()=>{}, [{name: id, value: 233}]]

// Not Found
let result = r.find('GET', '/photos/233')
// => [handler, params]
// => [null, []]
Expand Down
264 changes: 1 addition & 263 deletions benchmarks/index.js

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,32 @@ let Router = (function () {
});
}

Router.prototype.format = function format(path) {
let p = path;
let k = 0;
let alias = [];
for (let i = 0, l = path.length; i < l; i++) {
if (path.charCodeAt(i) === 58) {
let j = 0;
k++;
for (; i < l && path.charCodeAt(i) !== 47; i++) {
j++;
}
p = p.substring(0, i - j + 1 + (p.length - path.length)) + '$' + k + path.substring(i);
alias.push(path.substring(i - j + 1, i));
}
}
return [p, alias];
};

Router.prototype.add = function add(method, path, handler) {
var _format = this.format(path);

let p = _format[0];
let alias = _format[1];

if (handler) handler.alias = alias;
path = p;
for (let i = 0, l = path.length; i < l; i++) {
// `:`
if (path.charCodeAt(i) === 58) {
Expand Down Expand Up @@ -147,6 +172,11 @@ let Router = (function () {
// Found
result[0] = cn.handler;
result[1] = params;
if (cn.handler) {
cn.handler.alias.forEach(function (a, i) {
params[i].name = a;
});
}
return result;
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"devDependencies": {
"babel": "^5.x",
"benchmark": "^1.0.0",
"isparta": "^3.0.0",
"isparta": "^3.x",
"istanbul": "^0.3.13",
"mocha": "^2.2.1",
"mocha": "^2.x",
"path-to-regexp": "^1.0.3",
"route-recognizer": "^0.1.5",
"route-trie": "^0.2.0",
Expand Down
26 changes: 26 additions & 0 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,28 @@ class Router {
});
}

format(path) {
let p = path;
let k = 0;
let alias = [];
for (let i = 0, l = path.length; i < l; i++) {
if (path.charCodeAt(i) === 58) {
let j = 0;
k++;
for (; i < l && (path.charCodeAt(i) !== 47); i++) {
j++;
}
p = p.substring(0, i - j + 1 + (p.length - path.length)) + '$' + k + path.substring(i);
alias.push(path.substring(i - j + 1, i));
}
}
return [p, alias];
}

add(method, path, handler) {
let [p, alias] = this.format(path);
if (handler) handler.alias = alias
path = p;
for (let i = 0, l = path.length; i < l; i++) {
// `:`
if (path.charCodeAt(i) === 58) {
Expand Down Expand Up @@ -144,6 +165,11 @@ class Router {
// Found
result[0] = cn.handler;
result[1] = params;
if (cn.handler) {
cn.handler.alias.forEach((a, i) => {
params[i].name = a;
});
}
return result;
}

Expand Down
262 changes: 262 additions & 0 deletions test/github-api.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions test/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ describe('Router', () => {
r.add('GET', '/users/:id', () => {})
r.add('GET', '/users/:userId/photos/:photoId', () => {})
r.trees['GET'].printTree('', true)
/*
let [h, params] = r.find('GET', '/users/233');
assert.notEqual(null, h);
assert.equal('id', params[0].name);
Expand All @@ -102,7 +101,6 @@ describe('Router', () => {
assert.equal(233, params2[0].value);
assert.equal('photoId', params2[1].name);
assert.equal(377, params2[1].value);
*/
});

it('params resource', () => {
Expand Down

0 comments on commit e813249

Please sign in to comment.