Skip to content

Commit efbe556

Browse files
author
Kevin Smithson
committed
added caseSensitive and pathToRegexpOptions to RouteConfig and pathToRegexpOptions to RouteRecord. Used to support senstive matches and pass along more options to pathToRegexp. #1214
1 parent 656d117 commit efbe556

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

examples/redirect/app.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ const router = new VueRouter({
5050
// redirect with params
5151
{ path: '/redirect-with-params/:id', redirect: '/with-params/:id' },
5252

53+
// redirect with caseSensitive
54+
{ path: '/foobar', redirect: '/FooBar', caseSensitive: true },
55+
56+
// redirect with pathToRegexpOptions
57+
{ path: '/FooBar', redirect: '/bar', pathToRegexpOptions: { sensitive: true }},
58+
5359
// catch all redirect
5460
{ path: '*', redirect: '/' }
5561
]
@@ -96,6 +102,14 @@ new Vue({
96102
<li><router-link to="/redirect-with-params/123">
97103
/redirect-with-params/123 (redirects to /with-params/123)
98104
</router-link></li>
105+
106+
<li><router-link to="/foobar">
107+
/foobar (redirects to /FooBar)
108+
</router-link></li>
109+
110+
<li><router-link to="/FooBar">
111+
/FooBar (redirects to /bar)
112+
</router-link></li>
99113
100114
<li><router-link to="/not-found">
101115
/not-found (redirects to /)

flow/declarations.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ declare type RouterOptions = {
2929

3030
declare type RedirectOption = RawLocation | ((to: Route) => RawLocation)
3131

32+
declare type PathToRegexpOptions = {
33+
sensitive?: boolean,
34+
strict?: boolean,
35+
end?: boolean
36+
}
37+
3238
declare type RouteConfig = {
3339
path: string;
3440
name?: string;
@@ -40,6 +46,8 @@ declare type RouteConfig = {
4046
beforeEnter?: NavigationGuard;
4147
meta?: any;
4248
props?: boolean | Object | Function;
49+
caseSensitive?: boolean;
50+
pathToRegexpOptions?: PathToRegexpOptions;
4351
}
4452

4553
declare type RouteRecord = {
@@ -53,6 +61,7 @@ declare type RouteRecord = {
5361
beforeEnter: ?NavigationGuard;
5462
meta: any;
5563
props: boolean | Object | Function | Dictionary<boolean | Object | Function>;
64+
pathToRegexpOptions: PathToRegexpOptions;
5665
}
5766

5867
declare type Location = {

src/create-matcher.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
3232
if (process.env.NODE_ENV !== 'production') {
3333
warn(record, `Route with name '${name}' does not exist`)
3434
}
35-
const paramNames = getRouteRegex(record.path).keys
35+
const paramNames = getRouteRegex(record.path, record.pathToRegexpOptions).keys
3636
.filter(key => !key.optional)
3737
.map(key => key.name)
3838

@@ -55,8 +55,9 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
5555
} else if (location.path) {
5656
location.params = {}
5757
for (const path in pathMap) {
58-
if (matchRoute(path, location.params, location.path)) {
59-
return _createRoute(pathMap[path], location, redirectedFrom)
58+
const record = pathMap[path]
59+
if (matchRoute(path, location.params, location.path, record.pathToRegexpOptions)) {
60+
return _createRoute(record, location, redirectedFrom)
6061
}
6162
}
6263
}
@@ -164,9 +165,10 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
164165
function matchRoute (
165166
path: string,
166167
params: Object,
167-
pathname: string
168+
pathname: string,
169+
pathToRegexpOptions: PathToRegexpOptions
168170
): boolean {
169-
const { regexp, keys } = getRouteRegex(path)
171+
const { regexp, keys } = getRouteRegex(path, pathToRegexpOptions)
170172
const m = pathname.match(regexp)
171173

172174
if (!m) {

src/create-route-map.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ function addRouteRecord (
4141
)
4242
}
4343

44+
const pathToRegexpOptions: PathToRegexpOptions = route.pathToRegexpOptions || {}
45+
46+
if (typeof route.caseSensitive === 'boolean') {
47+
pathToRegexpOptions.sensitive = route.caseSensitive
48+
}
49+
4450
const record: RouteRecord = {
4551
path: normalizePath(path, parent),
4652
components: route.components || { default: route.component },
@@ -55,7 +61,8 @@ function addRouteRecord (
5561
? {}
5662
: route.components
5763
? route.props
58-
: { default: route.props }
64+
: { default: route.props },
65+
pathToRegexpOptions: pathToRegexpOptions
5966
}
6067

6168
if (route.children) {

src/util/params.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const regexpCache: {
1010
}
1111
} = Object.create(null)
1212

13-
export function getRouteRegex (path: string): Object {
13+
export function getRouteRegex (path: string, options: PathToRegexpOptions): Object {
1414
const hit = regexpCache[path]
1515
let keys, regexp
1616

@@ -19,7 +19,7 @@ export function getRouteRegex (path: string): Object {
1919
regexp = hit.regexp
2020
} else {
2121
keys = []
22-
regexp = Regexp(path, keys)
22+
regexp = Regexp(path, keys, options)
2323
regexpCache[path] = { keys, regexp }
2424
}
2525

0 commit comments

Comments
 (0)