Skip to content

Commit

Permalink
added caseSensitive and pathToRegexpOptions to RouteConfig and pathTo…
Browse files Browse the repository at this point in the history
…RegexpOptions to RouteRecord. Used to support senstive matches and pass along more options to pathToRegexp. vuejs#1214
  • Loading branch information
Kevin Smithson committed Feb 28, 2017
1 parent 656d117 commit efbe556
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
14 changes: 14 additions & 0 deletions examples/redirect/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ const router = new VueRouter({
// redirect with params
{ path: '/redirect-with-params/:id', redirect: '/with-params/:id' },

// redirect with caseSensitive
{ path: '/foobar', redirect: '/FooBar', caseSensitive: true },

// redirect with pathToRegexpOptions
{ path: '/FooBar', redirect: '/bar', pathToRegexpOptions: { sensitive: true }},

// catch all redirect
{ path: '*', redirect: '/' }
]
Expand Down Expand Up @@ -96,6 +102,14 @@ new Vue({
<li><router-link to="/redirect-with-params/123">
/redirect-with-params/123 (redirects to /with-params/123)
</router-link></li>
<li><router-link to="/foobar">
/foobar (redirects to /FooBar)
</router-link></li>
<li><router-link to="/FooBar">
/FooBar (redirects to /bar)
</router-link></li>
<li><router-link to="/not-found">
/not-found (redirects to /)
Expand Down
9 changes: 9 additions & 0 deletions flow/declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ declare type RouterOptions = {

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

declare type PathToRegexpOptions = {
sensitive?: boolean,
strict?: boolean,
end?: boolean
}

declare type RouteConfig = {
path: string;
name?: string;
Expand All @@ -40,6 +46,8 @@ declare type RouteConfig = {
beforeEnter?: NavigationGuard;
meta?: any;
props?: boolean | Object | Function;
caseSensitive?: boolean;
pathToRegexpOptions?: PathToRegexpOptions;
}

declare type RouteRecord = {
Expand All @@ -53,6 +61,7 @@ declare type RouteRecord = {
beforeEnter: ?NavigationGuard;
meta: any;
props: boolean | Object | Function | Dictionary<boolean | Object | Function>;
pathToRegexpOptions: PathToRegexpOptions;
}

declare type Location = {
Expand Down
12 changes: 7 additions & 5 deletions src/create-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
if (process.env.NODE_ENV !== 'production') {
warn(record, `Route with name '${name}' does not exist`)
}
const paramNames = getRouteRegex(record.path).keys
const paramNames = getRouteRegex(record.path, record.pathToRegexpOptions).keys
.filter(key => !key.optional)
.map(key => key.name)

Expand All @@ -55,8 +55,9 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
} else if (location.path) {
location.params = {}
for (const path in pathMap) {
if (matchRoute(path, location.params, location.path)) {
return _createRoute(pathMap[path], location, redirectedFrom)
const record = pathMap[path]
if (matchRoute(path, location.params, location.path, record.pathToRegexpOptions)) {
return _createRoute(record, location, redirectedFrom)
}
}
}
Expand Down Expand Up @@ -164,9 +165,10 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
function matchRoute (
path: string,
params: Object,
pathname: string
pathname: string,
pathToRegexpOptions: PathToRegexpOptions
): boolean {
const { regexp, keys } = getRouteRegex(path)
const { regexp, keys } = getRouteRegex(path, pathToRegexpOptions)
const m = pathname.match(regexp)

if (!m) {
Expand Down
9 changes: 8 additions & 1 deletion src/create-route-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ function addRouteRecord (
)
}

const pathToRegexpOptions: PathToRegexpOptions = route.pathToRegexpOptions || {}

if (typeof route.caseSensitive === 'boolean') {
pathToRegexpOptions.sensitive = route.caseSensitive
}

const record: RouteRecord = {
path: normalizePath(path, parent),
components: route.components || { default: route.component },
Expand All @@ -55,7 +61,8 @@ function addRouteRecord (
? {}
: route.components
? route.props
: { default: route.props }
: { default: route.props },
pathToRegexpOptions: pathToRegexpOptions
}

if (route.children) {
Expand Down
4 changes: 2 additions & 2 deletions src/util/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const regexpCache: {
}
} = Object.create(null)

export function getRouteRegex (path: string): Object {
export function getRouteRegex (path: string, options: PathToRegexpOptions): Object {
const hit = regexpCache[path]
let keys, regexp

Expand All @@ -19,7 +19,7 @@ export function getRouteRegex (path: string): Object {
regexp = hit.regexp
} else {
keys = []
regexp = Regexp(path, keys)
regexp = Regexp(path, keys, options)
regexpCache[path] = { keys, regexp }
}

Expand Down

0 comments on commit efbe556

Please sign in to comment.