js: Use modern spread arguments syntax.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg
2020-02-11 16:35:16 -08:00
committed by Tim Abbott
parent 2475adbf8a
commit 59d55d1e06
18 changed files with 84 additions and 94 deletions

View File

@@ -308,11 +308,12 @@ run_test('content_typeahead_selected', () => {
};
let caret_called1 = false;
let caret_called2 = false;
fake_this.$element.caret = function (arg1, arg2) {
if (arguments.length === 0) { // .caret() used in split_at_cursor
fake_this.$element.caret = function (...args) {
if (args.length === 0) { // .caret() used in split_at_cursor
caret_called1 = true;
return fake_this.query.length;
}
const [arg1, arg2] = args;
// .caret() used in setTimeout
assert.equal(arg1, arg2);
caret_called2 = true;

View File

@@ -513,7 +513,7 @@ run_test('exit button on pill', () => {
};
const exit_click_handler = container.get_on_handler('click', '.exit');
exit_click_handler.apply(exit_button_stub, [e]);
exit_click_handler.call(exit_button_stub, e);
assert(next_pill_focused);
@@ -548,7 +548,7 @@ run_test('misc things', () => {
},
};
animation_end_handler.apply(input_stub);
animation_end_handler.call(input_stub);
assert(shake_class_removed);
// bad data

View File

@@ -124,10 +124,10 @@ run_test('status_from_timestamp', () => {
pushable: true,
};
let called = false;
blueslip.error = function () {
assert.equal(arguments[0], 'Unexpected status');
assert.deepEqual(arguments[1].presence_object, info.random_client);
assert.equal(arguments[2], undefined);
blueslip.error = function (msg, more_info, stack) {
assert.equal(msg, 'Unexpected status');
assert.deepEqual(more_info.presence_object, info.random_client);
assert.equal(stack, undefined);
called = true;
};
status = status_from_timestamp(

View File

@@ -79,7 +79,7 @@ function test_create_bot_type_input_box_toggle(f) {
const GENERIC_BOT_TYPE = '1';
$('#create_bot_type :selected').val(EMBEDDED_BOT_TYPE);
f.apply();
f();
assert(!create_payload_url.hasClass('required'));
assert(!payload_url_inputbox.visible());
assert($('#select_service_name').hasClass('required'));
@@ -87,13 +87,13 @@ function test_create_bot_type_input_box_toggle(f) {
assert(config_inputbox.visible());
$('#create_bot_type :selected').val(OUTGOING_WEBHOOK_BOT_TYPE);
f.apply();
f();
assert(create_payload_url.hasClass('required'));
assert(payload_url_inputbox.visible());
assert(!config_inputbox.visible());
$('#create_bot_type :selected').val(GENERIC_BOT_TYPE);
f.apply();
f();
assert(!create_payload_url.hasClass('required'));
assert(!payload_url_inputbox.visible());
assert(!config_inputbox.visible());

View File

@@ -429,7 +429,7 @@ function test_change_allow_subdomains(change_allow_subdomains) {
parents_obj.set_find_results('.domain', domain_obj);
elem_obj.prop('checked', allow);
change_allow_subdomains.apply(elem_obj, [ev]);
change_allow_subdomains.call(elem_obj, ev);
success_callback();
assert.equal(info.val(),
@@ -440,7 +440,7 @@ function test_change_allow_subdomains(change_allow_subdomains) {
allow = false;
elem_obj.prop('checked', allow);
change_allow_subdomains.apply(elem_obj, [ev]);
change_allow_subdomains.call(elem_obj, ev);
success_callback();
assert.equal(info.val(),
'translated: Update successful: Subdomains no longer allowed for example.com');

View File

@@ -54,10 +54,8 @@ exports.make_event_store = (selector) => {
return handler;
},
off: function () {
const event_name = arguments[0];
if (arguments.length === 1) {
off: function (event_name, ...args) {
if (args.length === 0) {
on_functions.delete(event_name);
return;
}
@@ -69,15 +67,12 @@ exports.make_event_store = (selector) => {
throw Error('zjquery does not support this call sequence');
},
on: function () {
on: function (event_name, ...args) {
// parameters will either be
// (event_name, handler) or
// (event_name, sel, handler)
const event_name = arguments[0];
let handler;
if (arguments.length === 2) {
handler = arguments[1];
if (args.length === 1) {
const [handler] = args;
if (on_functions.has(event_name)) {
console.info('\nEither the app or the test can be at fault here..');
console.info('(sometimes you just want to call $.clear_all_elements();)\n');
@@ -88,12 +83,11 @@ exports.make_event_store = (selector) => {
return;
}
if (arguments.length !== 3) {
if (args.length !== 2) {
throw Error('wrong number of arguments passed in');
}
const sel = arguments[1];
handler = arguments[2];
const [sel, handler] = args;
assert.equal(typeof sel, 'string', 'String selectors expected here.');
assert.equal(typeof handler, 'function', 'An handler function expected here.');
@@ -267,12 +261,12 @@ exports.make_new_elem = function (selector, opts) {
event_store.generic_event('keyup', arg);
return self;
},
off: function () {
event_store.off.apply(undefined, arguments);
off: function (...args) {
event_store.off(...args);
return self;
},
on: function () {
event_store.on.apply(undefined, arguments);
on: function (...args) {
event_store.on(...args);
return self;
},
parent: function () {
@@ -346,18 +340,18 @@ exports.make_new_elem = function (selector, opts) {
event_store.trigger(ev);
return self;
},
val: function () {
if (arguments.length === 0) {
val: function (...args) {
if (args.length === 0) {
return value || '';
}
value = arguments[0];
[value] = args;
return self;
},
css: function () {
if (arguments.length === 0) {
css: function (...args) {
if (args.length === 0) {
return css || {};
}
css = arguments[0];
[css] = args;
return self;
},
visible: function () {

View File

@@ -118,11 +118,11 @@ function scroll_finish() {
}
$(function () {
$.fn.safeOuterHeight = function () {
return $(this).outerHeight.apply(this, arguments) || 0;
$.fn.safeOuterHeight = function (...args) {
return this.outerHeight(...args) || 0;
};
$.fn.safeOuterWidth = function () {
return $(this).outerWidth.apply(this, arguments) || 0;
$.fn.safeOuterWidth = function (...args) {
return this.outerWidth(...args) || 0;
};
$('.app').scroll(_.throttle(function () {
scroll_finish();

View File

@@ -216,22 +216,22 @@ function build_arg_list(msg, more_info) {
exports.debug = function blueslip_debug(msg, more_info) {
const args = build_arg_list(msg, more_info);
logger.debug.apply(logger, args);
logger.debug(...args);
};
exports.log = function blueslip_log(msg, more_info) {
const args = build_arg_list(msg, more_info);
logger.log.apply(logger, args);
logger.log(...args);
};
exports.info = function blueslip_info(msg, more_info) {
const args = build_arg_list(msg, more_info);
logger.info.apply(logger, args);
logger.info(...args);
};
exports.warn = function blueslip_warn(msg, more_info) {
const args = build_arg_list(msg, more_info);
logger.warn.apply(logger, args);
logger.warn(...args);
if (page_params.debug_mode) {
console.trace();
}
@@ -242,7 +242,7 @@ exports.error = function blueslip_error(msg, more_info, stack) {
stack = Error().stack;
}
const args = build_arg_list(msg, more_info);
logger.error.apply(logger, args);
logger.error(...args);
report_error(msg, stack, {more_info: more_info});
if (page_params.debug_mode) {

View File

@@ -507,7 +507,7 @@ Filter.prototype = {
return sorted_terms;
},
can_bucket_by: function () {
can_bucket_by: function (...wanted_term_types) {
// TODO: in ES6 use spread operator
//
// Examples call:
@@ -520,7 +520,6 @@ Filter.prototype = {
// a predicate to a larger list of candidate ids.
//
// (It's for optimization, basically.)
const wanted_term_types = [].slice.call(arguments);
const all_term_types = this.sorted_term_types();
const term_types = all_term_types.slice(0, wanted_term_types.length);

View File

@@ -145,13 +145,14 @@ exports.create = function ($container, list, opts) {
// reset the data associated with a list. This is so that instead of
// initializing a new progressive list render instance, you can just
// update the data of an existing one.
data: function (data) {
data: function (...args) {
// if no args are provided then just return the existing data.
// this interface is similar to how many jQuery functions operate,
// where a call to the method without data returns the existing data.
if (typeof data === "undefined" && arguments.length === 0) {
if (args.length === 0) {
return meta.list;
}
const [data] = args;
if (Array.isArray(data)) {
meta.list = data;

View File

@@ -252,10 +252,10 @@ function make_dimen_wrapper(dimen_name, dimen_func) {
return dimen_func.call(exports.message_pane);
},
});
return function viewport_dimension_wrapper() {
if (arguments.length !== 0) {
return function viewport_dimension_wrapper(...args) {
if (args.length !== 0) {
dimensions[dimen_name].reset();
return dimen_func.apply(exports.message_pane, arguments);
return dimen_func.apply(exports.message_pane, args);
}
return dimensions[dimen_name].get();
};

View File

@@ -28,9 +28,9 @@ function elem_to_user_id(elem) {
// ones that no longer have valid parents in the DOM.
(function (popover) {
$.fn.popover = function () {
$.fn.popover = function (...args) {
// apply the jQuery object as `this`, and popover function arguments.
popover.apply(this, arguments);
popover.apply(this, args);
// if there is a valid "popover" key in the jQuery data object then
// push it to the array.

View File

@@ -21,12 +21,12 @@ $(function () {
// will no longer cast to a Number but rather NaN. For this, we create the
// `safeOuterHeight` and `safeOuterWidth` functions to safely return a result
// (or 0).
$.fn.safeOuterHeight = function () {
return $(this).outerHeight.apply(this, arguments) || 0;
$.fn.safeOuterHeight = function (...args) {
return this.outerHeight(...args) || 0;
};
$.fn.safeOuterWidth = function () {
return $(this).outerWidth.apply(this, arguments) || 0;
$.fn.safeOuterWidth = function (...args) {
return this.outerWidth(...args) || 0;
};
// For some reason, jQuery wants this to be attached to an element.

View File

@@ -472,7 +472,7 @@ function populate_messages_sent_by_client(data) {
$('#id_messages_sent_by_client > div').removeClass("spinner");
const data_ = plot_data[user_button][time_button];
layout.height = layout.margin.b + data_.trace.x.length * 30;
layout.xaxis.range = [0, Math.max.apply(null, data_.trace.x) * 1.3];
layout.xaxis.range = [0, Math.max(...data_.trace.x) * 1.3];
Plotly.newPlot('id_messages_sent_by_client',
[data_.trace, data_.trace_annotations],
layout,

View File

@@ -437,11 +437,11 @@ exports.filter_table = function (query) {
ui.reset_scrollbar($("#subscription_overlay .streams-list"));
const all_stream_ids = [].concat(
buckets.name,
buckets.desc,
buckets.other
);
const all_stream_ids = [
...buckets.name,
...buckets.desc,
...buckets.other,
];
for (const stream_id of all_stream_ids) {
ui.get_content_element($('#subscriptions_table .streams-list')).append(widgets[stream_id]);

View File

@@ -16,31 +16,31 @@ Handlebars.registerHelper('plural', function (condition, one, other) {
Handlebars.registerHelper({
eq: function (a, b) { return a === b; },
and: function () {
// last argument is Handlebars options
if (arguments.length < 2) {
and: function (...args) {
args.pop(); // Handlebars options
if (args.length === 0) {
return true;
}
let i;
for (i = 0; i < arguments.length - 2; i += 1) {
if (!arguments[i] || Handlebars.Utils.isEmpty(arguments[i])) {
return arguments[i];
const last = args.pop();
for (const arg of args) {
if (!arg || Handlebars.Utils.isEmpty(arg)) {
return arg;
}
}
return arguments[i];
return last;
},
or: function () {
// last argument is Handlebars options
if (arguments.length < 2) {
or: function (...args) {
args.pop(); // Handlebars options
if (args.length === 0) {
return false;
}
let i;
for (i = 0; i < arguments.length - 2; i += 1) {
if (arguments[i] && !Handlebars.Utils.isEmpty(arguments[i])) {
return arguments[i];
const last = args.pop();
for (const arg of args) {
if (arg && !Handlebars.Utils.isEmpty(arg)) {
return arg;
}
}
return arguments[i];
return last;
},
not: function (a) { return !a || Handlebars.Utils.isEmpty(a); },
});

View File

@@ -157,15 +157,12 @@ exports.unread_pm_counter = (function () {
};
self.get_msg_ids = function () {
const lists = [];
const ids = [];
for (const id_set of bucketer.values()) {
const members = Array.from(id_set);
lists.push(members);
ids.push(...Array.from(id_set));
}
const ids = [].concat.apply([], lists);
return util.sorted_ids(ids);
};
@@ -333,16 +330,14 @@ exports.unread_topic_counter = (function () {
return [];
}
const topic_lists = [];
const ids = [];
const sub = stream_data.get_sub_by_id(stream_id);
for (const [topic, msgs] of per_stream_bucketer) {
if (sub && !muting.is_topic_muted(stream_id, topic)) {
topic_lists.push(Array.from(msgs));
ids.push(...Array.from(msgs));
}
}
const ids = [].concat.apply([], topic_lists);
return util.sorted_ids(ids);
};

View File

@@ -170,9 +170,9 @@ exports.sort_emojis = function (objs, query) {
(x) => x.emoji_name
);
return [].concat(
popular_emoji_matches,
triage_results.matches,
triage_results.rest
);
return [
...popular_emoji_matches,
...triage_results.matches,
...triage_results.rest,
];
};