From: Eduardo San Martin Morote Date: Thu, 2 Apr 2020 08:21:52 +0000 (+0200) Subject: refactor: create utils/location.ts X-Git-Tag: v4.0.0-alpha.5~41 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=361e727f92f8be641cf429cf6c6112de9f6af32c;p=thirdparty%2Fvuejs%2Frouter.git refactor: create utils/location.ts --- diff --git a/__tests__/history/locationUtils.spec.ts b/__tests__/history/locationUtils.spec.ts deleted file mode 100644 index 35bf0b1b..00000000 --- a/__tests__/history/locationUtils.spec.ts +++ /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') - }) - }) -}) diff --git a/__tests__/url.spec.ts b/__tests__/url.spec.ts index 0e3b2ca0..3df0f73b 100644 --- a/__tests__/url.spec.ts +++ b/__tests__/url.spec.ts @@ -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') + }) +}) diff --git a/src/history/common.ts b/src/history/common.ts index 0eb47fff..dcb87231 100644 --- a/src/history/common.ts +++ b/src/history/common.ts @@ -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 -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 { diff --git a/src/history/html5.ts b/src/history/html5.ts index ffc79384..523a4f3e 100644 --- a/src/history/html5.ts +++ b/src/history/html5.ts @@ -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 diff --git a/src/router.ts b/src/router.ts index 38c9865d..c4e6ccb8 100644 --- a/src/router.ts +++ b/src/router.ts @@ -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 index 00000000..1dc7773c --- /dev/null +++ b/src/utils/location.ts @@ -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, '') || '/' +}