return path.replace(TRAILING_SLASH_RE, '$1')
}
-// TODO: this should now be used by the router
-// function applyToParam(
-// fn: (v: string) => string,
-// params: PathParams
-// ): PathParams {
-// const newParams: PathParams = {}
-
-// // TODO: could also normalize values like numbers and stuff
-// for (const key in params) {
-// const value = params[key]
-// newParams[key] = Array.isArray(value) ? value.map(fn) : fn(value)
-// }
-
-// return newParams
-// }
-
export function createRouterMatcher(
routes: RouteRecord[],
globalOptions: PathParserOptions
Lazy,
TODO,
Immutable,
- RouteParams,
} from './types'
import { RouterHistory, parseURL, stringifyURL } from './history/common'
import {
NavigationGuardRedirect,
NavigationAborted,
} from './errors'
-import { extractComponentsGuards, guardToPromiseFn } from './utils'
+import {
+ extractComponentsGuards,
+ guardToPromiseFn,
+ applyToParams,
+} from './utils'
import { useCallbacks } from './utils/callbacks'
import { encodeParam, decode } from './utils/encoding'
import { normalizeQuery, parseQuery, stringifyQuery } from './utils/query'
return history.base + to.fullPath
}
- // TODO: move these two somewhere else
- function encodeParams(params: RouteParams): RouteParams {
- const newParams: RouteParams = {}
- for (const key in params) {
- const value = params[key]
- newParams[key] = Array.isArray(value)
- ? value.map(encodeParam)
- : encodeParam(value)
- }
-
- return newParams
- }
-
- function decodeParams(params: RouteParams): RouteParams {
- const newParams: RouteParams = {}
- for (const key in params) {
- const value = params[key]
- newParams[key] = Array.isArray(value) ? value.map(decode) : decode(value)
- }
-
- return newParams
- }
+ const encodeParams = applyToParams.bind(null, encodeParam)
+ const decodeParams = applyToParams.bind(null, decode)
function resolve(
to: RouteLocation,
currentRoute.value = markNonReactive(toLocation)
// TODO: this doesn't work on first load. Moving it to RouterView could allow automatically handling transitions too maybe
// TODO: refactor with a state getter
- const state = isPush ? {} : window.history.state
+ const state = isPush || !isClient ? {} : window.history.state
handleScroll(toLocation, from, state && state.scroll).catch(err =>
triggerError(err, false)
)
-import { RouteLocationNormalized } from '../types'
+import { RouteLocationNormalized, RouteParams } from '../types'
import { guardToPromiseFn } from './guardToPromiseFn'
import { RouteRecordMatched } from '../matcher/types'
return guards
}
+
+export function applyToParams(
+ fn: (v: string) => string,
+ params: RouteParams
+): RouteParams {
+ const newParams: RouteParams = {}
+
+ for (const key in params) {
+ const value = params[key]
+ newParams[key] = Array.isArray(value) ? value.map(fn) : fn(value)
+ }
+
+ return newParams
+}