ts: Convert internal_url.js to TypeScript.

This commit is contained in:
Lalit
2023-05-25 11:43:18 +05:30
committed by Tim Abbott
parent 0b19d5cd5e
commit 56463ce921

View File

@@ -1,3 +1,5 @@
type MaybeGetStreamName = (id: number) => string | undefined;
const hashReplacements = new Map([ const hashReplacements = new Map([
["%", "."], ["%", "."],
["(", ".28"], ["(", ".28"],
@@ -8,11 +10,11 @@ const hashReplacements = new Map([
// Some browsers zealously URI-decode the contents of // Some browsers zealously URI-decode the contents of
// window.location.hash. So we hide our URI-encoding // window.location.hash. So we hide our URI-encoding
// by replacing % with . (like MediaWiki). // by replacing % with . (like MediaWiki).
export function encodeHashComponent(str) { export function encodeHashComponent(str: string): string {
return encodeURIComponent(str).replace(/[%().]/g, (matched) => hashReplacements.get(matched)); return encodeURIComponent(str).replace(/[%().]/g, (matched) => hashReplacements.get(matched)!);
} }
export function decodeHashComponent(str) { export function decodeHashComponent(str: string): string {
// This fails for URLs containing // This fails for URLs containing
// foo.foo or foo%foo due to our fault in special handling // foo.foo or foo%foo due to our fault in special handling
// of such characters when encoding. This can also, // of such characters when encoding. This can also,
@@ -21,8 +23,11 @@ export function decodeHashComponent(str) {
return decodeURIComponent(str.replace(/\./g, "%")); return decodeURIComponent(str.replace(/\./g, "%"));
} }
export function stream_id_to_slug(stream_id, maybe_get_stream_name) { export function stream_id_to_slug(
let name = maybe_get_stream_name(stream_id) || "unknown"; stream_id: number,
maybe_get_stream_name: MaybeGetStreamName,
): string {
let name = maybe_get_stream_name(stream_id) ?? "unknown";
// The name part of the URL doesn't really matter, so we try to // The name part of the URL doesn't really matter, so we try to
// make it pretty. // make it pretty.
@@ -31,10 +36,13 @@ export function stream_id_to_slug(stream_id, maybe_get_stream_name) {
// browsers that don't have it. // browsers that don't have it.
name = name.replace(/ /g, "-"); name = name.replace(/ /g, "-");
return stream_id + "-" + name; return `${stream_id}-${name}`;
} }
export function encode_stream_id(stream_id, maybe_get_stream_name) { export function encode_stream_id(
stream_id: number,
maybe_get_stream_name: MaybeGetStreamName,
): string {
// stream_id_to_slug appends the stream name, but it does not do the // stream_id_to_slug appends the stream name, but it does not do the
// URI encoding piece. // URI encoding piece.
const slug = stream_id_to_slug(stream_id, maybe_get_stream_name); const slug = stream_id_to_slug(stream_id, maybe_get_stream_name);
@@ -42,15 +50,20 @@ export function encode_stream_id(stream_id, maybe_get_stream_name) {
return encodeHashComponent(slug); return encodeHashComponent(slug);
} }
export function by_stream_url(stream_id, maybe_get_stream_name) { export function by_stream_url(
return "#narrow/stream/" + encode_stream_id(stream_id, maybe_get_stream_name); stream_id: number,
maybe_get_stream_name: MaybeGetStreamName,
): string {
return `#narrow/stream/${encode_stream_id(stream_id, maybe_get_stream_name)}`;
} }
export function by_stream_topic_url(stream_id, topic, maybe_get_stream_name) { export function by_stream_topic_url(
return ( stream_id: number,
"#narrow/stream/" + topic: string,
encode_stream_id(stream_id, maybe_get_stream_name) + maybe_get_stream_name: MaybeGetStreamName,
"/topic/" + ): string {
encodeHashComponent(topic) return `#narrow/stream/${encode_stream_id(
); stream_id,
maybe_get_stream_name,
)}/topic/${encodeHashComponent(topic)}`;
} }