Replace deprecated String#substr with String#slice (#8314)

This commit is contained in:
CommanderRoot 2022-04-14 09:52:42 +02:00 committed by GitHub
parent 0e68c16a90
commit c35fc169f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 37 additions and 37 deletions

View file

@ -57,7 +57,7 @@ function isValidHexColor(color: string): boolean {
return typeof color === "string" && return typeof color === "string" &&
(color.length === 7 || color.length === 9) && (color.length === 7 || color.length === 9) &&
color.charAt(0) === "#" && color.charAt(0) === "#" &&
!color.substr(1).split("").some(c => isNaN(parseInt(c, 16))); !color.slice(1).split("").some(c => isNaN(parseInt(c, 16)));
} }
function urlForColor(color: string): string { function urlForColor(color: string): string {

View file

@ -55,7 +55,7 @@ export default class CommandProvider extends AutocompleteProvider {
// check if the full match differs from the first word (i.e. returns false if the command has args) // check if the full match differs from the first word (i.e. returns false if the command has args)
if (command[0] !== command[1]) { if (command[0] !== command[1]) {
// The input looks like a command with arguments, perform exact match // The input looks like a command with arguments, perform exact match
const name = command[1].substr(1); // strip leading `/` const name = command[1].slice(1); // strip leading `/`
if (CommandMap.has(name) && CommandMap.get(name).isEnabled()) { if (CommandMap.has(name) && CommandMap.get(name).isEnabled()) {
// some commands, namely `me` don't suit having the usage shown whilst typing their arguments // some commands, namely `me` don't suit having the usage shown whilst typing their arguments
if (CommandMap.get(name).hideCompletionAfterSpace) return []; if (CommandMap.get(name).hideCompletionAfterSpace) return [];

View file

@ -103,7 +103,7 @@ export default class CreateRoomDialog extends React.Component<IProps, IState> {
createOpts.preset = Preset.PublicChat; createOpts.preset = Preset.PublicChat;
opts.guestAccess = false; opts.guestAccess = false;
const { alias } = this.state; const { alias } = this.state;
createOpts.room_alias_name = alias.substr(1, alias.indexOf(":") - 1); createOpts.room_alias_name = alias.substring(1, alias.indexOf(":"));
} else { } else {
// If we cannot change encryption we pass `true` for safety, the server should automatically do this for us. // If we cannot change encryption we pass `true` for safety, the server should automatically do this for us.
opts.encryption = this.state.canChangeEncryption ? this.state.isEncrypted : true; opts.encryption = this.state.canChangeEncryption ? this.state.isEncrypted : true;

View file

@ -55,7 +55,7 @@ const EffectsOverlay: FunctionComponent<IProps> = ({ roomWidth }) => {
const onAction = (payload: { action: string }) => { const onAction = (payload: { action: string }) => {
const actionPrefix = 'effects.'; const actionPrefix = 'effects.';
if (payload.action.indexOf(actionPrefix) === 0) { if (payload.action.indexOf(actionPrefix) === 0) {
const effect = payload.action.substr(actionPrefix.length); const effect = payload.action.slice(actionPrefix.length);
lazyLoadEffectModule(effect).then((module) => module?.start(canvasRef.current)); lazyLoadEffectModule(effect).then((module) => module?.start(canvasRef.current));
} }
}; };

View file

@ -364,7 +364,7 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
private insertText(textToInsert: string, inputType = "insertText"): void { private insertText(textToInsert: string, inputType = "insertText"): void {
const sel = document.getSelection(); const sel = document.getSelection();
const { caret, text } = getCaretOffsetAndText(this.editorRef.current, sel); const { caret, text } = getCaretOffsetAndText(this.editorRef.current, sel);
const newText = text.substr(0, caret.offset) + textToInsert + text.substr(caret.offset); const newText = text.slice(0, caret.offset) + textToInsert + text.slice(caret.offset);
caret.offset += textToInsert.length; caret.offset += textToInsert.length;
this.modifiedFlag = true; this.modifiedFlag = true;
this.props.model.update(newText, inputType, caret); this.props.model.update(newText, inputType, caret);

View file

@ -274,7 +274,7 @@ export default class MemberList extends React.Component<IProps, IState> {
this.sortNames.set( this.sortNames.set(
member, member,
(member.name[0] === '@' ? member.name.substr(1) : member.name).replace(SORT_REGEX, ""), (member.name[0] === '@' ? member.name.slice(1) : member.name).replace(SORT_REGEX, ""),
); );
// XXX: this user may have no lastPresenceTs value! // XXX: this user may have no lastPresenceTs value!

View file

@ -56,7 +56,7 @@ export const createSpace = async (
events_default: 100, events_default: 100,
invite: isPublic ? 0 : 50, invite: isPublic ? 0 : 50,
}, },
room_alias_name: isPublic && alias ? alias.substr(1, alias.indexOf(":") - 1) : undefined, room_alias_name: isPublic && alias ? alias.substring(1, alias.indexOf(":")) : undefined,
topic, topic,
...createOpts, ...createOpts,
}, },

View file

@ -97,7 +97,7 @@ function parseCodeBlock(n: Node, pc: PartCreator): Part[] {
if (n.firstChild?.nodeName === "CODE") { if (n.firstChild?.nodeName === "CODE") {
for (const className of (n.firstChild as HTMLElement).classList) { for (const className of (n.firstChild as HTMLElement).classList) {
if (className.startsWith("language-") && !className.startsWith("language-_")) { if (className.startsWith("language-") && !className.startsWith("language-_")) {
language = className.substr("language-".length); language = className.slice("language-".length);
break; break;
} }
} }
@ -118,7 +118,7 @@ function parseCodeBlock(n: Node, pc: PartCreator): Part[] {
} }
function parseHeader(n: Node, pc: PartCreator): Part[] { function parseHeader(n: Node, pc: PartCreator): Part[] {
const depth = parseInt(n.nodeName.substr(1), 10); const depth = parseInt(n.nodeName.slice(1), 10);
const prefix = pc.plain("#".repeat(depth) + " "); const prefix = pc.plain("#".repeat(depth) + " ");
return [prefix, ...parseChildren(n, pc)]; return [prefix, ...parseChildren(n, pc)];
} }

View file

@ -33,16 +33,16 @@ function firstDiff(a: string, b: string): number {
function diffStringsAtEnd(oldStr: string, newStr: string): IDiff { function diffStringsAtEnd(oldStr: string, newStr: string): IDiff {
const len = Math.min(oldStr.length, newStr.length); const len = Math.min(oldStr.length, newStr.length);
const startInCommon = oldStr.substr(0, len) === newStr.substr(0, len); const startInCommon = oldStr.slice(0, len) === newStr.slice(0, len);
if (startInCommon && oldStr.length > newStr.length) { if (startInCommon && oldStr.length > newStr.length) {
return { removed: oldStr.substr(len), at: len }; return { removed: oldStr.slice(len), at: len };
} else if (startInCommon && oldStr.length < newStr.length) { } else if (startInCommon && oldStr.length < newStr.length) {
return { added: newStr.substr(len), at: len }; return { added: newStr.slice(len), at: len };
} else { } else {
const commonStartLen = firstDiff(oldStr, newStr); const commonStartLen = firstDiff(oldStr, newStr);
return { return {
removed: oldStr.substr(commonStartLen), removed: oldStr.slice(commonStartLen),
added: newStr.substr(commonStartLen), added: newStr.slice(commonStartLen),
at: commonStartLen, at: commonStartLen,
}; };
} }
@ -55,7 +55,7 @@ export function diffDeletion(oldStr: string, newStr: string): IDiff {
} }
const firstDiffIdx = firstDiff(oldStr, newStr); const firstDiffIdx = firstDiff(oldStr, newStr);
const amount = oldStr.length - newStr.length; const amount = oldStr.length - newStr.length;
return { at: firstDiffIdx, removed: oldStr.substr(firstDiffIdx, amount) }; return { at: firstDiffIdx, removed: oldStr.slice(firstDiffIdx, firstDiffIdx + amount) };
} }
/** /**
@ -70,7 +70,7 @@ export function diffDeletion(oldStr: string, newStr: string): IDiff {
export function diffAtCaret(oldValue: string, newValue: string, caretPosition: number): IDiff { export function diffAtCaret(oldValue: string, newValue: string, caretPosition: number): IDiff {
const diffLen = newValue.length - oldValue.length; const diffLen = newValue.length - oldValue.length;
const caretPositionBeforeInput = caretPosition - diffLen; const caretPositionBeforeInput = caretPosition - diffLen;
const oldValueBeforeCaret = oldValue.substr(0, caretPositionBeforeInput); const oldValueBeforeCaret = oldValue.substring(0, caretPositionBeforeInput);
const newValueBeforeCaret = newValue.substr(0, caretPosition); const newValueBeforeCaret = newValue.substring(0, caretPosition);
return diffStringsAtEnd(oldValueBeforeCaret, newValueBeforeCaret); return diffStringsAtEnd(oldValueBeforeCaret, newValueBeforeCaret);
} }

View file

@ -281,7 +281,7 @@ export function toggleInlineFormat(range: Range, prefix: string, suffix = prefix
if (isFormatted) { if (isFormatted) {
// remove prefix and suffix formatting string // remove prefix and suffix formatting string
const partWithoutPrefix = parts[base].serialize(); const partWithoutPrefix = parts[base].serialize();
partWithoutPrefix.text = partWithoutPrefix.text.substr(prefix.length); partWithoutPrefix.text = partWithoutPrefix.text.slice(prefix.length);
parts[base] = partCreator.deserializePart(partWithoutPrefix); parts[base] = partCreator.deserializePart(partWithoutPrefix);
const partWithoutSuffix = parts[index - 1].serialize(); const partWithoutSuffix = parts[index - 1].serialize();

View file

@ -106,8 +106,8 @@ abstract class BasePart {
} }
public split(offset: number): IBasePart { public split(offset: number): IBasePart {
const splitText = this.text.substr(offset); const splitText = this.text.slice(offset);
this._text = this.text.substr(0, offset); this._text = this.text.slice(0, offset);
return new PlainPart(splitText); return new PlainPart(splitText);
} }
@ -115,7 +115,7 @@ abstract class BasePart {
// if the part would become invalid if it removed everything. // if the part would become invalid if it removed everything.
public remove(offset: number, len: number): string | undefined { public remove(offset: number, len: number): string | undefined {
// validate // validate
const strWithRemoval = this.text.substr(0, offset) + this.text.substr(offset + len); const strWithRemoval = this.text.slice(0, offset) + this.text.slice(offset + len);
for (let i = offset; i < (len + offset); ++i) { for (let i = offset; i < (len + offset); ++i) {
const chr = this.text.charAt(i); const chr = this.text.charAt(i);
if (!this.acceptsRemoval(i, chr)) { if (!this.acceptsRemoval(i, chr)) {
@ -131,8 +131,8 @@ abstract class BasePart {
for (let i = 0; i < str.length; ++i) { for (let i = 0; i < str.length; ++i) {
const chr = str.charAt(i); const chr = str.charAt(i);
if (!this.acceptsInsertion(chr, offset + i, inputType)) { if (!this.acceptsInsertion(chr, offset + i, inputType)) {
this._text = this._text + str.substr(0, i); this._text = this._text + str.slice(0, i);
return str.substr(i); return str.slice(i);
} }
} }
this._text = this._text + str; this._text = this._text + str;
@ -147,8 +147,8 @@ abstract class BasePart {
return false; return false;
} }
} }
const beforeInsert = this._text.substr(0, offset); const beforeInsert = this._text.slice(0, offset);
const afterInsert = this._text.substr(offset); const afterInsert = this._text.slice(offset);
this._text = beforeInsert + str + afterInsert; this._text = beforeInsert + str + afterInsert;
return true; return true;
} }
@ -156,8 +156,8 @@ abstract class BasePart {
public createAutoComplete(updateCallback: UpdateCallback): void {} public createAutoComplete(updateCallback: UpdateCallback): void {}
protected trim(len: number): string { protected trim(len: number): string {
const remaining = this._text.substr(len); const remaining = this._text.slice(len);
this._text = this._text.substr(0, len); this._text = this._text.slice(0, len);
return remaining; return remaining;
} }

View file

@ -290,7 +290,7 @@ export function replaceByRegexes(text: string, mapping: IVariables | Tags): stri
matchFoundSomewhere = true; matchFoundSomewhere = true;
// The textual part before the first match // The textual part before the first match
const head = inputText.substr(0, match.index); const head = inputText.slice(0, match.index);
const parts = []; const parts = [];
// keep track of prevMatch // keep track of prevMatch
@ -326,9 +326,9 @@ export function replaceByRegexes(text: string, mapping: IVariables | Tags): stri
let tail; let tail;
if (match) { if (match) {
const startIndex = prevMatch.index + prevMatch[0].length; const startIndex = prevMatch.index + prevMatch[0].length;
tail = inputText.substr(startIndex, match.index - startIndex); tail = inputText.slice(startIndex, match.index);
} else { } else {
tail = inputText.substr(prevMatch.index + prevMatch[0].length); tail = inputText.slice(prevMatch.index + prevMatch[0].length);
} }
if (tail) { if (tail) {
parts.push(tail); parts.push(tail);
@ -500,7 +500,7 @@ export function pickBestLanguage(langs: string[]): string {
{ {
// Failing that, a different dialect of the same language // Failing that, a different dialect of the same language
const closeLangIndex = normalisedLangs.findIndex((l) => l.substr(0, 2) === currentLang.substr(0, 2)); const closeLangIndex = normalisedLangs.findIndex((l) => l.slice(0, 2) === currentLang.slice(0, 2));
if (closeLangIndex > -1) return langs[closeLangIndex]; if (closeLangIndex > -1) return langs[closeLangIndex];
} }

View file

@ -115,7 +115,7 @@ export class ElementWidget extends Widget {
let theme = new ThemeWatcher().getEffectiveTheme(); let theme = new ThemeWatcher().getEffectiveTheme();
if (theme.startsWith("custom-")) { if (theme.startsWith("custom-")) {
const customTheme = getCustomTheme(theme.substr(7)); const customTheme = getCustomTheme(theme.slice(7));
// Jitsi only understands light/dark // Jitsi only understands light/dark
theme = customTheme.is_dark ? "dark" : "light"; theme = customTheme.is_dark ? "dark" : "light";
} }

View file

@ -230,7 +230,7 @@ export async function setTheme(theme?: string): Promise<void> {
clearCustomTheme(); clearCustomTheme();
let stylesheetName = theme; let stylesheetName = theme;
if (theme.startsWith("custom-")) { if (theme.startsWith("custom-")) {
const customTheme = getCustomTheme(theme.substr(7)); const customTheme = getCustomTheme(theme.slice(7));
stylesheetName = customTheme.is_dark ? "dark-custom" : "light-custom"; stylesheetName = customTheme.is_dark ? "dark-custom" : "light-custom";
setCustomThemeVars(customTheme); setCustomThemeVars(customTheme);
} }

View file

@ -59,7 +59,7 @@ describe("PosthogAnalytics", () => {
const hexHash = shaHashes[message]; const hexHash = shaHashes[message];
const bytes = []; const bytes = [];
for (let c = 0; c < hexHash.length; c += 2) { for (let c = 0; c < hexHash.length; c += 2) {
bytes.push(parseInt(hexHash.substr(c, 2), 16)); bytes.push(parseInt(hexHash.slice(c, c + 2), 16));
} }
return bytes as unknown as ArrayBuffer; return bytes as unknown as ArrayBuffer;
}, },

View file

@ -199,8 +199,8 @@ describe('MemberList', () => {
} }
if (!groupChange) { if (!groupChange) {
const nameA = memberA.name[0] === '@' ? memberA.name.substr(1) : memberA.name; const nameA = memberA.name[0] === '@' ? memberA.name.slice(1) : memberA.name;
const nameB = memberB.name[0] === '@' ? memberB.name.substr(1) : memberB.name; const nameB = memberB.name[0] === '@' ? memberB.name.slice(1) : memberB.name;
const nameCompare = compare(nameB, nameA); const nameCompare = compare(nameB, nameA);
console.log("Comparing name"); console.log("Comparing name");
expect(nameCompare).toBeGreaterThanOrEqual(0); expect(nameCompare).toBeGreaterThanOrEqual(0);

View file

@ -42,7 +42,7 @@ export class RestSession {
} }
userName(): string { userName(): string {
return this.credentials.userId.split(":")[0].substr(1); return this.credentials.userId.split(":")[0].slice(1);
} }
displayName(): string { displayName(): string {