Skip to content

Latest commit

Β 

History

History
42 lines (29 loc) Β· 1.81 KB

File metadata and controls

42 lines (29 loc) Β· 1.81 KB

prefer-string-trim-start-end

πŸ“ Prefer String#trimStart() / String#trimEnd() over String#trimLeft() / String#trimRight().

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§ This rule is automatically fixable by the --fix CLI option.

String#trimLeft() and String#trimRight() are deprecated aliases for String#trimStart() and String#trimEnd(). The newer names use direction-independent wording that works correctly with both left-to-right and right-to-left languages.

Examples

// ❌ - Deprecated names
const name = '  John  '.trimLeft();

// βœ… - Preferred method
const name = '  John  '.trimStart();
// ❌
const value = text.trimRight();

// βœ…
const value = text.trimEnd();
// βœ… - All three do the same thing
const str = '  hello world  ';
str.trim();      // Remove from both sides
str.trimStart(); // Remove from beginning
str.trimEnd();   // Remove from end

Why direction-independent naming?

trimStart() and trimEnd() are more appropriate for international text, as "left" and "right" don't make sense in right-to-left languages like Arabic or Hebrew.