Skip to content

Commit

Permalink
improve router config
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 12, 2017
1 parent 2d6f649 commit 9465ad9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ const router = parser.parse([
'getFavorites',
],
],
[
'GET',
'/users',
[
'getUser',
],
],
]);
const app = new Koa();
app.use(router.routes());
Expand Down
29 changes: 20 additions & 9 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ function convertUrls(str) {
}

function convertHandlers(str) {
if (isFunction(str)) {
return [str];
}
const arr = Array.isArray(str) ? str : str.split('&');
return arr.map((item) => {
if (isString(item)) {
Expand All @@ -38,6 +41,20 @@ function convertHandlers(str) {
});
}

function cut(str) {
if (!isString(str)) {
return str;
}
let tmp = str.trim();
if (tmp.charAt(0) === '[') {
tmp = tmp.substring(1);
}
if (tmp.charAt(tmp.length - 1) === ']') {
tmp = tmp.substring(0, tmp.length - 1);
}
return tmp;
}

/**
* [covert description]
* @param {[type]} str [description]
Expand All @@ -52,12 +69,6 @@ function covert(str) {
if (descArr.length !== 3) {
throw new Error(`${descArr.join(' ')} is invalid`);
}
const cut = (tmp) => {
if (isString(tmp)) {
return tmp.substring(1, tmp.length - 1);
}
return tmp;
};
return {
methods: convertMethods(cut(descArr[0])),
urls: convertUrls(cut(descArr[1])),
Expand All @@ -78,9 +89,9 @@ function covert(str) {
throw new Error(`${str} is invalid`);
}
return {
methods: convertMethods(arr[0].substring(1)),
urls: convertUrls(arr[1]),
handlers: convertHandlers(arr[2].substring(0, arr[2].length - 1)),
methods: convertMethods(cut(arr[0])),
urls: convertUrls(cut(arr[1])),
handlers: convertHandlers(cut(arr[2])),
};
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koa-router-parser",
"description": "router parser for koa",
"version": "3.1.0",
"version": "3.1.1",
"author": "Tree Xie <vicansocanbico@gmail.com>",
"keywords": [
"koa",
Expand Down
4 changes: 2 additions & 2 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ describe('koa-router-parser', () => {

it('should parse function using array successful', (done) => {
const router = parser.parse([
['[GET]', '[/user]', '[types(["xml", "json"]) & check-version(1) & check-name("vicanso") & check-options({"tag":"a"},1) & user]']
['GET', '[/user]', '[types(["xml", "json"]) & check-version(1) & check-name("vicanso") & check-options({"tag":"a"},1) & user]']
]);

const app = new Koa();
Expand All @@ -306,7 +306,7 @@ describe('koa-router-parser', () => {
const router = parser.parse([
[
['GET'],
['/user'],
'/user',
[
(ctx, next) => next(),
'types(["xml", "json"])',
Expand Down

0 comments on commit 9465ad9

Please sign in to comment.