mirror of
https://github.com/zulip/zulip.git
synced 2025-11-06 23:13:25 +00:00
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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user