+++ /dev/null
-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')
- })
- })
-})
+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)
).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')
+ })
+})
-import { LocationQueryRaw, LocationQuery } from '../utils/query'
-
interface HistoryLocation {
fullPath: string
state?: HistoryState
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 =
// 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 {
import {
RouterHistory,
NavigationCallback,
- stripBase,
NavigationType,
NavigationDirection,
HistoryLocationNormalized,
} from './common'
import { computeScrollPosition, ScrollToPosition } from '../utils/scroll'
import { warn } from 'vue'
+import { stripBase } from '../utils/location'
type PopStateListener = (this: Window, ev: PopStateEvent) => any
RouteLocationNormalizedLoaded,
RouteLocation,
} from './types'
-import {
- RouterHistory,
- parseURL,
- stringifyURL,
- HistoryState,
-} from './history/common'
+import { RouterHistory, HistoryState } from './history/common'
import {
ScrollToPosition,
ScrollPosition,
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
--- /dev/null
+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, '') || '/'
+}