Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix case when routes have both '/' and '*' #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
24 changes: 21 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}
Expand All @@ -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];
Expand Down Expand Up @@ -125,4 +143,4 @@ export default function switchPath(
});

return validate({sourcePath, matchedPath, matchedValue, routes});
}
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export interface RouteDefinitions extends Object {
export interface SwitchPathReturn {
path: string | null;
value: any | null;
}
}
32 changes: 32 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down