js: Convert static/js/hash_util.js to ES6 module.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-27 16:07:13 -08:00 committed by Tim Abbott
parent 9d318f054f
commit c94ffb5319
36 changed files with 84 additions and 85 deletions

View File

@ -144,7 +144,6 @@
"emoji_picker": false,
"favicon": false,
"flatpickr": false,
"hash_util": false,
"hashchange": false,
"helpers": false,
"history": false,

View File

@ -91,7 +91,6 @@ rewiremock.enable();
const huddle_data = zrequire("huddle_data");
const compose_fade = zrequire("compose_fade");
zrequire("unread");
zrequire("hash_util");
const narrow = zrequire("narrow");
const presence = zrequire("presence");
const people = zrequire("people");

View File

@ -21,7 +21,9 @@ const compose_pm_pill = {__esModule: true};
rewiremock("../../static/js/compose_pm_pill").with(compose_pm_pill);
const hash_util = set_global("hash_util", {});
const hash_util = {__esModule: true};
rewiremock("../../static/js/hash_util").with(hash_util);
rewiremock("../../static/js/reload_state").with({
is_in_progress: () => false,

View File

@ -32,7 +32,6 @@ set_global("Image", Image);
const doc = "";
set_global("document", doc);
zrequire("hash_util");
const emoji = zrequire("emoji", "shared/js/emoji");
const emoji_codes = zrequire("emoji_codes", "generated/emoji/emoji_codes.json");
const pygments_data = zrequire("pygments_data", "generated/pygments_data.json");

View File

@ -22,7 +22,6 @@ rewiremock("katex").with({
rewiremock.enable();
const markdown_config = zrequire("markdown_config");
zrequire("hash_util");
zrequire("message_store");
const markdown = zrequire("markdown");

View File

@ -13,7 +13,6 @@ const {run_test} = require("../zjsunit/test");
const peer_data = zrequire("peer_data");
const people = zrequire("people");
zrequire("hash_util");
const stream_data = zrequire("stream_data");
set_global("page_params", {

View File

@ -32,7 +32,6 @@ rewiremock.enable();
zrequire("presence");
zrequire("buddy_data");
zrequire("hash_util");
const people = zrequire("people");
const pm_conversations = zrequire("pm_conversations");
const pm_list = zrequire("pm_list");

View File

@ -46,7 +46,6 @@ rewiremock("clipboard").with(ClipboardJS);
rewiremock.enable();
zrequire("hash_util");
zrequire("narrow");
const people = zrequire("people");
zrequire("presence");

View File

@ -47,7 +47,7 @@ set_global("unread", {
},
},
});
set_global("hash_util", {
rewiremock("../../static/js/hash_util").with({
by_stream_uri: () => "https://www.example.com",
by_stream_topic_uri: () => "https://www.example.com",
});

View File

@ -14,7 +14,6 @@ const page_params = set_global("page_params", {
});
const color_data = zrequire("color_data");
zrequire("hash_util");
const stream_topic_history = zrequire("stream_topic_history");
const people = zrequire("people");
const stream_color = zrequire("stream_color");

View File

@ -14,7 +14,7 @@ stub_templates(() => noop);
rewiremock("../../static/js/channel").with({});
set_global("hashchange", {update_browser_history: noop});
set_global("hash_util", {
rewiremock("../../static/js/hash_util").with({
stream_edit_uri: noop,
by_stream_uri: noop,
});

View File

@ -32,7 +32,6 @@ rewiremock.enable();
const {Filter} = zrequire("Filter", "js/filter");
const stream_sort = zrequire("stream_sort");
const stream_color = zrequire("stream_color");
zrequire("hash_util");
const unread = zrequire("unread");
const stream_data = zrequire("stream_data");
const scroll_util = zrequire("scroll_util");

View File

@ -2,6 +2,8 @@
const {strict: assert} = require("assert");
const rewiremock = require("rewiremock/node");
const {stub_templates} = require("../zjsunit/handlebars");
const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
@ -19,10 +21,12 @@ set_global("location", {
hash: `#streams/${denmark_stream_id}/announce`,
});
set_global("hash_util", {
rewiremock("../../static/js/hash_util").with({
by_stream_uri: () => {},
});
rewiremock.enable();
const stream_data = zrequire("stream_data");
const subs = zrequire("subs");
@ -205,3 +209,4 @@ run_test("redraw_left_panel", (override) => {
assert(!$(".right .settings").visible());
assert($(".nothing-selected").visible());
});
rewiremock.disable();

View File

@ -26,7 +26,6 @@ set_global("message_list", {});
rewiremock.enable();
zrequire("hash_util");
const stream_data = zrequire("stream_data");
const unread = zrequire("unread");
const stream_topic_history = zrequire("stream_topic_history");

View File

@ -19,7 +19,6 @@ const peer_data = zrequire("peer_data");
const people = zrequire("people");
const stream_data = zrequire("stream_data");
zrequire("narrow");
zrequire("hash_util");
const emoji = zrequire("emoji", "shared/js/emoji");
const pygments_data = zrequire("pygments_data", "generated/pygments_data.json");

View File

@ -109,7 +109,6 @@ rewiremock.enable();
const util = zrequire("util");
zrequire("hash_util");
zrequire("message_view_header");
zrequire("presence");
zrequire("search_pill_widget");

View File

@ -1,6 +1,7 @@
"use strict";
const compose_fade = require("./compose_fade");
const hash_util = require("./hash_util");
const people = require("./people");
const user_status = require("./user_status");
const util = require("./util");

View File

@ -30,7 +30,6 @@ import "../ui";
import "../composebox_typeahead";
import "../hotkey";
import "../notifications";
import "../hash_util";
import "../hashchange";
import "../message_flags";
import "../starred_messages";

View File

@ -9,6 +9,7 @@ import render_buddy_list_tooltip_content from "../templates/buddy_list_tooltip_c
import * as channel from "./channel";
import * as compose from "./compose";
import * as compose_state from "./compose_state";
import * as hash_util from "./hash_util";
import * as message_edit from "./message_edit";
import * as message_edit_history from "./message_edit_history";
import * as muting_ui from "./muting_ui";

View File

@ -11,6 +11,7 @@ const compose_fade = require("./compose_fade");
const compose_pm_pill = require("./compose_pm_pill");
const compose_state = require("./compose_state");
const drafts = require("./drafts");
const hash_util = require("./hash_util");
const message_viewport = require("./message_viewport");
const narrow_state = require("./narrow_state");
const notifications = require("./notifications");

View File

@ -22,7 +22,6 @@ declare let emoji: any;
declare let emoji_picker: any;
declare let favicon: any;
declare let hashchange: any;
declare let hash_util: any;
declare let helpers: any;
declare let home_msg_list: any;
declare let hotspots: any;

View File

@ -1,16 +1,14 @@
"use strict";
import * as narrow_state from "./narrow_state";
import * as people from "./people";
import * as stream_data from "./stream_data";
import * as ui_report from "./ui_report";
const narrow_state = require("./narrow_state");
const people = require("./people");
const stream_data = require("./stream_data");
const ui_report = require("./ui_report");
exports.get_hash_category = function (hash) {
export function get_hash_category(hash) {
// given "#streams/subscribed", returns "streams"
return hash ? hash.replace(/^#/, "").split(/\//)[0] : "";
};
}
exports.get_hash_section = function (hash) {
export function get_hash_section(hash) {
// given "#settings/your-account", returns "your-account"
// given '#streams/5/social", returns "5"
if (!hash) {
@ -20,16 +18,16 @@ exports.get_hash_section = function (hash) {
const 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).
exports.encodeHashComponent = function (str) {
export function encodeHashComponent(str) {
return encodeURIComponent(str).replace(/\./g, "%2E").replace(/%/g, ".");
};
}
exports.encode_operand = function (operator, operand) {
export function encode_operand(operator, operand) {
if (operator === "group-pm-with" || operator === "pm-with" || operator === "sender") {
const slug = people.emails_to_slug(operand);
if (slug) {
@ -38,29 +36,29 @@ exports.encode_operand = function (operator, operand) {
}
if (operator === "stream") {
return exports.encode_stream_name(operand);
return encode_stream_name(operand);
}
return exports.encodeHashComponent(operand);
};
return encodeHashComponent(operand);
}
exports.encode_stream_id = function (stream_id) {
export function encode_stream_id(stream_id) {
// stream_data appends the stream name, but it does not do the
// URI encoding piece
const slug = stream_data.id_to_slug(stream_id);
return exports.encodeHashComponent(slug);
};
return encodeHashComponent(slug);
}
exports.encode_stream_name = function (operand) {
export function encode_stream_name(operand) {
// stream_data prefixes the stream id, but it does not do the
// URI encoding piece
operand = stream_data.name_to_slug(operand);
return exports.encodeHashComponent(operand);
};
return encodeHashComponent(operand);
}
exports.decodeHashComponent = function (str) {
export function decodeHashComponent(str) {
try {
// This fails for URLs containing
// foo.foo or foo%foo due to our fault in special handling
@ -73,9 +71,9 @@ exports.decodeHashComponent = function (str) {
ui_report.error(i18n.t("Invalid URL"), undefined, $("#home-error"), 2000);
return "";
}
};
}
exports.decode_operand = function (operator, operand) {
export function decode_operand(operator, operand) {
if (operator === "group-pm-with" || operator === "pm-with" || operator === "sender") {
const emails = people.slug_to_emails(operand);
if (emails) {
@ -83,32 +81,27 @@ exports.decode_operand = function (operator, operand) {
}
}
operand = exports.decodeHashComponent(operand);
operand = decodeHashComponent(operand);
if (operator === "stream") {
return stream_data.slug_to_name(operand);
}
return operand;
};
}
exports.by_stream_uri = function (stream_id) {
return "#narrow/stream/" + exports.encode_stream_id(stream_id);
};
export function by_stream_uri(stream_id) {
return "#narrow/stream/" + encode_stream_id(stream_id);
}
exports.by_stream_topic_uri = function (stream_id, topic) {
return (
"#narrow/stream/" +
exports.encode_stream_id(stream_id) +
"/topic/" +
exports.encodeHashComponent(topic)
);
};
export function by_stream_topic_uri(stream_id, topic) {
return "#narrow/stream/" + encode_stream_id(stream_id) + "/topic/" + encodeHashComponent(topic);
}
// Encodes an operator list into the
// corresponding hash: the # component
// of the narrow URL
exports.operators_to_hash = function (operators) {
export function operators_to_hash(operators) {
let hash = "#";
if (operators !== undefined) {
@ -123,33 +116,33 @@ exports.operators_to_hash = function (operators) {
hash +=
"/" +
sign +
exports.encodeHashComponent(operator) +
encodeHashComponent(operator) +
"/" +
exports.encode_operand(operator, operand);
encode_operand(operator, operand);
}
}
return hash;
};
}
exports.by_sender_uri = function (reply_to) {
return exports.operators_to_hash([{operator: "sender", operand: reply_to}]);
};
export function by_sender_uri(reply_to) {
return operators_to_hash([{operator: "sender", operand: reply_to}]);
}
exports.pm_with_uri = function (reply_to) {
export function pm_with_uri(reply_to) {
const slug = people.emails_to_slug(reply_to);
return "#narrow/pm-with/" + slug;
};
}
exports.huddle_with_uri = function (user_ids_string) {
export function huddle_with_uri(user_ids_string) {
// This method is convenient for callers
// that have already converted emails to a comma-delimited
// list of user_ids. We should be careful to keep this
// consistent with hash_util.decode_operand.
return "#narrow/pm-with/" + user_ids_string + "-group";
};
}
exports.by_conversation_and_time_uri = function (message) {
export function by_conversation_and_time_uri(message) {
const absolute_url =
window.location.protocol +
"//" +
@ -157,36 +150,34 @@ exports.by_conversation_and_time_uri = function (message) {
"/" +
window.location.pathname.split("/")[1];
const suffix = "/near/" + exports.encodeHashComponent(message.id);
const suffix = "/near/" + encodeHashComponent(message.id);
if (message.type === "stream") {
return (
absolute_url + exports.by_stream_topic_uri(message.stream_id, message.topic) + suffix
);
return absolute_url + by_stream_topic_uri(message.stream_id, message.topic) + suffix;
}
return absolute_url + people.pm_perma_link(message) + suffix;
};
}
exports.stream_edit_uri = function (sub) {
const hash = `#streams/${sub.stream_id}/${exports.encodeHashComponent(sub.name)}`;
export function stream_edit_uri(sub) {
const hash = `#streams/${sub.stream_id}/${encodeHashComponent(sub.name)}`;
return hash;
};
}
exports.search_public_streams_notice_url = function () {
export function search_public_streams_notice_url() {
// Computes the URL of the current narrow if streams:public were added.
const operators = narrow_state.filter().operators();
const public_operator = {operator: "streams", operand: "public"};
return exports.operators_to_hash([public_operator].concat(operators));
};
return operators_to_hash([public_operator].concat(operators));
}
exports.parse_narrow = function (hash) {
export function parse_narrow(hash) {
let i;
const operators = [];
for (i = 1; i < hash.length; i += 2) {
// We don't construct URLs with an odd number of components,
// but the user might write one.
let operator = exports.decodeHashComponent(hash[i]);
let operator = decodeHashComponent(hash[i]);
// Do not parse further if empty operator encountered.
if (operator === "") {
break;
@ -204,10 +195,8 @@ exports.parse_narrow = function (hash) {
operator = operator.slice(1);
}
const operand = exports.decode_operand(operator, raw_operand);
const operand = decode_operand(operator, raw_operand);
operators.push({negated, operator, operand});
}
return operators;
};
window.hash_util = exports;
}

View File

@ -2,6 +2,7 @@
const drafts = require("./drafts");
const floating_recipient_bar = require("./floating_recipient_bar");
const hash_util = require("./hash_util");
const info_overlay = require("./info_overlay");
const invite = require("./invite");
const message_viewport = require("./message_viewport");

View File

@ -1,3 +1,4 @@
import * as hash_util from "./hash_util";
import * as people from "./people";
import * as stream_data from "./stream_data";
import * as user_groups from "./user_groups";

View File

@ -9,6 +9,7 @@ import render_single_message from "../templates/single_message.hbs";
import * as compose from "./compose";
import * as compose_fade from "./compose_fade";
import * as condense from "./condense";
import * as hash_util from "./hash_util";
import * as message_edit from "./message_edit";
import * as message_viewport from "./message_viewport";
import * as narrow_state from "./narrow_state";

View File

@ -1,6 +1,7 @@
import _ from "lodash";
import * as floating_recipient_bar from "./floating_recipient_bar";
import * as hash_util from "./hash_util";
import * as loading from "./loading";
import * as message_viewport from "./message_viewport";
import * as narrow_state from "./narrow_state";

View File

@ -1,3 +1,4 @@
import * as hash_util from "./hash_util";
import * as narrow_state from "./narrow_state";
import * as people from "./people";
import * as pm_conversations from "./pm_conversations";

View File

@ -16,6 +16,7 @@ import render_user_profile_modal from "../templates/user_profile_modal.hbs";
import * as compose_state from "./compose_state";
import * as condense from "./condense";
import * as feature_flags from "./feature_flags";
import * as hash_util from "./hash_util";
import * as message_edit from "./message_edit";
import * as message_edit_history from "./message_edit_history";
import * as message_viewport from "./message_viewport";

View File

@ -5,6 +5,7 @@ const render_recent_topics_filters = require("../templates/recent_topics_filters
const render_recent_topics_body = require("../templates/recent_topics_table.hbs");
const drafts = require("./drafts");
const hash_util = require("./hash_util");
const ListWidget = require("./list_widget");
const {localstorage} = require("./localstorage");
const muting = require("./muting");

View File

@ -2,6 +2,7 @@
const channel = require("./channel");
const compose = require("./compose");
const hash_util = require("./hash_util");
const notifications = require("./notifications");
const people = require("./people");
const transmit = require("./transmit");

View File

@ -1,5 +1,6 @@
import * as color_data from "./color_data";
import {FoldDict} from "./fold_dict";
import * as hash_util from "./hash_util";
import * as peer_data from "./peer_data";
import * as people from "./people";
import * as settings_config from "./settings_config";

View File

@ -5,6 +5,7 @@ import render_subscription_settings from "../templates/subscription_settings.hbs
import render_subscription_stream_privacy_modal from "../templates/subscription_stream_privacy_modal.hbs";
import * as channel from "./channel";
import * as hash_util from "./hash_util";
import * as ListWidget from "./list_widget";
import * as narrow_state from "./narrow_state";
import * as overlays from "./overlays";

View File

@ -5,6 +5,7 @@ const _ = require("lodash");
const render_stream_privacy = require("../templates/stream_privacy.hbs");
const render_stream_sidebar_row = require("../templates/stream_sidebar_row.hbs");
const hash_util = require("./hash_util");
const keydown_util = require("./keydown_util");
const {ListCursor} = require("./list_cursor");
const narrow_state = require("./narrow_state");

View File

@ -7,6 +7,7 @@ import render_topic_sidebar_actions from "../templates/topic_sidebar_actions.hbs
import render_unstar_messages_modal from "../templates/unstar_messages_modal.hbs";
import * as channel from "./channel";
import * as hash_util from "./hash_util";
import * as message_edit from "./message_edit";
import * as muting from "./muting";
import * as muting_ui from "./muting_ui";

View File

@ -10,6 +10,7 @@ const render_subscriptions = require("../templates/subscriptions.hbs");
const channel = require("./channel");
const components = require("./components");
const compose_state = require("./compose_state");
const hash_util = require("./hash_util");
const loading = require("./loading");
const message_live_update = require("./message_live_update");
const overlays = require("./overlays");

View File

@ -1,3 +1,4 @@
import * as hash_util from "./hash_util";
import * as muting from "./muting";
import * as narrow_state from "./narrow_state";
import * as stream_topic_history from "./stream_topic_history";