Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

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

N-Shar-ma727774a0e54d
diff --git a/web/src/hash_parser.ts b/web/src/hash_parser.ts
index 580f951a3ea..a286fe75d8b 100644
--- a/web/src/hash_parser.ts
+++ b/web/src/hash_parser.ts
@@ -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 {
diff --git a/web/src/hashchange.js b/web/src/hashchange.js
index ed22d2906be..fb314f8a901 100644
--- a/web/src/hashchange.js
+++ b/web/src/hashchange.js
@@ -253,7 +253,7 @@ function do_hashchange_overlay(old_hash) {
if (coming_from_overlay && base === old_base) {
if (base === "streams") {
// 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);
return;
}
@@ -320,7 +320,7 @@ function do_hashchange_overlay(old_hash) {
if (base === "streams") {
// 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);
return;
}
diff --git a/web/tests/hash_util.test.js b/web/tests/hash_util.test.js
index 21302208fe8..1a04e4f7e32 100644
--- a/web/tests/hash_util.test.js
+++ b/web/tests/hash_util.test.js
@@ -77,14 +77,14 @@ run_test("test_get_hash_section", () => {
run_test("get_current_nth_hash_section", () => {
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(2), "profile");
+ assert.equal(hash_parser.get_current_nth_hash_section(0), "#settings");
+ assert.equal(hash_parser.get_current_nth_hash_section(1), "profile");
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(2), "10");
- assert.equal(hash_parser.get_current_nth_hash_section(3), "general");
- assert.equal(hash_parser.get_current_nth_hash_section(4), "");
+ assert.equal(hash_parser.get_current_nth_hash_section(0), "#settings");
+ assert.equal(hash_parser.get_current_nth_hash_section(1), "10");
+ assert.equal(hash_parser.get_current_nth_hash_section(2), "general");
+ assert.equal(hash_parser.get_current_nth_hash_section(3), "");
});
run_test("build_reload_url", () => {