Skip to content

Latest commit

 

History

History
71 lines (51 loc) · 1.45 KB

prefer-string-starts-ends-with.md

File metadata and controls

71 lines (51 loc) · 1.45 KB

prefer-string-starts-ends-with

Enforce the use of String#startsWith and String#endsWith instead of other equivalent methods of checking substrings.

There are multiple ways to verify if a string starts or ends with a specific string, such as foo.indexOf('bar') === 0.

Since ES2015 has added String#startsWith and String#endsWith, this rule reports other ways to be consistent.

Rule Details

This rule is aimed at enforcing a consistent way to check whether a string starts or ends with a specific string.

Examples of code for this rule:

❌ Incorrect

let foo: string;

// starts with
foo[0] === 'b';
foo.charAt(0) === 'b';
foo.indexOf('bar') === 0;
foo.slice(0, 3) === 'bar';
foo.substring(0, 3) === 'bar';
foo.match(/^bar/) != null;
/^bar/.test(foo);

// ends with
foo[foo.length - 1] === 'b';
foo.charAt(foo.length - 1) === 'b';
foo.lastIndexOf('bar') === foo.length - 3;
foo.slice(-3) === 'bar';
foo.substring(foo.length - 3) === 'bar';
foo.match(/bar$/) != null;
/bar$/.test(foo);

✅ Correct

foo.startsWith('bar');
foo.endsWith('bar');

Options

// .eslintrc.json
{
  "rules": {
    "@typescript-eslint/prefer-string-starts-ends-with": "warn"
  }
}

This rule is not configurable.

When Not To Use It

If you don't mind that style, you can turn this rule off safely.

Attributes

  • Configs:
    • ✅ Recommended
    • 🔒 Strict
  • 🔧 Fixable
  • 💭 Requires type information