mirror of
https://github.com/zulip/zulip.git
synced 2025-11-09 08:26:11 +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] || "";
|
return parts[1] || "";
|
||||||
}
|
}
|
||||||
|
|
||||||
export function get_current_nth_hash_section(n: number): string {
|
function get_nth_hash_section(hash: string, n: number): string {
|
||||||
const hash = window.location.hash;
|
// given "#settings/profile" and n=1, returns "profile"
|
||||||
// given "#settings/profile" and n=2, returns "profile"
|
// given '#streams/5/social" and n=2, returns "social"
|
||||||
// given '#streams/5/social" and n=3, returns "social"
|
|
||||||
const parts = hash.replace(/\/$/, "").split(/\//);
|
const parts = hash.replace(/\/$/, "").split(/\//);
|
||||||
if (parts.length < n) {
|
return parts.at(n) ?? "";
|
||||||
return "";
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
export function get_current_hash_category(): string {
|
||||||
|
|||||||
@@ -253,7 +253,7 @@ function do_hashchange_overlay(old_hash) {
|
|||||||
if (coming_from_overlay && base === old_base) {
|
if (coming_from_overlay && base === old_base) {
|
||||||
if (base === "streams") {
|
if (base === "streams") {
|
||||||
// e.g. #streams/29/social/subscribers
|
// e.g. #streams/29/social/subscribers
|
||||||
const right_side_tab = hash_parser.get_current_nth_hash_section(4);
|
const right_side_tab = hash_parser.get_current_nth_hash_section(3);
|
||||||
stream_settings_ui.change_state(section, right_side_tab);
|
stream_settings_ui.change_state(section, right_side_tab);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -320,7 +320,7 @@ function do_hashchange_overlay(old_hash) {
|
|||||||
|
|
||||||
if (base === "streams") {
|
if (base === "streams") {
|
||||||
// e.g. #streams/29/social/subscribers
|
// e.g. #streams/29/social/subscribers
|
||||||
const right_side_tab = hash_parser.get_current_nth_hash_section(4);
|
const right_side_tab = hash_parser.get_current_nth_hash_section(3);
|
||||||
stream_settings_ui.launch(section, right_side_tab);
|
stream_settings_ui.launch(section, right_side_tab);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,14 +77,14 @@ run_test("test_get_hash_section", () => {
|
|||||||
|
|
||||||
run_test("get_current_nth_hash_section", () => {
|
run_test("get_current_nth_hash_section", () => {
|
||||||
window.location.hash = "#settings/profile";
|
window.location.hash = "#settings/profile";
|
||||||
assert.equal(hash_parser.get_current_nth_hash_section(1), "#settings");
|
assert.equal(hash_parser.get_current_nth_hash_section(0), "#settings");
|
||||||
assert.equal(hash_parser.get_current_nth_hash_section(2), "profile");
|
assert.equal(hash_parser.get_current_nth_hash_section(1), "profile");
|
||||||
|
|
||||||
window.location.hash = "#settings/10/general";
|
window.location.hash = "#settings/10/general";
|
||||||
assert.equal(hash_parser.get_current_nth_hash_section(1), "#settings");
|
assert.equal(hash_parser.get_current_nth_hash_section(0), "#settings");
|
||||||
assert.equal(hash_parser.get_current_nth_hash_section(2), "10");
|
assert.equal(hash_parser.get_current_nth_hash_section(1), "10");
|
||||||
assert.equal(hash_parser.get_current_nth_hash_section(3), "general");
|
assert.equal(hash_parser.get_current_nth_hash_section(2), "general");
|
||||||
assert.equal(hash_parser.get_current_nth_hash_section(4), "");
|
assert.equal(hash_parser.get_current_nth_hash_section(3), "");
|
||||||
});
|
});
|
||||||
|
|
||||||
run_test("build_reload_url", () => {
|
run_test("build_reload_url", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user