diff --git a/packages/inula-router/src/history/utils.ts b/packages/inula-router/src/history/utils.ts index cfa49b7a..27447967 100644 --- a/packages/inula-router/src/history/utils.ts +++ b/packages/inula-router/src/history/utils.ts @@ -28,31 +28,32 @@ export function createPath(path: Partial): string { } export function parsePath(url: string): Partial { - if (!url) { - return {}; - } - let parsedPath: Partial = {}; + const parsedPath: Partial = { + pathname: url || '/', + search: '', + hash: '', + }; - let hashIdx = url.indexOf('#'); + const hashIdx = url.indexOf('#'); if (hashIdx > -1) { - parsedPath.hash = url.substring(hashIdx); + const hash = url.substring(hashIdx); + parsedPath.hash = hash === '#' ? '' : hash; url = url.substring(0, hashIdx); } - let searchIdx = url.indexOf('?'); + const searchIdx = url.indexOf('?'); if (searchIdx > -1) { - parsedPath.search = url.substring(searchIdx); + const search = url.substring(searchIdx); + parsedPath.search = search === '?' ? '' : search; url = url.substring(0, searchIdx); } - if (url) { - parsedPath.pathname = url; - } + parsedPath.pathname = url; return parsedPath; } export function createLocation(current: string | Location, to: To, state?: S, key?: string): Readonly> { - let pathname = typeof current === 'string' ? current : current.pathname; - let urlObj = typeof to === 'string' ? parsePath(to) : to; + const pathname = typeof current === 'string' ? current : current.pathname; + const urlObj = typeof to === 'string' ? parsePath(to) : to; // 随机key长度取6 const getRandKey = genRandomKey(6); const location = { @@ -64,7 +65,13 @@ export function createLocation(current: string | Location, to: To, state?: S, ...urlObj, }; if (!location.pathname) { - location.pathname = '/'; + location.pathname = pathname ? pathname : '/'; + } + if (location.search && location.search[0] !== '?') { + location.search = '?' + location.search; + } + if (location.hash && location.hash[0] !== '#') { + location.hash = '#' + location.hash; } return location; } @@ -95,7 +102,7 @@ export function normalizeSlash(path: string): string { return tempPath; } -export function hasBasename(path: string, prefix: string): Boolean { +export function hasBasename(path: string, prefix: string): boolean { return ( path.toLowerCase().indexOf(prefix.toLowerCase()) === 0 && ['/', '?', '#', ''].includes(path.charAt(prefix.length)) ); @@ -109,12 +116,12 @@ export function stripBasename(path: string, prefix: string): string { export function createMemoryRecord(initVal: S, fn: (arg: S) => T) { let visitedRecord: T[] = [fn(initVal)]; - function getDelta(toKey: S, fromKey: S): number { - let toIdx = visitedRecord.lastIndexOf(fn(toKey)); + function getDelta(to: S, form: S): number { + let toIdx = visitedRecord.lastIndexOf(fn(to)); if (toIdx === -1) { toIdx = 0; } - let fromIdx = visitedRecord.lastIndexOf(fn(fromKey)); + let fromIdx = visitedRecord.lastIndexOf(fn(form)); if (fromIdx === -1) { fromIdx = 0; }