Improve flexibility of url parsing (#26)

This commit is contained in:
Justin Starry
2020-03-30 23:34:03 +08:00
committed by Michael Vines
parent 1ad1ba73fa
commit e59625a41d
3 changed files with 31 additions and 3 deletions

View File

@@ -11,6 +11,20 @@ export function findGetParameter(parameterName: string): string | null {
return result;
}
export function findPathSegment(pathName: string): string | null {
const segments = window.location.pathname.substr(1).split("/");
if (segments.length < 2) return null;
// remove all but last two segments
segments.splice(0, segments.length - 2);
if (segments[0] === pathName) {
return segments[1];
}
return null;
}
export function assertUnreachable(x: never): never {
throw new Error("Unreachable!");
}