Skip to content

Commit

Permalink
Rewrite parsePath
Browse files Browse the repository at this point in the history
  • Loading branch information
zoontek committed Dec 19, 2023
1 parent 8b3bd15 commit 9578ce4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
27 changes: 14 additions & 13 deletions src/historyLite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Location> => {
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*/),
};
};

0 comments on commit 9578ce4

Please sign in to comment.