Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
js: Convert static/js/util.js to ES6 module.
Anders Kaseorg34b98a1cf634
diff --git a/static/js/util.js b/static/js/util.jsindex 89dcdd2d425..253c8658d1a 100644--- a/static/js/util.js+++ b/static/js/util.js@@ -1,10 +1,9 @@-"use strict";+import _ from "lodash";-const _ = require("lodash");// From MDN: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/random-exports.random_int = function random_int(min, max) {+export function random_int(min, max) {return Math.floor(Math.random() * (max - min + 1)) + min;-};+}// Like C++'s std::lower_bound. Returns the first index at which// `value` could be inserted without changing the ordering. Assumes@@ -17,7 +16,7 @@ exports.random_int = function random_int(min, max) {//// Usage: lower_bound(array, value, [less])// lower_bound(array, first, last, value, [less])-exports.lower_bound = function (array, arg1, arg2, arg3, arg4) {+export function lower_bound(array, arg1, arg2, arg3, arg4) {let first;let last;let value;@@ -55,27 +54,27 @@ exports.lower_bound = function (array, arg1, arg2, arg3, arg4) {}}return first;-};+}function lower_same(a, b) {return a.toLowerCase() === b.toLowerCase();}-exports.same_stream_and_topic = function util_same_stream_and_topic(a, b) {+export const same_stream_and_topic = function util_same_stream_and_topic(a, b) {// Streams and topics are case-insensitive.return a.stream_id === b.stream_id && lower_same(a.topic, b.topic);};-exports.is_pm_recipient = function (user_id, message) {+export function is_pm_recipient(user_id, message) {const recipients = message.to_user_ids.split(",");return recipients.includes(user_id.toString());-};+}-exports.extract_pm_recipients = function (recipients) {+export function extract_pm_recipients(recipients) {return recipients.split(/\s*[,;]\s*/).filter((recipient) => recipient.trim() !== "");-};+}-exports.same_recipient = function util_same_recipient(a, b) {+export const same_recipient = function util_same_recipient(a, b) {if (a === undefined || b === undefined) {return false;}@@ -90,14 +89,14 @@ exports.same_recipient = function util_same_recipient(a, b) {}return a.to_user_ids === b.to_user_ids;case "stream":- return exports.same_stream_and_topic(a, b);+ return same_stream_and_topic(a, b);}// should never get herereturn false;};-exports.same_sender = function util_same_sender(a, b) {+export const same_sender = function util_same_sender(a, b) {return (a !== undefined &&b !== undefined &&@@ -105,7 +104,7 @@ exports.same_sender = function util_same_sender(a, b) {);};-exports.normalize_recipients = function (recipients) {+export function normalize_recipients(recipients) {// Converts a string listing emails of message recipients// into a canonical formatting: emails sorted ASCIIbetically// with exactly one comma and no spaces between each.@@ -114,13 +113,13 @@ exports.normalize_recipients = function (recipients) {recipients = recipients.filter((s) => s.length > 0);recipients.sort();return recipients.join(",");-};+}// Avoid URI decode errors by removing characters from the end// one by one until the decode succeeds. This makes sense if// we are decoding input that the user is in the middle of// typing.-exports.robust_uri_decode = function (str) {+export function robust_uri_decode(str) {let end = str.length;while (end > 0) {try {@@ -133,13 +132,13 @@ exports.robust_uri_decode = function (str) {}}return "";-};+}// If we can, use a locale-aware sorter. However, if the browser// doesn't support the ECMAScript Internationalization API// Specification, do a dumb string comparison because// String.localeCompare is really slow.-exports.make_strcmp = function () {+export function make_strcmp() {try {const collator = new Intl.Collator();return collator.compare;@@ -150,10 +149,11 @@ exports.make_strcmp = function () {return function util_strcmp(a, b) {return a < b ? -1 : a > b ? 1 : 0;};-};-exports.strcmp = exports.make_strcmp();+}-exports.array_compare = function util_array_compare(a, b) {+export const strcmp = make_strcmp();++export const array_compare = function util_array_compare(a, b) {if (a.length !== b.length) {return false;}@@ -174,7 +174,7 @@ exports.array_compare = function util_array_compare(a, b) {* which should be a function that computes the uncached value.*/const unassigned_value_sentinel = {};-class CachedValue {+export class CachedValue {_value = unassigned_value_sentinel;constructor(opts) {@@ -192,17 +192,19 @@ class CachedValue {this._value = unassigned_value_sentinel;}}-exports.CachedValue = CachedValue;-exports.find_wildcard_mentions = function (message_content) {+export function find_wildcard_mentions(message_content) {const mention = message_content.match(/(^|\s)(@\*{2}(all|everyone|stream)\*{2})($|\s)/);if (mention === null) {return null;}return mention[3];-};+}-exports.move_array_elements_to_front = function util_move_array_elements_to_front(array, selected) {+export const move_array_elements_to_front = function util_move_array_elements_to_front(+ array,+ selected,+) {const selected_hash = new Set(selected);const selected_elements = [];const unselected_elements = [];@@ -213,12 +215,12 @@ exports.move_array_elements_to_front = function util_move_array_elements_to_fron};// check by the userAgent string if a user's client is likely mobile.-exports.is_mobile = function () {+export function is_mobile() {const regex = "Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini";return new RegExp(regex, "i").test(window.navigator.userAgent);-};+}-exports.sorted_ids = function (ids) {+export function sorted_ids(ids) {// This mapping makes sure we are using ints, and// it also makes sure we don't mutate the list.let id_list = ids.map((s) => Number.parseInt(s, 10));@@ -226,30 +228,30 @@ exports.sorted_ids = function (ids) {id_list = _.sortedUniq(id_list);return id_list;-};+}-exports.set_match_data = function (target, source) {+export function set_match_data(target, source) {target.match_subject = source.match_subject;target.match_content = source.match_content;-};+}-exports.get_match_topic = function (obj) {+export function get_match_topic(obj) {return obj.match_subject;-};+}-exports.get_draft_topic = function (obj) {+export function get_draft_topic(obj) {// We will need to support subject for old drafts.return obj.topic || obj.subject || "";-};+}-exports.get_reload_topic = function (obj) {+export function get_reload_topic(obj) {// When we first upgrade to releases that have// topic=foo in the code, the user's reload URL// may still have subject=foo from the prior version.return obj.topic || obj.subject || "";-};+}-exports.get_edit_event_topic = function (obj) {+export function get_edit_event_topic(obj) {if (obj.topic === undefined) {return obj.subject;}@@ -257,27 +259,27 @@ exports.get_edit_event_topic = function (obj) {// This code won't be reachable till we fix the// server, but we use it now in tests.return obj.topic;-};+}-exports.get_edit_event_orig_topic = function (obj) {+export function get_edit_event_orig_topic(obj) {return obj.orig_subject;-};+}-exports.get_edit_event_prev_topic = function (obj) {+export function get_edit_event_prev_topic(obj) {return obj.prev_subject;-};+}-exports.is_topic_synonym = function (operator) {+export function is_topic_synonym(operator) {return operator === "subject";-};+}-exports.convert_message_topic = function (message) {+export function convert_message_topic(message) {if (message.topic === undefined) {message.topic = message.subject;}-};+}-exports.clean_user_content_links = function (html) {+export function clean_user_content_links(html) {const content = new DOMParser().parseFromString(html, "text/html").body;for (const elt of content.querySelectorAll("a")) {// Ensure that all external links have target="_blank"@@ -331,4 +333,4 @@ exports.clean_user_content_links = function (html) {);}return content.innerHTML;-};+}