From 9e66cb6b8a1b008bd372d2e4412a020b16d36952 Mon Sep 17 00:00:00 2001 From: Hadrien de Cuzey Date: Wed, 26 Jul 2017 19:08:15 +0300 Subject: [PATCH 1/3] chore(lint): Add missing new lines --- src/index.ts | 2 +- src/types.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9446525..c999ce8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -125,4 +125,4 @@ export default function switchPath( }); return validate({sourcePath, matchedPath, matchedValue, routes}); -} \ No newline at end of file +} diff --git a/src/types.ts b/src/types.ts index 4eb6065..625d540 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,4 +5,4 @@ export interface RouteDefinitions extends Object { export interface SwitchPathReturn { path: string | null; value: any | null; -} \ No newline at end of file +} From 30b29f7993cabdcdb814f0365626092ec5616bb5 Mon Sep 17 00:00:00 2001 From: Hadrien de Cuzey Date: Wed, 26 Jul 2017 19:16:10 +0300 Subject: [PATCH 2/3] chore(dependencies): Set the maximum version of typescript so that the tests can pass --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5097418..81e910d 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "mocha": "^2.5.3", "ts-node": "^1.7.2", "tslint": "^4.0.2", - "typescript": "^2.1.4", + "typescript": "^2.3.4", "uglify-js": "^2.7.5", "validate-commit-msg": "^2.8.2" }, From 67c71df1b24dcb5cf9ad568413e05b70e7e1f638 Mon Sep 17 00:00:00 2001 From: Hadrien de Cuzey Date: Wed, 26 Jul 2017 19:27:58 +0300 Subject: [PATCH 3/3] fix(#16): edge case on $ --- src/index.ts | 22 ++++++++++++++++++++-- test/index.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index c999ce8..b270d3d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -80,8 +80,13 @@ function validate({sourcePath, matchedPath, matchedValue, routes}: ValidationObj let path = matchedPath ? validatePath(sourcePath, matchedPath) : null; let value = matchedValue; if (!path) { - path = routes[`*`] ? sourcePath : null; - value = path ? routes[`*`] : null; + if (sourcePath === `/`) { + path = routes[`*`] ? sourcePath : null; + value = path ? routes[`/$`] : null; + } else { + path = routes[`*`] ? sourcePath : null; + value = path ? routes[`*`] : null; + } } return {path, value}; } @@ -95,6 +100,19 @@ export default function switchPath( let matchedValue: string | null = null; traverseRoutes(routes, function matchPattern(pattern) { + if (pattern[pattern.length - 1] === `$`) { + const realPattern = pattern.split(`/$`).join(``); + if (sourcePath.search(realPattern) === 0 && + betterMatch(pattern, matchedPath) || + sourcePath.search(realPattern + `/`) && + betterMatch(pattern, matchedPath)) + { + matchedPath = realPattern; + matchedValue = routes[pattern]; + } + return; + } + if (sourcePath.search(pattern) === 0 && betterMatch(pattern, matchedPath)) { matchedPath = pattern; matchedValue = routes[pattern]; diff --git a/test/index.ts b/test/index.ts index 6d66955..e12bd54 100644 --- a/test/index.ts +++ b/test/index.ts @@ -203,6 +203,38 @@ describe('switchPath basic usage', () => { assert.strictEqual(path, '/1736'); assert.strictEqual(value, 'id is 1736'); }); + + it('should match routes explicitly using `$`', () => { + const { path, value } = switchPath('/', { + '/$': 123, + '/other': 456, + '*': 'not found route', + }); + + assert.strictEqual(path, '/'); + assert.strictEqual(value, 123); + }); + + it('should match routes explicitly using `$`, even if they come after a potential match', () => { + const { path, value } = switchPath('/other', { + '/': 123, + '/other/$': 456, + }); + + assert.strictEqual(path, '/other'); + assert.strictEqual(value, 456); + }); + + it('should allow opting out of partial matching for routes using `$`', () => { + const {path, value} = switchPath('/random', { + '/$': 123, + '/other': 456, + '*': 'not found route', + }); + + assert.strictEqual(path, '/random'); + assert.strictEqual(value, 'not found route'); + }); }); describe('switchPath corner cases', () => {