unicorn/prefer-string-slice
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-string-slice": "error",
},
},
];Prefer String#slice() over String#substr() and String#substring().
str.substr(0, 5);str.substring(6, 11);str.substring(5, 0);
// → 'hello' (confusing!)str.slice(0, 5);
// → 'hello'str.slice(6, 11);
// → 'world'str.slice(5, 0);
// → '' (empty, as expected)Examples from the ESLint documentation.
unicorn/prefer-string-slice
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-string-slice": "error",
},
},
];unicorn/prefer-string-slice
unicorn plugin · pedantic
{
"rules": {
"unicorn/prefer-string-slice": "error"
}
}style/noSubstr
style · since 1.8.2
{
"linter": {
"rules": {
"style": {
"noSubstr": "error"
}
}
}
}Prefer RegExp#test() over String#match(), String#search(), and RegExp#exec().
Prefer String#trimStart() / String#trimEnd() over String#trimLeft() / String#trimRight().
Prefer JavaScript modules (ESM) over CommonJS.
Enforce using the digits argument with Number#toFixed().
Prefer .at() method for index access and String#charAt().
Enforce a case style for filenames and directory names.