]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor(utils): move location utils to its file
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 21 Apr 2020 16:33:57 +0000 (18:33 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 21 Apr 2020 16:33:57 +0000 (18:33 +0200)
__tests__/location.spec.ts
src/components/Link.ts
src/router.ts
src/utils/index.ts
src/utils/location.ts

index e5a9f93e6999d10a606a3cb886310c86150eee01..07222f32770451c68968c3574d200e6850984c4b 100644 (file)
@@ -4,8 +4,8 @@ import {
   parseURL as originalParseURL,
   stringifyURL as originalStringifyURL,
   stripBase,
+  isSameLocationObject,
 } from '../src/utils/location'
-import { isSameLocationObject } from '../src/utils'
 
 describe('parseURL', () => {
   let parseURL = originalParseURL.bind(null, parseQuery)
index d6295f34fc6e0593ce9623b86195f1711e5e354a..b83e8feae03d20463b668864841b144362ccdc3b 100644 (file)
@@ -9,7 +9,7 @@ import {
   Component,
 } from 'vue'
 import { RouteLocationRaw, VueUseOptions, RouteLocation } from '../types'
-import { isSameLocationObject, isSameRouteRecord } from '../utils'
+import { isSameLocationObject, isSameRouteRecord } from '../utils/location'
 import { routerKey, routeLocationKey } from '../utils/injectionSymbols'
 import { RouteRecord } from '../matcher/types'
 
index 3bb368419b9e7924de45fe25f64803bc7c5582b6..4063b382936cffb1a2038409e60bc0b57e7a23e2 100644 (file)
@@ -31,12 +31,7 @@ import {
   NavigationFailure,
   NavigationRedirectError,
 } from './errors'
-import {
-  applyToParams,
-  isSameRouteRecord,
-  isSameLocationObject,
-  isBrowser,
-} from './utils'
+import { applyToParams, isBrowser } from './utils'
 import { useCallbacks } from './utils/callbacks'
 import { encodeParam, decode } from './utils/encoding'
 import {
@@ -59,7 +54,7 @@ import { RouteRecord, RouteRecordNormalized } from './matcher/types'
 import { Link } from './components/Link'
 import { View } from './components/View'
 import { routerKey, routeLocationKey } from './utils/injectionSymbols'
-import { parseURL, stringifyURL } from './utils/location'
+import { parseURL, stringifyURL, isSameRouteLocation } from './utils/location'
 import { extractComponentsGuards, guardToPromiseFn } from './navigationGuards'
 
 /**
@@ -756,18 +751,3 @@ function extractChangingRecords(
 
   return [leavingRecords, updatingRecords, enteringRecords]
 }
-
-// TODO: move to utils and test
-function isSameRouteLocation(a: RouteLocation, b: RouteLocation): boolean {
-  let aLastIndex = a.matched.length - 1
-  let bLastIndex = b.matched.length - 1
-
-  return (
-    aLastIndex > -1 &&
-    aLastIndex === bLastIndex &&
-    isSameRouteRecord(a.matched[aLastIndex], b.matched[bLastIndex]) &&
-    isSameLocationObject(a.params, b.params) &&
-    isSameLocationObject(a.query, b.query) &&
-    a.hash === b.hash
-  )
-}
index 1b1f4658ce898f170a9153c1ff8eb53b9d07420c..139e3f567522a408f9763c03154bdd82f9db392c 100644 (file)
@@ -1,11 +1,4 @@
-import {
-  RouteLocationNormalized,
-  RouteParams,
-  RouteComponent,
-  RouteParamValue,
-} from '../types'
-import { RouteRecord } from '../matcher/types'
-import { LocationQueryValue } from './query'
+import { RouteParams, RouteComponent } from '../types'
 import { hasSymbol } from './injectionSymbols'
 
 export * from './env'
@@ -27,71 +20,3 @@ export function applyToParams(
 
   return newParams
 }
-
-export function isSameRouteRecord(a: RouteRecord, b: RouteRecord): boolean {
-  // since the original record has an undefined value for aliasOf
-  // but all aliases point to the original record, this will always compare
-  // the original record
-  return (a.aliasOf || a) === (b.aliasOf || b)
-}
-
-export function isSameLocationObject(
-  a: RouteLocationNormalized['query'],
-  b: RouteLocationNormalized['query']
-): boolean
-export function isSameLocationObject(
-  a: RouteLocationNormalized['params'],
-  b: RouteLocationNormalized['params']
-): boolean
-export function isSameLocationObject(
-  a: RouteLocationNormalized['query' | 'params'],
-  b: RouteLocationNormalized['query' | 'params']
-): boolean {
-  if (Object.keys(a).length !== Object.keys(b).length) return false
-
-  for (let key in a) {
-    if (!isSameLocationObjectValue(a[key], b[key])) return false
-  }
-
-  return true
-}
-
-function isSameLocationObjectValue(
-  a: LocationQueryValue | LocationQueryValue[],
-  b: LocationQueryValue | LocationQueryValue[]
-): boolean
-function isSameLocationObjectValue(
-  a: RouteParamValue | RouteParamValue[],
-  b: RouteParamValue | RouteParamValue[]
-): boolean
-function isSameLocationObjectValue(
-  a:
-    | LocationQueryValue
-    | LocationQueryValue[]
-    | RouteParamValue
-    | RouteParamValue[],
-  b:
-    | LocationQueryValue
-    | LocationQueryValue[]
-    | RouteParamValue
-    | RouteParamValue[]
-): boolean {
-  return Array.isArray(a)
-    ? isEquivalentArray(a, b)
-    : Array.isArray(b)
-    ? isEquivalentArray(b, a)
-    : a === b
-}
-
-/**
- * Check if two arrays are the same or if an array with one single entry is the
- * same as another primitive value. Used to check query and parameters
- *
- * @param a array of values
- * @param b array of values or a single value
- */
-function isEquivalentArray<T>(a: T[], b: T[] | T): boolean {
-  return Array.isArray(b)
-    ? a.length === b.length && a.every((value, i) => value === b[i])
-    : a.length === 1 && a[0] === b
-}
index 1dc7773c0c996d2ca0bf6649b232ccf2e0268a1e..fb82350dfef159eb52d138a9b4b4c1f0dfc1436d 100644 (file)
@@ -1,13 +1,27 @@
-import { LocationQuery, LocationQueryRaw } from './query'
+import { LocationQuery, LocationQueryRaw, LocationQueryValue } from './query'
+import {
+  RouteLocation,
+  RouteLocationNormalized,
+  RouteParamValue,
+} from '../types'
+import { RouteRecord } from '../matcher/types'
 
-export interface LocationNormalized {
+/**
+ * Location object returned by {@link `parseURL`}.
+ * @internal
+ */
+interface LocationNormalized {
   path: string
   fullPath: string
   hash: string
   query: LocationQuery
 }
 
-export interface LocationPartial {
+/**
+ * Location object accepted by {@link `stringifyURL`}.
+ * @internal
+ */
+interface LocationPartial {
   path: string
   query?: LocationQueryRaw
   hash?: string
@@ -84,3 +98,103 @@ export function stripBase(pathname: string, base: string): string {
   if (!base || pathname.indexOf(base) !== 0) return pathname
   return pathname.replace(base, '') || '/'
 }
+
+/**
+ * Checks if two RouteLocation are equal. This means that both locations are
+ * pointing towards the same {@link RouteRecord} and that all `params`, `query`
+ * parameters and `hash` are the same
+ *
+ * @param a first {@link RouteLocation}
+ * @param b second {@link RouteLocation}
+ */
+export function isSameRouteLocation(
+  a: RouteLocation,
+  b: RouteLocation
+): boolean {
+  let aLastIndex = a.matched.length - 1
+  let bLastIndex = b.matched.length - 1
+
+  return (
+    aLastIndex > -1 &&
+    aLastIndex === bLastIndex &&
+    isSameRouteRecord(a.matched[aLastIndex], b.matched[bLastIndex]) &&
+    isSameLocationObject(a.params, b.params) &&
+    isSameLocationObject(a.query, b.query) &&
+    a.hash === b.hash
+  )
+}
+
+/**
+ * Check if two `RouteRecords` are equal. Takes into account aliases: they are
+ * considered equal to the `RouteRecord` they are aliasing.
+ *
+ * @param a first {@link RouteRecord}
+ * @param b second {@link RouteRecord}
+ */
+export function isSameRouteRecord(a: RouteRecord, b: RouteRecord): boolean {
+  // since the original record has an undefined value for aliasOf
+  // but all aliases point to the original record, this will always compare
+  // the original record
+  return (a.aliasOf || a) === (b.aliasOf || b)
+}
+
+export function isSameLocationObject(
+  a: RouteLocationNormalized['query'],
+  b: RouteLocationNormalized['query']
+): boolean
+export function isSameLocationObject(
+  a: RouteLocationNormalized['params'],
+  b: RouteLocationNormalized['params']
+): boolean
+export function isSameLocationObject(
+  a: RouteLocationNormalized['query' | 'params'],
+  b: RouteLocationNormalized['query' | 'params']
+): boolean {
+  if (Object.keys(a).length !== Object.keys(b).length) return false
+
+  for (let key in a) {
+    if (!isSameLocationObjectValue(a[key], b[key])) return false
+  }
+
+  return true
+}
+
+function isSameLocationObjectValue(
+  a: LocationQueryValue | LocationQueryValue[],
+  b: LocationQueryValue | LocationQueryValue[]
+): boolean
+function isSameLocationObjectValue(
+  a: RouteParamValue | RouteParamValue[],
+  b: RouteParamValue | RouteParamValue[]
+): boolean
+function isSameLocationObjectValue(
+  a:
+    | LocationQueryValue
+    | LocationQueryValue[]
+    | RouteParamValue
+    | RouteParamValue[],
+  b:
+    | LocationQueryValue
+    | LocationQueryValue[]
+    | RouteParamValue
+    | RouteParamValue[]
+): boolean {
+  return Array.isArray(a)
+    ? isEquivalentArray(a, b)
+    : Array.isArray(b)
+    ? isEquivalentArray(b, a)
+    : a === b
+}
+
+/**
+ * Check if two arrays are the same or if an array with one single entry is the
+ * same as another primitive value. Used to check query and parameters
+ *
+ * @param a array of values
+ * @param b array of values or a single value
+ */
+function isEquivalentArray<T>(a: T[], b: T[] | T): boolean {
+  return Array.isArray(b)
+    ? a.length === b.length && a.every((value, i) => value === b[i])
+    : a.length === 1 && a[0] === b
+}