From 61a9edbab2b8c4a36bc722cc3e512f524fd6bd6c Mon Sep 17 00:00:00 2001 From: lengfangbing Date: Sun, 20 Sep 2020 15:26:45 +0800 Subject: [PATCH] feat: add new parser for router path --- router_test.ts | 1 + utils/test/url_test.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/router_test.ts b/router_test.ts index afd5e1d..b396dc7 100644 --- a/router_test.ts +++ b/router_test.ts @@ -1,6 +1,7 @@ import { parseUrlQuery, splitPath, + splitNewPath, splitUrl } from './utils/test/url_test.ts'; diff --git a/utils/test/url_test.ts b/utils/test/url_test.ts index f1ec227..f6ce41a 100644 --- a/utils/test/url_test.ts +++ b/utils/test/url_test.ts @@ -91,6 +91,33 @@ export function splitPath(path: string){ } return res; } +export function splitNewPath(path: string){ + const res = []; + let url = path.substring(1) || '/'; + let i = 0; + while((i = url.indexOf('/')) >= 0){ + const v = url.substring(0, i); + let j = 0; + if((j = v.indexOf(':')) >= 0){ + res.push({key: '', paramsName: v.substring(j+1)}); + }else if((j = v.indexOf('*')) >= 0){ + res.push({key: '#', paramsName: v.substring(j+1)}); + }else{ + res.push(`/${v}`); + } + url = url.substring(i+1); + } + if(url.length){ + if((i = url.indexOf(':')) >= 0){ + res.push({key: '', paramsName: url.substring(i+1)}); + }else if((i = url.indexOf('*')) >= 0){ + res.push({key: '#', paramsName: url.substring(i+1)}); + }else{ + res.push(`/${url}`); + } + } + return res; +} declare global{ interface Map{ toObj: Function