diff --git a/frontend_tests/node_tests/hash_util.js b/frontend_tests/node_tests/hash_util.js index 56db635538..22aa1e8165 100644 --- a/frontend_tests/node_tests/hash_util.js +++ b/frontend_tests/node_tests/hash_util.js @@ -63,6 +63,25 @@ run_test('hash_util', () => { encode_decode_operand(operator, operand, 'testing.20123'); }); +run_test('test_get_hash_category', () => { + assert.deepEqual( + hash_util.get_hash_category('streams/subscribed'), + 'streams' + ); + assert.deepEqual( + hash_util.get_hash_category('#settings/display-settings'), + 'settings' + ); + assert.deepEqual( + hash_util.get_hash_category('#drafts'), + 'drafts' + ); + assert.deepEqual( + hash_util.get_hash_category('invites'), + 'invites' + ); +}); + run_test('test_parse_narrow', () => { assert.deepEqual( hash_util.parse_narrow(['narrow', 'stream', '11-social']), diff --git a/static/js/hash_util.js b/static/js/hash_util.js index 4b0b9e9777..f19125a354 100644 --- a/static/js/hash_util.js +++ b/static/js/hash_util.js @@ -2,6 +2,11 @@ var hash_util = (function () { var exports = {}; +exports.get_hash_category = function (hash) { + // given "#streams/subscribed", returns "streams" + return hash ? hash.replace(/^#/, "").split(/\//)[0] : ""; +}; + // Some browsers zealously URI-decode the contents of // window.location.hash. So we hide our URI-encoding // by replacing % with . (like MediaWiki). diff --git a/static/js/hashchange.js b/static/js/hashchange.js index 1b53294dd5..9979447b60 100644 --- a/static/js/hashchange.js +++ b/static/js/hashchange.js @@ -59,10 +59,6 @@ var state = { old_hash: typeof window !== "undefined" ? window.location.hash : "#", }; -function get_main_hash(hash) { - return hash ? hash.replace(/^#/, "").split(/\//)[0] : ""; -} - function get_hash_components() { var hash = window.location.hash.split(/\//); @@ -75,7 +71,7 @@ function get_hash_components() { function is_overlay_hash(hash) { // Hash changes within this list are overlays and should not unnarrow (etc.) var overlay_list = ["streams", "drafts", "settings", "organization", "invite"]; - var main_hash = get_main_hash(hash); + var main_hash = hash_util.get_hash_category(hash); return overlay_list.indexOf(main_hash) > -1; } @@ -141,8 +137,8 @@ function do_hashchange_normal(from_reload) { } function do_hashchange_overlay(old_hash) { - var base = get_main_hash(window.location.hash); - var old_base = get_main_hash(old_hash); + var base = hash_util.get_hash_category(window.location.hash); + var old_base = hash_util.get_hash_category(old_hash); var coming_from_overlay = is_overlay_hash(old_hash || '#');