js: Convert _.any(a, …), _.some(a, …) to a.some(…).

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 19:08:04 -08:00
committed by Tim Abbott
parent 2285ee922e
commit 70ff164f89
15 changed files with 32 additions and 66 deletions

View File

@@ -38,9 +38,7 @@ people.add(me);
people.initialize_current_user(me.user_id);
function contains_sub(subs, sub) {
return _.any(subs, function (s) {
return s.name === sub.name;
});
return subs.some(s => s.name === sub.name);
}
run_test('basics', () => {

View File

@@ -151,9 +151,7 @@ const generate_emoji_picker_content = function (id) {
emojis_used = reactions.get_emojis_used_by_user_for_message_id(id);
}
for (const emoji_dict of emoji.emojis_by_name.values()) {
emoji_dict.has_reacted = _.any(emoji_dict.aliases, function (alias) {
return emojis_used.includes(alias);
});
emoji_dict.has_reacted = emoji_dict.aliases.some(alias => emojis_used.includes(alias));
}
return render_emoji_popover_content({

View File

@@ -362,19 +362,19 @@ Filter.prototype = {
},
has_negated_operand: function (operator, operand) {
return _.any(this._operators, function (elem) {
return elem.negated && (elem.operator === operator && elem.operand === operand);
});
return this._operators.some(
elem => elem.negated && (elem.operator === operator && elem.operand === operand)
);
},
has_operand: function (operator, operand) {
return _.any(this._operators, function (elem) {
return !elem.negated && (elem.operator === operator && elem.operand === operand);
});
return this._operators.some(
elem => !elem.negated && (elem.operator === operator && elem.operand === operand)
);
},
has_operator: function (operator) {
return _.any(this._operators, function (elem) {
return this._operators.some(elem => {
if (elem.negated && !['search', 'has'].includes(elem.operator)) {
return false;
}
@@ -474,7 +474,7 @@ Filter.prototype = {
return Filter.term_type(term) === 'pm-with';
};
if (!_.any(terms, is_pm_with)) {
if (!terms.some(is_pm_with)) {
return terms;
}

View File

@@ -400,9 +400,7 @@ exports.group_pm_with_user_ids = function (message) {
}
const user_ids = message.display_recipient.map(recip => recip.id);
const is_user_present = _.some(user_ids, function (user_id) {
return exports.is_my_user_id(user_id);
});
const is_user_present = user_ids.some(user_id => exports.is_my_user_id(user_id));
if (is_user_present) {
user_ids.sort();
if (user_ids.length > 2) {
@@ -470,9 +468,7 @@ exports.update_email_in_reply_to = function (reply_to, user_id, new_email) {
return reply_to;
}
const needs_patch = _.any(persons, function (person) {
return person.user_id === user_id;
});
const needs_patch = persons.some(person => person.user_id === user_id);
if (!needs_patch) {
return reply_to;
@@ -791,11 +787,7 @@ exports.build_termlet_matcher = function (termlet) {
}
const names = full_name.toLowerCase().split(' ');
return _.any(names, function (name) {
if (name.startsWith(termlet)) {
return true;
}
});
return names.some(name => name.startsWith(termlet));
};
};
@@ -835,9 +827,7 @@ exports.filter_people_by_search_terms = function (users, search_terms) {
}
// Return user emails that include search terms
const match = _.any(matchers, function (matcher) {
return matcher(user);
});
const match = matchers.some(matcher => matcher(user));
if (match) {
filtered_users.set(person.user_id, true);

View File

@@ -162,9 +162,7 @@ exports.poll_data_holder = function (is_my_poll, question, options) {
// function to check whether option already exists
self.is_option_present = function (data, latest_option) {
return _.any(data, function (el) {
return el.option === latest_option;
});
return data.some(el => el.option === latest_option);
};
// function to add all options added along with the /poll command

View File

@@ -441,12 +441,8 @@ exports.toggle_actions_popover = function (element, id) {
const should_display_edit_history_option =
message.edit_history &&
_.any(
message.edit_history,
entry =>
entry.prev_content !== undefined ||
util.get_edit_event_prev_topic(entry) !== undefined
) &&
message.edit_history.some(entry => entry.prev_content !== undefined ||
util.get_edit_event_prev_topic(entry) !== undefined) &&
page_params.realm_allow_edit_history;
// Disabling this for /me messages is a temporary workaround

View File

@@ -32,11 +32,9 @@ exports.open_reactions_popover = function () {
exports.current_user_has_reacted_to_emoji = function (message, emoji_code, type) {
const user_id = page_params.user_id;
return _.any(message.reactions, function (r) {
return r.user.id === user_id &&
return message.reactions.some(r => r.user.id === user_id &&
r.reaction_type === type &&
r.emoji_code === emoji_code;
});
r.emoji_code === emoji_code);
};
function get_message(message_id) {

View File

@@ -17,7 +17,7 @@ function make_person_highlighter(query) {
function match_criteria(operators, criteria) {
const filter = new Filter(operators);
return _.any(criteria, function (cr) {
return criteria.some(cr => {
if (_.has(cr, 'operand')) {
return filter.has_operand(cr.operator, cr.operand);
}

View File

@@ -17,11 +17,7 @@ exports.vanilla_match = function (opts) {
This is case insensitive.
*/
const val = opts.val.toLowerCase();
return _.any(opts.search_terms, function (term) {
if (val.includes(term)) {
return true;
}
});
return opts.search_terms.some(term => val.includes(term));
};
window.search_util = exports;

View File

@@ -197,9 +197,9 @@ exports.populate_user_groups = function () {
const blur_exceptions = _.without([".pill-container", ".name", ".description", ".input", ".delete"],
except_class);
if ($(event.relatedTarget).closest('#user-groups #' + data.id).length) {
return _.some(blur_exceptions, function (class_name) {
return $(event.relatedTarget).closest(class_name).length;
});
return blur_exceptions.some(
class_name => $(event.relatedTarget).closest(class_name).length
);
}
return false;
}

View File

@@ -17,13 +17,11 @@ function filter_streams_by_search(streams, search_term) {
let search_terms = search_term.toLowerCase().split(",");
search_terms = search_terms.map(s => s.trim());
const filtered_streams = streams.filter(stream => _.any(search_terms, function (search_term) {
const filtered_streams = streams.filter(stream => search_terms.some(search_term => {
const lower_stream_name = stream.toLowerCase();
const cands = lower_stream_name.split(" ");
cands.push(lower_stream_name);
return _.any(cands, function (name) {
return name.startsWith(search_term);
});
return cands.some(name => name.startsWith(search_term));
}));
return filtered_streams;

View File

@@ -38,7 +38,7 @@ const tictactoe_data_holder = function () {
return square_values[i];
}
return _.any(lines, line_won) || _.all(board, filled);
return lines.some(line_won) || _.all(board, filled);
}
self.get_widget_data = function () {

View File

@@ -21,9 +21,7 @@ exports.task_data_holder = function () {
self.check_task = {
task_exists: function (task) {
const task_exists = _.any(all_tasks, function (item) {
return item.task === task;
});
const task_exists = all_tasks.some(item => item.task === task);
return task_exists;
},
};

View File

@@ -80,9 +80,7 @@ exports.has_unconverted_data = function (pill_widget) {
}
const items = pill_widget.items();
const has_unknown_items = _.any(items, function (item) {
return item.user_id === undefined;
});
const has_unknown_items = items.some(item => item.user_id === undefined);
return has_unknown_items;
};

View File

@@ -75,7 +75,7 @@ function query_matches_string(query, source_str, split_char) {
// account, there might be 2 attrs: their full name and their email.
// * split_char is the separator for this syntax (e.g. ' ').
exports.query_matches_source_attrs = (query, source, match_attrs, split_char) => {
return _.any(match_attrs, function (attr) {
return match_attrs.some(attr => {
const source_str = source[attr].toLowerCase();
return query_matches_string(query, source_str, split_char);
});
@@ -151,9 +151,7 @@ exports.sort_emojis = function (objs, query) {
function decent_match(name) {
const pieces = name.toLowerCase().split('_');
return _.any(pieces, (piece) => {
return piece.startsWith(lowerQuery);
});
return pieces.some(piece => piece.startsWith(lowerQuery));
}
const popular_set = new Set(exports.popular_emojis);