]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: create utils/location.ts
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 2 Apr 2020 08:21:52 +0000 (10:21 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 2 Apr 2020 08:21:52 +0000 (10:21 +0200)
__tests__/history/locationUtils.spec.ts [deleted file]
__tests__/url.spec.ts
src/history/common.ts
src/history/html5.ts
src/router.ts
src/utils/location.ts [new file with mode: 0644]

diff --git a/__tests__/history/locationUtils.spec.ts b/__tests__/history/locationUtils.spec.ts
deleted file mode 100644 (file)
index 35bf0b1..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-import { stripBase } from '../../src/history/common'
-import { createDom } from '../utils'
-
-describe('History Location Utils', () => {
-  beforeAll(() => {
-    createDom()
-  })
-
-  describe('stripBase', () => {
-    it('returns the pathname if no base', () => {
-      expect(stripBase('', '')).toBe('')
-      expect(stripBase('/', '')).toBe('/')
-      expect(stripBase('/thing', '')).toBe('/thing')
-    })
-
-    it('returns the pathname without the base', () => {
-      expect(stripBase('/base', '/base')).toBe('/')
-      expect(stripBase('/base/', '/base')).toBe('/')
-      expect(stripBase('/base/foo', '/base')).toBe('/foo')
-    })
-  })
-})
index 0e3b2ca09a1201e8c89c15c3e384e5949cb1a159..3df0f73bb9e8286b4c9cf2341bf13e76dc200000 100644 (file)
@@ -1,9 +1,10 @@
+import { normalizeHistoryLocation as normalizeLocation } from '../src/history/common'
+import { parseQuery, stringifyQuery } from '../src/utils/query'
 import {
   parseURL as originalParseURL,
   stringifyURL as originalStringifyURL,
-  normalizeHistoryLocation as normalizeLocation,
-} from '../src/history/common'
-import { parseQuery, stringifyQuery } from '../src/utils/query'
+  stripBase,
+} from '../src/utils/location'
 
 describe('parseURL', () => {
   let parseURL = originalParseURL.bind(null, parseQuery)
@@ -145,3 +146,17 @@ describe('normalizeLocation', () => {
     ).toEqual({ fullPath: '/foo' })
   })
 })
+
+describe('stripBase', () => {
+  it('returns the pathname if no base', () => {
+    expect(stripBase('', '')).toBe('')
+    expect(stripBase('/', '')).toBe('/')
+    expect(stripBase('/thing', '')).toBe('/thing')
+  })
+
+  it('returns the pathname without the base', () => {
+    expect(stripBase('/base', '/base')).toBe('/')
+    expect(stripBase('/base/', '/base')).toBe('/')
+    expect(stripBase('/base/foo', '/base')).toBe('/foo')
+  })
+})
index 0eb47fffe5235d725b9610388bd8bf4d5810b4f5..dcb8723193b8c54eb85df5791aebcb5535f47e4d 100644 (file)
@@ -1,5 +1,3 @@
-import { LocationQueryRaw, LocationQuery } from '../utils/query'
-
 interface HistoryLocation {
   fullPath: string
   state?: HistoryState
@@ -7,18 +5,6 @@ interface HistoryLocation {
 
 export type RawHistoryLocation = HistoryLocation | string
 export type HistoryLocationNormalized = Pick<HistoryLocation, 'fullPath'>
-export interface LocationPartial {
-  path: string
-  query?: LocationQueryRaw
-  hash?: string
-}
-export interface LocationNormalized {
-  path: string
-  fullPath: string
-  hash: string
-  query: LocationQuery
-}
-
 // pushState clones the state passed and do not accept everything
 // it doesn't accept symbols, nor functions as values. It also ignores Symbols as keys
 type HistoryStateValue =
@@ -87,75 +73,6 @@ export interface RouterHistory {
 
 // Generic utils
 
-/**
- * Transforms an URI into a normalized history location
- * @param parseQuery
- * @param location - URI to normalize
- * @returns a normalized history location
- */
-export function parseURL(
-  parseQuery: (search: string) => LocationQuery,
-  location: string
-): LocationNormalized {
-  let path = '',
-    query: LocationQuery = {},
-    searchString = '',
-    hash = ''
-
-  // Could use URL and URLSearchParams but IE 11 doesn't support it
-  const searchPos = location.indexOf('?')
-  const hashPos = location.indexOf('#', searchPos > -1 ? searchPos : 0)
-
-  if (searchPos > -1) {
-    path = location.slice(0, searchPos)
-    searchString = location.slice(
-      searchPos + 1,
-      hashPos > -1 ? hashPos : location.length
-    )
-
-    query = parseQuery(searchString)
-  }
-
-  if (hashPos > -1) {
-    path = path || location.slice(0, hashPos)
-    // keep the # character
-    hash = location.slice(hashPos, location.length)
-  }
-
-  // no search and no query
-  path = path || location
-
-  return {
-    fullPath: location,
-    path,
-    query,
-    hash,
-  }
-}
-
-/**
- * Stringify a URL object
- * @param stringifyQuery
- * @param location
- */
-export function stringifyURL(
-  stringifyQuery: (query: LocationQueryRaw) => string,
-  location: LocationPartial
-): string {
-  let query: string = location.query ? stringifyQuery(location.query) : ''
-  return location.path + (query && '?') + query + (location.hash || '')
-}
-
-/**
- * Strips off the base from the beginning of a location.pathname
- * @param pathname - location.pathname
- * @param base - base to strip off
- */
-export function stripBase(pathname: string, base: string): string {
-  if (!base || pathname.indexOf(base) !== 0) return pathname
-  return pathname.replace(base, '') || '/'
-}
-
 export function normalizeHistoryLocation(
   location: RawHistoryLocation
 ): HistoryLocationNormalized {
index ffc7938492f2ccf07e6a83891ec28fcbdcd8fda2..523a4f3e176a9cbf745996c338a4bc135f3f8e99 100644 (file)
@@ -1,7 +1,6 @@
 import {
   RouterHistory,
   NavigationCallback,
-  stripBase,
   NavigationType,
   NavigationDirection,
   HistoryLocationNormalized,
@@ -12,6 +11,7 @@ import {
 } from './common'
 import { computeScrollPosition, ScrollToPosition } from '../utils/scroll'
 import { warn } from 'vue'
+import { stripBase } from '../utils/location'
 
 type PopStateListener = (this: Window, ev: PopStateEvent) => any
 
index 38c9865dc73fac2e83b02d7c3348edbe59f865ed..c4e6ccb8171b5398ef443887f08c249ae7864448 100644 (file)
@@ -11,12 +11,7 @@ import {
   RouteLocationNormalizedLoaded,
   RouteLocation,
 } from './types'
-import {
-  RouterHistory,
-  parseURL,
-  stringifyURL,
-  HistoryState,
-} from './history/common'
+import { RouterHistory, HistoryState } from './history/common'
 import {
   ScrollToPosition,
   ScrollPosition,
@@ -53,6 +48,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'
 
 /**
  * Internal type to define an ErrorHandler
diff --git a/src/utils/location.ts b/src/utils/location.ts
new file mode 100644 (file)
index 0000000..1dc7773
--- /dev/null
@@ -0,0 +1,86 @@
+import { LocationQuery, LocationQueryRaw } from './query'
+
+export interface LocationNormalized {
+  path: string
+  fullPath: string
+  hash: string
+  query: LocationQuery
+}
+
+export interface LocationPartial {
+  path: string
+  query?: LocationQueryRaw
+  hash?: string
+}
+
+/**
+ * Transforms an URI into a normalized history location
+ *
+ * @param parseQuery
+ * @param location - URI to normalize
+ * @returns a normalized history location
+ */
+export function parseURL(
+  parseQuery: (search: string) => LocationQuery,
+  location: string
+): LocationNormalized {
+  let path = '',
+    query: LocationQuery = {},
+    searchString = '',
+    hash = ''
+
+  // Could use URL and URLSearchParams but IE 11 doesn't support it
+  const searchPos = location.indexOf('?')
+  const hashPos = location.indexOf('#', searchPos > -1 ? searchPos : 0)
+
+  if (searchPos > -1) {
+    path = location.slice(0, searchPos)
+    searchString = location.slice(
+      searchPos + 1,
+      hashPos > -1 ? hashPos : location.length
+    )
+
+    query = parseQuery(searchString)
+  }
+
+  if (hashPos > -1) {
+    path = path || location.slice(0, hashPos)
+    // keep the # character
+    hash = location.slice(hashPos, location.length)
+  }
+
+  // no search and no query
+  path = path || location
+
+  return {
+    fullPath: location,
+    path,
+    query,
+    hash,
+  }
+}
+
+/**
+ * Stringifies a URL object
+ *
+ * @param stringifyQuery
+ * @param location
+ */
+export function stringifyURL(
+  stringifyQuery: (query: LocationQueryRaw) => string,
+  location: LocationPartial
+): string {
+  let query: string = location.query ? stringifyQuery(location.query) : ''
+  return location.path + (query && '?') + query + (location.hash || '')
+}
+
+/**
+ * Strips off the base from the beginning of a location.pathname
+ *
+ * @param pathname - location.pathname
+ * @param base - base to strip off
+ */
+export function stripBase(pathname: string, base: string): string {
+  if (!base || pathname.indexOf(base) !== 0) return pathname
+  return pathname.replace(base, '') || '/'
+}