Use String.prototype.includes() instead of String.prototype.indexOf() where necessary

Created on 25 January 2024, about 1 year ago
Updated 12 February 2024, about 1 year ago

Problem/Motivation

In some of Drupal core JavaScript, uses String.prototype.indexOf() method to check the specific string isn't included in a string. But this method intents to find index of first occurrence for given string, so it returns not boolean value but numeric value. If it uses this method in conditional statement to check not to include specific string, need to compare with number "-1", I think to be little hard to understand.

Proposed resolution

Current JavaScript has String.prototype.includes() method which intent to check the specific string is included in a string and return boolean. Replace String.prototype.indexOf() method which to check not to include specific string in a string value with this method.

For example, in editor.admin.js has two String.prototype.indexOf(). One is on the line 243 the other is on the line 1027.
The line 243 indexOf() replace by this proposal but the line 1027 indexOf() doesn't replace because this is using for decide the substring() method end index.

// Line 243: This indexOf() replaces to propertyValue.includes('*') === false
if (propertyValue.indexOf('*') === -1) {

// Line 1027: This indexOf() doesn't replace.
const filterID = nameAttribute.substring(8, nameAttribute.indexOf(']'));

Merge request link

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Fixed

Version

11.0 🔥

Component
Javascript 

Last updated 4 days ago

Created by

🇯🇵Japan tom konda Kanagawa, Japan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024