hashchange: Add hash_util.get_hash_section().

We'll use this mostly for streams/settings URLs at first.
This commit is contained in:
Steve Howell
2018-12-05 19:33:52 +00:00
committed by Tim Abbott
parent 7cbf031f18
commit a2fd901bec
2 changed files with 37 additions and 0 deletions

View File

@@ -82,6 +82,31 @@ run_test('test_get_hash_category', () => {
);
});
run_test('test_get_hash_section', () => {
assert.equal(
hash_util.get_hash_section('streams/subscribed'),
'subscribed'
);
assert.equal(
hash_util.get_hash_section('#settings/your-account'),
'your-account'
);
assert.equal(
hash_util.get_hash_section('settings/10/general/'),
'10'
);
assert.equal(
hash_util.get_hash_section('#drafts'),
''
);
assert.equal(
hash_util.get_hash_section(''),
''
);
});
run_test('test_parse_narrow', () => {
assert.deepEqual(
hash_util.parse_narrow(['narrow', 'stream', '11-social']),

View File

@@ -7,6 +7,18 @@ exports.get_hash_category = function (hash) {
return hash ? hash.replace(/^#/, "").split(/\//)[0] : "";
};
exports.get_hash_section = function (hash) {
// given "#settings/your-account", returns "your-account"
// given '#streams/5/social", returns "5"
if (!hash) {
return '';
}
var parts = hash.replace(/\/$/, "").split(/\//);
return parts[1] || '';
};
// Some browsers zealously URI-decode the contents of
// window.location.hash. So we hide our URI-encoding
// by replacing % with . (like MediaWiki).