js: Simplify indexing arrays from the end with Array#at.

https://github.com/tc39/proposal-relative-indexing-method

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-01-24 00:05:06 -08:00 committed by Tim Abbott
parent 0b03628324
commit c6b372b471
13 changed files with 26 additions and 33 deletions

View File

@ -54,7 +54,7 @@ function test_with(fixture) {
},
last: () => {
assert.notEqual(fixture.all_messages, undefined);
return fixture.all_messages[fixture.all_messages.length - 1];
return fixture.all_messages.at(-1);
},
};

View File

@ -109,10 +109,7 @@ function diff_strings(string_0, string_1) {
output_lines.push(line);
} else if (line.startsWith("?")) {
changes_list = parse_questionmark_line(line);
output_lines[output_lines.length - 1] = apply_color(
output_lines[output_lines.length - 1],
changes_list,
);
output_lines.push(apply_color(output_lines.pop(), changes_list));
} else {
output_lines.push(line);
}

View File

@ -470,7 +470,7 @@ function drafts_initialize_focus(event_name) {
let draft_element;
if (event_name === "up_arrow") {
draft_element = document.querySelectorAll(
'[data-draft-id="' + draft_id_arrow[draft_id_arrow.length - 1] + '"]',
'[data-draft-id="' + draft_id_arrow.at(-1) + '"]',
);
} else if (event_name === "down_arrow") {
draft_element = document.querySelectorAll('[data-draft-id="' + draft_id_arrow[0] + '"]');
@ -565,7 +565,7 @@ export function drafts_handle_events(e, event_key) {
if (document.activeElement.parentElement.hasAttribute("data-draft-id")) {
restore_draft(focused_draft_id);
} else {
const first_draft = draft_id_arrow[draft_id_arrow.length - 1];
const first_draft = draft_id_arrow.at(-1);
restore_draft(first_draft);
}
}

View File

@ -563,7 +563,7 @@ export function emoji_select_tab(elt) {
// Handles the corner case of the last category being
// smaller than half of the emoji picker height.
if (elt_height + scrolltop === scrollheight) {
currently_selected = section_head_offsets[section_head_offsets.length - 1].section;
currently_selected = section_head_offsets.at(-1).section;
}
// Handles the corner case of the scrolling back to top.
if (scrolltop === 0) {

View File

@ -85,7 +85,7 @@ export function first_visible_message(bar) {
}
// If none of the messages are visible, just take the last message.
return $(messages[messages.length - 1]);
return messages.last();
}
export function get_date(elem) {

View File

@ -440,7 +440,7 @@ export function initialize(home_view_loaded) {
// If we fall through here, we need to keep fetching more data, and
// we'll call back to the function we're in.
const messages = data.messages;
const latest_id = messages[messages.length - 1].id;
const latest_id = messages.at(-1).id;
load_messages({
anchor: latest_id,

View File

@ -39,7 +39,7 @@ export class MessageListData {
}
last() {
return this._items[this._items.length - 1];
return this._items.at(-1);
}
select_idx() {

View File

@ -344,9 +344,7 @@ export class MessageListView {
current_group,
current_group.message_containers[0],
);
current_group.message_containers[
current_group.message_containers.length - 1
].include_footer = true;
current_group.message_containers.at(-1).include_footer = true;
new_message_groups.push(current_group);
}
};
@ -1212,7 +1210,7 @@ export class MessageListView {
for (const message_container of message_containers) {
if (
current_group.length === 0 ||
same_recipient(current_group[current_group.length - 1], message_container)
same_recipient(current_group.at(-1), message_container)
) {
current_group.push(message_container);
} else {

View File

@ -74,8 +74,7 @@ function get_selected_integration_name() {
}
function get_fixture_format(fixture_name) {
const pieces = fixture_name.split(".");
return pieces[pieces.length - 1];
return fixture_name.split(".").at(-1);
}
function get_custom_http_headers() {

View File

@ -641,7 +641,6 @@ export function get_search_result(base_query, query) {
}
const person_suggestion_ops = ["sender", "pm-with", "from", "group-pm"];
const search_operators_len = search_operators.length;
// Handle spaces in person name in new suggestions only. Checks if the last operator is 'search'
// and the second last operator in search_operators is one out of person_suggestion_ops.
@ -650,11 +649,11 @@ export function get_search_result(base_query, query) {
// is an email of a user, both of these operators remain unchanged. Otherwise search operator
// will be deleted and new last will become {operator:'sender', operand: 'Ted sm`....}.
if (
search_operators_len > 1 &&
search_operators.length > 1 &&
last.operator === "search" &&
person_suggestion_ops.includes(search_operators[search_operators_len - 2].operator)
person_suggestion_ops.includes(search_operators.at(-2).operator)
) {
const person_op = search_operators[search_operators_len - 2];
const person_op = search_operators.at(-2);
if (!people.reply_to_to_user_ids_string(person_op.operand)) {
last = {
operator: person_op.operator,
@ -662,11 +661,11 @@ export function get_search_result(base_query, query) {
negated: person_op.negated,
};
if (page_params.search_pills_enabled) {
all_operators[all_operators.length - 2] = last;
all_operators.splice(-1, 1);
all_operators.splice(-2);
all_operators.push(last);
}
search_operators[search_operators_len - 2] = last;
search_operators.splice(-1, 1);
search_operators.splice(-2);
search_operators.push(last);
}
}

View File

@ -73,7 +73,7 @@ function update_last_full_update(end_times) {
return;
}
last_full_update = Math.min(last_full_update, end_times[end_times.length - 1]);
last_full_update = Math.min(last_full_update, end_times.at(-1));
const update_time = new Date(last_full_update * 1000);
const locale_date = update_time.toLocaleDateString("en-US", {
year: "numeric",
@ -253,7 +253,7 @@ function populate_messages_sent_over_time(data) {
dates,
values,
last_value_is_partial: !is_boundary(
new Date(start_dates[start_dates.length - 1].getTime() + 60 * 60 * 1000),
new Date(start_dates.at(-1).getTime() + 60 * 60 * 1000),
),
};
}
@ -386,7 +386,7 @@ function compute_summary_chart_data(time_series_data, num_steps, labels_) {
}
let sum = 0;
for (let i = 1; i <= num_steps; i += 1) {
sum += array[array.length - i];
sum += array.at(-i);
}
data.set(key, sum);
}
@ -939,7 +939,7 @@ function populate_messages_read_over_time(data) {
dates,
values,
last_value_is_partial: !is_boundary(
new Date(start_dates[start_dates.length - 1].getTime() + 60 * 60 * 1000),
new Date(start_dates.at(-1).getTime() + 60 * 60 * 1000),
),
};
}

View File

@ -242,7 +242,7 @@ export function setup_upload(config) {
return;
}
const split_uri = uri.split("/");
const filename = split_uri[split_uri.length - 1];
const filename = split_uri.at(-1);
if (config.mode === "compose" && !compose_state.composing()) {
compose_actions.start("stream");
}

View File

@ -209,18 +209,18 @@ export function process_fenced_code(content) {
handler_stack.push(current_handler);
for (const line of input) {
const handler = handler_stack[handler_stack.length - 1];
const handler = handler_stack.at(-1);
handler.handle_line(line);
}
// Clean up all trailing blocks by letting them
// insert closing fences
while (handler_stack.length !== 0) {
const handler = handler_stack[handler_stack.length - 1];
const handler = handler_stack.at(-1);
handler.done();
}
if (output.length > 2 && output[output.length - 2] !== "") {
if (output.length > 2 && output.at(-2) !== "") {
output.push("");
}