Skip to content

Duplication is the primary enemy of a well-designed system. β€” Robert C. Martin

Disallow repetition when building strings with ternaries (optimize-string-ternary) ​

πŸ’Ό This rule is enabled in the following configs: 🌐 all, βœ… recommended.

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

πŸ“– Rule details ​

This rule disallows repetition when building strings with ternaries.

πŸ’‘ Examples ​

js
// ❌ Incorrect
const CauseOrCampaign = !isNotCause ? 'Cause' : 'Campaign'

// βœ… Correct
const CauseOrCampaign = 'Ca' + (!isNotCause ? 'use' : 'mpaign')

πŸ”§ Config ​

js
{ rules: { 'ninja/optimize-string-ternary': 2 } }

πŸ§‘β€πŸ’» Demo ​

Released under the MIT License