]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: applyToParams
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 5 Feb 2020 14:08:29 +0000 (15:08 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 5 Feb 2020 14:08:29 +0000 (15:08 +0100)
__tests__/mount.ts
src/matcher/index.ts
src/router.ts
src/utils/index.ts

index ddebd18412dd8d0d3badccbc4c6b82cf3085a670..782570ee9b600fadff4215baafa8fcdf7a6647ba 100644 (file)
@@ -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)
index a225c58ca4a64fb48675c1a08782263cb393023f..3de98cf8b571b387709d41b1be6c0bb62c1e8ec0 100644 (file)
@@ -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
index 0c1cf8234c695dc6293aedc25736a6825378a4e4..aed0b767abfb77b428271ddd10493050d4bf13b6 100644 (file)
@@ -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)
     )
index 04d5aee2b6bb8dea67c560fccb4389aa000e272f..d9c8169e23b21b3114b2f0596469cf3c9018bd95 100644 (file)
@@ -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
+}