diff --git a/src/helpers.ts b/src/helpers.ts index b84db846..8a402646 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -7,7 +7,7 @@ export const isNonEmpty = (value: string): boolean => value !== ""; export const isParam = (value: string): boolean => value.startsWith(":"); export const ensureSlashPrefix = (value: string): string => - value[0] === "/" ? value : `/${value}`; + value[0] === "/" ? value : "/" + value; export const areParamsArrayEqual = (arrayA: string[], arrayB: string[]) => arrayA.length === arrayB.length && diff --git a/src/historyLite.ts b/src/historyLite.ts index e29a98fd..5ea2c2c3 100644 --- a/src/historyLite.ts +++ b/src/historyLite.ts @@ -83,21 +83,22 @@ export const createPath = ({ pathname, search }: Location) => { return output; }; -export const parsePath = (path: string): Location => { - let rest = path; +// rename this decodeRoute (encodeRoute should not exists) +export const parsePath = (path: string): Readonly => { + const hashIndex = path.indexOf("#"); - const output: Location = { pathname: "", search: "" }; - const hashIndex = rest.indexOf("#"); - const searchIndex = rest.indexOf("?"); + const cleanPath = ensureSlashPrefix( + hashIndex < 0 ? path : path.substring(0, hashIndex), + ); - if (hashIndex >= 0) { - rest = rest.substring(0, hashIndex); - } - if (searchIndex >= 0) { - output.search = rest.substring(searchIndex); - rest = rest.substring(0, searchIndex); + const searchIndex = cleanPath.indexOf("?"); + + if (searchIndex < 0) { + return { pathname: cleanPath, search: "" }; } - output.pathname = ensureSlashPrefix(rest); - return output; + return { + pathname: cleanPath.substring(0, searchIndex), + search: cleanPath.substring(searchIndex /*+ 1*/), + }; };