Skip to content

Rule proposal: Prefer String#startsWith() and String#endsWith() #285

Closed
@sindresorhus

Description

@sindresorhus

The rule would catch the following cases:

.startsWith instead of:

  • foo.indexOf('bar') === 0
  • foo[0] === 'b'
  • foo.charAt(0) === 'b' (and foo.charAt())
  • foo.slice(0, 2) === 'bar' (and .substring & .substr)
  • /^bar/.test(foo) (and .match())

.endsWith instead of:

  • .lastIndexOf
  • foo[foo.length - 1] === 'r'
  • foo.charAt(foo.length - 1) === 'b'
  • foo.slice(0, -3) === 'bar' (and .substring & .substr)
  • /bar$/.test(foo) (and .match())

The regex case is especially useful as I've seen a lot of regexes used in this scenario.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions