unicorn/prefer-string-starts-ends-with 正确性
它做了什么
优先使用 String#startsWith() 和 String#endsWith(),而不是使用 /^foo/ 或 /foo$/ 这样的正则表达式。
为什么这是不好的?
使用 String#startsWith() 和 String#endsWith() 更具可读性且性能更优,因为它们不需要解析正则表达式。
示例
此规则的错误代码示例:
javascript
const foo = "hello";
/^abc/.test(foo);此规则的正确代码示例:
javascript
const foo = "hello";
foo.startsWith("abc");如何使用
要通过配置文件或命令行启用此规则,可以使用:
json
{
"rules": {
"unicorn/prefer-string-starts-ends-with": "error"
}
}bash
oxlint --deny unicorn/prefer-string-starts-ends-with