From cb4f24b2a0350b79b05bce54729b5c20e5e65020 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 5 Feb 2020 15:08:29 +0100 Subject: [PATCH] refactor: applyToParams --- __tests__/mount.ts | 1 - src/matcher/index.ts | 16 ---------------- src/router.ts | 33 ++++++++------------------------- src/utils/index.ts | 16 +++++++++++++++- 4 files changed, 23 insertions(+), 43 deletions(-) diff --git a/__tests__/mount.ts b/__tests__/mount.ts index ddebd184..782570ee 100644 --- a/__tests__/mount.ts +++ b/__tests__/mount.ts @@ -11,7 +11,6 @@ export function mount( }, rootProps = {} ) { - // TODO: update with alpha-4 const { template, components, ...ComponentWithoutTemplate } = Component const app = createApp(ComponentWithoutTemplate as any, rootProps) diff --git a/src/matcher/index.ts b/src/matcher/index.ts index a225c58c..3de98cf8 100644 --- a/src/matcher/index.ts +++ b/src/matcher/index.ts @@ -27,22 +27,6 @@ function removeTrailingSlash(path: string): string { 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 diff --git a/src/router.ts b/src/router.ts index 0c1cf823..aed0b767 100644 --- a/src/router.ts +++ b/src/router.ts @@ -9,7 +9,6 @@ import { Lazy, TODO, Immutable, - RouteParams, } from './types' import { RouterHistory, parseURL, stringifyURL } from './history/common' import { @@ -23,7 +22,11 @@ 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' @@ -94,28 +97,8 @@ export function createRouter({ 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, @@ -336,7 +319,7 @@ export function createRouter({ 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) ) diff --git a/src/utils/index.ts b/src/utils/index.ts index 04d5aee2..d9c8169e 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,4 +1,4 @@ -import { RouteLocationNormalized } from '../types' +import { RouteLocationNormalized, RouteParams } from '../types' import { guardToPromiseFn } from './guardToPromiseFn' import { RouteRecordMatched } from '../matcher/types' @@ -35,3 +35,17 @@ export async function extractComponentsGuards( 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 +} -- 2.47.3