js: Convert _.find(a, …) to a.find(…).

And convert the corresponding function expressions to arrow style
while we’re here.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg
2020-02-07 20:31:13 -08:00
committed by Tim Abbott
parent cdd774b790
commit 336a279005
9 changed files with 16 additions and 30 deletions

View File

@@ -98,9 +98,7 @@ exports.toggle = function (opts) {
maybe_go_right: maybe_go_right,
disable_tab: function (name) {
const value = _.find(opts.values, function (o) {
return o.key === name;
});
const value = opts.values.find(o => o.key === name);
const idx = opts.values.indexOf(value);
meta.$ind_tab.eq(idx).addClass('disabled');
@@ -118,9 +116,7 @@ exports.toggle = function (opts) {
// go through the process of finding the correct tab for a given name,
// and when found, select that one and provide the proper callback.
goto: function (name) {
const value = _.find(opts.values, function (o) {
return o.label === name || o.key === name;
});
const value = opts.values.find(o => o.label === name || o.key === name);
const idx = opts.values.indexOf(value);

View File

@@ -254,9 +254,9 @@ function get_alias_to_be_used(message_id, emoji_name) {
}
}
const user_id = page_params.user_id;
const reaction = _.find(message.reactions, function (reaction) {
return reaction.user.id === user_id && aliases.includes(reaction.emoji_name);
});
const reaction = message.reactions.find(
reaction => reaction.user.id === user_id && aliases.includes(reaction.emoji_name)
);
if (reaction) {
return reaction.emoji_name;
}
@@ -729,7 +729,7 @@ exports.register_click_handlers = function () {
$("body").on("click", ".emoji-popover-tab-item", function (e) {
e.stopPropagation();
e.preventDefault();
const offset = _.find(section_head_offsets, function (o) {
const offset = section_head_offsets.find(function (o) {
return o.section === $(this).attr("data-tab-name");
}.bind(this));

View File

@@ -530,7 +530,7 @@ Filter.prototype = {
first_valid_id_from: function (msg_ids) {
const predicate = this.predicate();
const first_id = _.find(msg_ids, function (msg_id) {
const first_id = msg_ids.find(msg_id => {
const message = message_store.get(msg_id);
if (message === undefined) {

View File

@@ -219,9 +219,7 @@ exports.create = function (opts) {
},
getByID: function (id) {
return _.find(store.pills, function (pill) {
return pill.id === id;
});
return store.pills.find(pill => pill.id === id);
},
items: function () {

View File

@@ -40,14 +40,12 @@ exports.set_name_in_mention_element = function (element, name) {
exports.contains_backend_only_syntax = function (content) {
// Try to guess whether or not a message has bugdown in it
// If it doesn't, we can immediately render it client-side
const markedup = _.find(backend_only_markdown_re, function (re) {
return re.test(content);
});
const markedup = backend_only_markdown_re.find(re => re.test(content));
// If a realm filter doesn't start with some specified characters
// then don't render it locally. It is workaround for the fact that
// javascript regex doesn't support lookbehind.
const false_filter_match = _.find(realm_filter_list, function (re) {
const false_filter_match = realm_filter_list.find(re => {
const pattern = /(?:[^\s'"\(,:<])/.source + re[0].source + /(?![\w])/.source;
const regex = new RegExp(pattern);
return regex.test(content);

View File

@@ -180,9 +180,7 @@ MessageListData.prototype = {
},
first_unread_message_id: function () {
const first_unread = _.find(this._items, function (message) {
return unread.message_unread(message);
});
const first_unread = this._items.find(message => unread.message_unread(message));
if (first_unread) {
return first_unread.id;

View File

@@ -1156,12 +1156,12 @@ MessageListView.prototype = {
// groups are merged etc.) , but we only call this from flows
// like message editing, so it's not a big performance
// problem.
return _.find(this._message_groups, function (message_group) {
return this._message_groups.find(
// Since we don't have a way to get a message group from
// the containing message container, we just do a search
// to find it.
return message_group.message_group_id === message_group_id;
});
message_group => message_group.message_group_id === message_group_id
);
},
_rerender_header: function (message_containers) {

View File

@@ -64,9 +64,7 @@ function is_local_part(value, element) {
}
exports.type_id_to_string = function (type_id) {
const name = _.find(page_params.bot_types, function (bot_type) {
return bot_type.type_id === type_id;
}).name;
const name = page_params.bot_types.find(bot_type => bot_type.type_id === type_id).name;
return i18n.t(name);
};

View File

@@ -90,9 +90,7 @@ exports.update_message = function (submsg) {
message.submessages = [];
}
const existing = _.find(message.submessages, function (sm) {
return sm.id === submsg.id;
});
const existing = message.submessages.find(sm => sm.id === submsg.id);
if (existing !== undefined) {
blueslip.warn("Got submessage multiple times: " + submsg.id);