Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

eslint: Fix unicorn/prefer-string-slice.

Anders Kaseorg48f5e5179a1d
diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.js
index 156ab76f68a..b68e3c85458 100644
--- a/frontend_tests/node_tests/compose.js
+++ b/frontend_tests/node_tests/compose.js
@@ -453,9 +453,9 @@ run_test("markdown_shortcuts", () => {
const compose_textarea = $("#compose-textarea");
const value = compose_textarea.val();
$("#compose-textarea").val(
- value.substring(0, compose_textarea.range().start) +
+ value.slice(0, compose_textarea.range().start) +
markdown +
- value.substring(compose_textarea.range().end, value.length),
+ value.slice(compose_textarea.range().end),
);
};
@@ -467,7 +467,7 @@ run_test("markdown_shortcuts", () => {
range: noop,
text: $("#compose-textarea")
.val()
- .substring(range_start, range_length + range_start),
+ .slice(range_start, range_length + range_start),
};
};
$("#compose-textarea").caret = noop;
diff --git a/frontend_tests/node_tests/typeahead_helper.js b/frontend_tests/node_tests/typeahead_helper.js
index 97304480cee..1a8a5148dcf 100644
--- a/frontend_tests/node_tests/typeahead_helper.js
+++ b/frontend_tests/node_tests/typeahead_helper.js
@@ -556,7 +556,7 @@ run_test("render_stream", () => {
global.stub_templates((template_name, args) => {
assert.equal(template_name, "typeahead_list_item");
assert.equal(args.primary, stream.name);
- const short_desc = stream.description.substring(0, 35);
+ const short_desc = stream.description.slice(0, 35);
assert.equal(args.secondary, short_desc + "...");
rendered = true;
return "typeahead-item-stub";
diff --git a/static/js/alert_words.js b/static/js/alert_words.js
index e4ca89259e8..96e4a3e64db 100644
--- a/static/js/alert_words.js
+++ b/static/js/alert_words.js
@@ -45,12 +45,12 @@ exports.process_message = function (message) {
regex,
(match, before, word, after, offset, content) => {
// Logic for ensuring that we don't muck up rendered HTML.
- const pre_match = content.substring(0, offset);
+ const pre_match = content.slice(0, offset);
// We want to find the position of the `<` and `>` only in the
// match and the string before it. So, don't include the last
// character of match in `check_string`. This covers the corner
// case when there is an alert word just before `<` or `>`.
- const check_string = pre_match + match.substring(0, match.length - 1);
+ const check_string = pre_match + match.slice(0, -1);
const in_tag = check_string.lastIndexOf("<") > check_string.lastIndexOf(">");
// Matched word is inside a HTML tag so don't perform any highlighting.
if (in_tag === true) {
diff --git a/static/js/colorspace.js b/static/js/colorspace.js
index 5b2d6027ddd..5a0e0e50c17 100644
--- a/static/js/colorspace.js
+++ b/static/js/colorspace.js
@@ -35,9 +35,9 @@ exports.luminance_to_lightness = function (luminance) {
exports.getDecimalColor = function (hexcolor) {
return {
- r: Number.parseInt(hexcolor.substr(1, 2), 16),
- g: Number.parseInt(hexcolor.substr(3, 2), 16),
- b: Number.parseInt(hexcolor.substr(5, 2), 16),
+ r: Number.parseInt(hexcolor.slice(1, 3), 16),
+ g: Number.parseInt(hexcolor.slice(3, 5), 16),
+ b: Number.parseInt(hexcolor.slice(5, 7), 16),
};
};
diff --git a/static/js/composebox_typeahead.js b/static/js/composebox_typeahead.js
index 8401c5b0c13..33b7ca14c95 100644
--- a/static/js/composebox_typeahead.js
+++ b/static/js/composebox_typeahead.js
@@ -289,7 +289,10 @@ exports.tokenize_compose_str = function (s) {
// this will reposition the user from. If | is the cursor, implements:
//
// `#**stream name** >|` => `#**stream name>|`.
- if (s.substring(i - 2, i) === "**" || s.substring(i - 3, i) === "** ") {
+ if (
+ s.slice(Math.max(0, i - 2), i) === "**" ||
+ s.slice(Math.max(0, i - 3), i) === "** "
+ ) {
// return any string as long as its not ''.
return ">topic_jump";
}
@@ -328,9 +331,9 @@ exports.broadcast_mentions = function () {
function filter_mention_name(current_token) {
if (current_token.startsWith("**")) {
- current_token = current_token.substring(2);
+ current_token = current_token.slice(2);
} else if (current_token.startsWith("*")) {
- current_token = current_token.substring(1);
+ current_token = current_token.slice(1);
}
if (current_token.length < 1 || current_token.lastIndexOf("*") !== -1) {
return false;
@@ -580,7 +583,7 @@ exports.get_candidates = function (query) {
}
// Start syntax highlighting autocompleter if the first three characters are ```
- const syntax_token = current_token.substring(0, 3);
+ const syntax_token = current_token.slice(0, 3);
if (this.options.completions.syntax && (syntax_token === "```" || syntax_token === "~~~")) {
// Only autocomplete if user starts typing a language after ```
if (current_token.length === 3) {
@@ -588,14 +591,14 @@ exports.get_candidates = function (query) {
}
// If the only input is a space, don't autocomplete
- current_token = current_token.substring(3);
+ current_token = current_token.slice(3);
if (current_token === " ") {
return false;
}
// Trim the first whitespace if it is there
if (current_token[0] === " ") {
- current_token = current_token.substring(1);
+ current_token = current_token.slice(1);
}
this.completing = "syntax";
this.token = current_token;
@@ -617,19 +620,19 @@ exports.get_candidates = function (query) {
return false;
}
this.completing = "emoji";
- this.token = current_token.substring(1);
+ this.token = current_token.slice(1);
return exports.emoji_collection;
}
if (this.options.completions.mention && current_token[0] === "@") {
- current_token = current_token.substring(1);
+ current_token = current_token.slice(1);
this.completing = "mention";
// Silent mentions
let is_silent = false;
if (current_token.startsWith("_")) {
this.completing = "silent_mention";
is_silent = true;
- current_token = current_token.substring(1);
+ current_token = current_token.slice(1);
}
current_token = filter_mention_name(current_token);
if (!current_token) {
@@ -646,7 +649,7 @@ exports.get_candidates = function (query) {
}
if (this.options.completions.slash && current_token[0] === "/") {
- current_token = current_token.substring(1);
+ current_token = current_token.slice(1);
this.completing = "slash";
this.token = current_token;
@@ -658,9 +661,9 @@ exports.get_candidates = function (query) {
return false;
}
- current_token = current_token.substring(1);
+ current_token = current_token.slice(1);
if (current_token.startsWith("**")) {
- current_token = current_token.substring(2);
+ current_token = current_token.slice(2);
}
// Don't autocomplete if there is a space following a '#'
@@ -780,27 +783,19 @@ exports.content_typeahead_selected = function (item, event) {
beginning.charAt(beginning.lastIndexOf(":") - 1) === " " ||
beginning.charAt(beginning.lastIndexOf(":") - 1) === "\n"
) {
- beginning =
- beginning.substring(0, beginning.length - this.token.length - 1) +
- ":" +
- item.emoji_name +
- ": ";
+ beginning = beginning.slice(0, -this.token.length - 1) + ":" + item.emoji_name + ": ";
} else {
- beginning =
- beginning.substring(0, beginning.length - this.token.length - 1) +
- " :" +
- item.emoji_name +
- ": ";
+ beginning = beginning.slice(0, -this.token.length - 1) + " :" + item.emoji_name + ": ";
}
} else if (this.completing === "mention" || this.completing === "silent_mention") {
const is_silent = this.completing === "silent_mention";
- beginning = beginning.substring(0, beginning.length - this.token.length - 1);
+ beginning = beginning.slice(0, -this.token.length - 1);
if (beginning.endsWith("@_*")) {
- beginning = beginning.substring(0, beginning.length - 3);
+ beginning = beginning.slice(0, -3);
} else if (beginning.endsWith("@*") || beginning.endsWith("@_")) {
- beginning = beginning.substring(0, beginning.length - 2);
+ beginning = beginning.slice(0, -2);
} else if (beginning.endsWith("@")) {
- beginning = beginning.substring(0, beginning.length - 1);
+ beginning = beginning.slice(0, -1);
}
if (user_groups.is_user_group(item)) {
beginning += "@*" + item.name + "* ";
@@ -817,15 +812,11 @@ exports.content_typeahead_selected = function (item, event) {
}
}
} else if (this.completing === "slash") {
- beginning =
- beginning.substring(0, beginning.length - this.token.length - 1) +
- "/" +
- item.name +
- " ";
+ beginning = beginning.slice(0, -this.token.length - 1) + "/" + item.name + " ";
} else if (this.completing === "stream") {
- beginning = beginning.substring(0, beginning.length - this.token.length - 1);
+ beginning = beginning.slice(0, -this.token.length - 1);
if (beginning.endsWith("#*")) {
- beginning = beginning.substring(0, beginning.length - 2);
+ beginning = beginning.slice(0, -2);
}
beginning += "#**" + item.name;
if (event && event.key === ">") {
@@ -845,12 +836,12 @@ exports.content_typeahead_selected = function (item, event) {
// If cursor is at end of input ("rest" is empty), then
// complete the token before the cursor, and add a closing fence
// after the cursor
- beginning = beginning.substring(0, backticks) + item + "\n";
- rest = "\n" + beginning.substring(backticks - 4, backticks).trim() + rest;
+ beginning = beginning.slice(0, backticks) + item + "\n";
+ rest = "\n" + beginning.slice(Math.max(0, backticks - 4), backticks).trim() + rest;
} else {
// If more text after the input, then complete the token, but don't touch
// "rest" (i.e. do not add a closing fence)
- beginning = beginning.substring(0, backticks) + item;
+ beginning = beginning.slice(0, backticks) + item;
}
} else if (this.completing === "topic_jump") {
// Put the cursor at the end of immediately preceding stream mention syntax,
@@ -858,24 +849,25 @@ exports.content_typeahead_selected = function (item, event) {
// final ** and set things up for the topic_list typeahead.
const index = beginning.lastIndexOf("**");
if (index !== -1) {
- beginning = beginning.substring(0, index) + ">";
+ beginning = beginning.slice(0, index) + ">";
}
} else if (this.completing === "topic_list") {
// Stream + topic mention typeahead; close the stream+topic mention syntax
// with the topic and the final **.
- const start = beginning.length - this.token.length;
- beginning = beginning.substring(0, start) + item + "** ";
+ const start = -this.token.length;
+ beginning = beginning.slice(0, start) + item + "** ";
} else if (this.completing === "time_jump") {
- let timestring = beginning.substring(beginning.lastIndexOf("<time:"));
+ let timestring = beginning.slice(Math.max(0, beginning.lastIndexOf("<time:")));
if (timestring.startsWith("<time:") && timestring.endsWith(">")) {
- timestring = timestring.substring(6, timestring.length - 1);
+ timestring = timestring.slice(6, -1);
}
const timestamp = timerender.get_timestamp_for_flatpickr(timestring);
const on_timestamp_selection = (val) => {
const datestr = val;
beginning =
- beginning.substring(0, beginning.lastIndexOf("<time")) + `<time:${datestr}> `;
+ beginning.slice(0, Math.max(0, beginning.lastIndexOf("<time"))) +
+ `<time:${datestr}> `;
if (rest.startsWith(">")) {
rest = rest.slice(1);
}
diff --git a/static/js/notifications.js b/static/js/notifications.js
index 4e6958e8423..55e177c2bbb 100644
--- a/static/js/notifications.js
+++ b/static/js/notifications.js
@@ -358,7 +358,7 @@ function process_notification(notification) {
break;
}
}
- content = content.substring(0, i);
+ content = content.slice(0, i);
content += " [...]";
}
diff --git a/static/js/pill_typeahead.js b/static/js/pill_typeahead.js
index 2d584802330..3d5a23814a6 100644
--- a/static/js/pill_typeahead.js
+++ b/static/js/pill_typeahead.js
@@ -33,7 +33,7 @@ exports.set_up = function (input, pills, opts) {
query = query.replace(/\u00A0/g, String.fromCharCode(32));
if (include_streams(query)) {
- query = query.trim().substring(1);
+ query = query.trim().slice(1);
return item.name.toLowerCase().indexOf(query) !== -1;
}
@@ -47,7 +47,7 @@ exports.set_up = function (input, pills, opts) {
},
sorter(matches) {
if (include_streams(this.query)) {
- return typeahead_helper.sort_streams(matches, this.query.trim().substring(1));
+ return typeahead_helper.sort_streams(matches, this.query.trim().slice(1));
}
return typeahead_helper.sort_recipientbox_typeahead(this.query, matches, "");
diff --git a/static/js/portico/dev-login.js b/static/js/portico/dev-login.js
index 41929db274b..d32fadf1924 100644
--- a/static/js/portico/dev-login.js
+++ b/static/js/portico/dev-login.js
@@ -4,7 +4,7 @@ $(() => {
// This code will be executed when the user visits /login and
// dev_login.html is rendered.
if ($("[data-page-id='dev-login']").length > 0) {
- if (window.location.hash.substring(0, 1) === "#") {
+ if (window.location.hash.slice(0, 1) === "#") {
/* We append the location.hash to the input field with name next so that URL can be
preserved after user is logged in. See this:
https://stackoverflow.com/questions/5283395/url-hash-is-persisting-between-redirects */
diff --git a/static/js/portico/signup.js b/static/js/portico/signup.js
index 65a89550b31..47c36252ac2 100644
--- a/static/js/portico/signup.js
+++ b/static/js/portico/signup.js
@@ -96,7 +96,7 @@ $(() => {
// Code in this block will be executed when the user visits /register
// i.e. accounts_home.html is rendered.
if ($("[data-page-id='accounts-home']").length > 0) {
- if (window.location.hash.substring(0, 1) === "#") {
+ if (window.location.hash.slice(0, 1) === "#") {
document.email_form.action += window.location.hash;
}
}
@@ -104,7 +104,7 @@ $(() => {
// Code in this block will be executed when the user is at login page
// i.e. login.html is rendered.
if ($("[data-page-id='login-page']").length > 0) {
- if (window.location.hash.substring(0, 1) === "#") {
+ if (window.location.hash.slice(0, 1) === "#") {
/* We append the location.hash to the formaction so that URL can be
preserved after user is logged in. See this:
https://stackoverflow.com/questions/5283395/url-hash-is-persisting-between-redirects */
diff --git a/static/js/reload.js b/static/js/reload.js
index 98f434ebbdc..fa0408d4173 100644
--- a/static/js/reload.js
+++ b/static/js/reload.js
@@ -93,7 +93,7 @@ function preserve_state(send_after_reload, save_pointer, save_narrow, save_compo
// done before the first call to get_events
exports.initialize = function () {
const location = window.location.toString();
- const hash_fragment = location.substring(location.indexOf("#") + 1);
+ const hash_fragment = location.slice(location.indexOf("#") + 1);
// hash_fragment should be e.g. `reload:12345123412312`
if (hash_fragment.search("reload:") !== 0) {
diff --git a/static/js/rtl.js b/static/js/rtl.js
index f1b8083c043..0da1a13a752 100644
--- a/static/js/rtl.js
+++ b/static/js/rtl.js
@@ -158,7 +158,7 @@ exports.set_rtl_class_for_textarea = function (textarea) {
// Set the rtl class if the text has an rtl direction, remove it otherwise
let text = textarea.val();
if (text.startsWith("```quote")) {
- text = text.substr(8);
+ text = text.slice(8);
}
if (exports.get_direction(text) === "rtl") {
textarea.addClass("rtl");
diff --git a/static/js/search_suggestion.js b/static/js/search_suggestion.js
index ab4f197984b..bd8e0b1379f 100644
--- a/static/js/search_suggestion.js
+++ b/static/js/search_suggestion.js
@@ -406,7 +406,7 @@ function get_special_filter_suggestions(last, operators, suggestions) {
}
// returns the substring after the ":" symbol.
- const suggestion_operand = s.search_string.substring(s.search_string.indexOf(":") + 1);
+ const suggestion_operand = s.search_string.slice(s.search_string.indexOf(":") + 1);
// e.g for `att` search query, `has:attachment` should be suggested.
const show_operator_suggestions =
last.operator === "search" && suggestion_operand.toLowerCase().startsWith(last_string);
diff --git a/static/js/stream_pill.js b/static/js/stream_pill.js
index 072bb6f6c65..8fa5351b2e1 100644
--- a/static/js/stream_pill.js
+++ b/static/js/stream_pill.js
@@ -9,7 +9,7 @@ exports.create_item_from_stream_name = function (stream_name, current_items) {
if (!stream_name.startsWith("#")) {
return undefined;
}
- stream_name = stream_name.substring(1);
+ stream_name = stream_name.slice(1);
const sub = stream_data.get_sub(stream_name);
if (!sub) {
diff --git a/static/js/subs.js b/static/js/subs.js
index d82e7e17ef9..acbc6733679 100644
--- a/static/js/subs.js
+++ b/static/js/subs.js
@@ -64,7 +64,7 @@ exports.get_active_data = function () {
function get_hash_safe() {
if (typeof window !== "undefined" && typeof window.location.hash === "string") {
- return window.location.hash.substr(1);
+ return window.location.hash.slice(1);
}
return "";
@@ -94,7 +94,7 @@ function should_list_all_streams() {
// this finds the stream that is actively open in the settings and focused in
// the left side.
exports.active_stream = function () {
- const hash_components = window.location.hash.substr(1).split(/\//);
+ const hash_components = window.location.hash.slice(1).split(/\//);
// if the string casted to a number is valid, and another component
// after exists then it's a stream name/id pair.
diff --git a/static/js/typeahead_helper.js b/static/js/typeahead_helper.js
index 6bc39327647..16cac154e65 100644
--- a/static/js/typeahead_helper.js
+++ b/static/js/typeahead_helper.js
@@ -131,7 +131,7 @@ exports.clear_rendered_stream = function (stream_id) {
exports.render_stream = function (stream) {
let desc = stream.description;
- const short_desc = desc.substring(0, 35);
+ const short_desc = desc.slice(0, 35);
if (desc !== short_desc) {
desc = short_desc + "...";
diff --git a/static/js/util.js b/static/js/util.js
index bd123006d1b..099c3b18f59 100644
--- a/static/js/util.js
+++ b/static/js/util.js
@@ -124,7 +124,7 @@ exports.robust_uri_decode = function (str) {
let end = str.length;
while (end > 0) {
try {
- return decodeURIComponent(str.substring(0, end));
+ return decodeURIComponent(str.slice(0, end));
} catch (e) {
if (!(e instanceof URIError)) {
throw e;