refactor: Extract get_nth_hash_section() and use 0-based indexing.

From `get_current_nth_hash_section` `get_nth_hash_section()` is
extracted to allow for reuse for any hash. The indexing is also changed
to 0-based and negative indexing also works now.

This is a prep commit for the next, where `get_nth_hash_section()` will
be used in a new function with negative indexing.
This commit is contained in:
N-Shar-ma
2024-01-27 03:47:49 +05:30
committed by Tim Abbott
parent 706be812b9
commit 727774a0e5
3 changed files with 15 additions and 16 deletions

View File

@@ -15,16 +15,15 @@ export function get_hash_section(hash?: string): string {
return parts[1] || "";
}
export function get_current_nth_hash_section(n: number): string {
const hash = window.location.hash;
// given "#settings/profile" and n=2, returns "profile"
// given '#streams/5/social" and n=3, returns "social"
function get_nth_hash_section(hash: string, n: number): string {
// given "#settings/profile" and n=1, returns "profile"
// given '#streams/5/social" and n=2, returns "social"
const parts = hash.replace(/\/$/, "").split(/\//);
if (parts.length < n) {
return "";
}
return parts.at(n) ?? "";
}
return parts[n - 1] || "";
export function get_current_nth_hash_section(n: number): string {
return get_nth_hash_section(window.location.hash, n);
}
export function get_current_hash_category(): string {