From ca95567e08d8de4adb518a6e23a4dd590d23dd36 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 6 Dec 2024 09:40:16 +0100 Subject: [PATCH] refactor: simplify parseURL --- packages/router/src/location.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/router/src/location.ts b/packages/router/src/location.ts index a8d5f919..ad317332 100644 --- a/packages/router/src/location.ts +++ b/packages/router/src/location.ts @@ -52,19 +52,19 @@ export function parseURL( // NOTE: we could use URL and URLSearchParams but they are 2 to 5 times slower than this method const hashPos = location.indexOf('#') - // let searchPos = location.indexOf('?') - let searchPos = - hashPos >= 0 - ? // find the query string before the hash to avoid including a ? in the hash - // e.g. /foo#hash?query -> has no query - location.lastIndexOf('?', hashPos) - : location.indexOf('?') + let searchPos = location.indexOf('?') + + // This ensures that the ? is not part of the hash + // e.g. /foo#hash?query -> has no query + searchPos = hashPos >= 0 && searchPos > hashPos ? -1 : searchPos if (searchPos >= 0) { path = location.slice(0, searchPos) - searchString = - '?' + - location.slice(searchPos + 1, hashPos > 0 ? hashPos : location.length) + // keep the ? char + searchString = location.slice( + searchPos, + hashPos > 0 ? hashPos : location.length + ) query = parseQuery(searchString) } @@ -213,7 +213,7 @@ export function resolveRelativePath(to: string, from: string): string { return to } - // resolve '' with '/anything' -> '/anything' + // resolve to: '' with from: '/anything' -> '/anything' if (!to) return from const fromSegments = from.split('/') -- 2.47.2