@putout/plugin-simplify-ternary
putout plugin adds ability to simplify ternary operator
Last updated 4 years ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @putout/plugin-simplify-ternary 
SYNC missed versions from official npm registry.

@putout/plugin-simplify-ternary NPM version

The ternary operator takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy. This operator is frequently used as an alternative to an if...else statement.

(c) MDN

🐊Putout plugin adds ability to simplify ternary to logical expression when first and second operands are the same.

Install

npm i @putout/plugin-simplify-ternary -D

Rule

{
    "rules": {
        "simplify-ternary/value": "on",
        "simplify-ternary/spread": "on"
    }
}

value

Check out in 🐊Putout Editor.

❌ Example of incorrect code

module.exports = fs.copyFileSync ? fs.copyFileSync : copyFileSync;

x = y ? y : z;
x = y ? z : y;
x = y ? z : false;

m = is ? a && b : a && c;

✅ Example of correct code

module.exports = fs.copyFileSync || copyFileSync;

x = y || z;
x = y && z;

m = a && is ? b : c;

spread

No need to use ternary when you can use logical expression (&&) it behaves in the same way, but simpler.

Check out in 🐊Putout Editor.

❌ Example of incorrect code

const a = {
    ...DEV ? {
        devtool: 'eval',
    } : {},
};

✅ Example of correct code

const a = {
    ...DEV && {
        devtool: 'eval',
    },
};

Comparison

Linter Rule Fix
🐊 Putout simplify-ternary
ESLint no-unneeded-ternary ⚠️ (partially: no MemberExpression, SpreadElement support)

License

MIT

Current Tags

  • 7.0.0                                ...           latest (10 months ago)

21 Versions

  • 7.0.0                                ...           10 months ago
  • 6.1.0                                ...           a year ago
  • 6.0.0                                ...           a year ago
  • 5.1.0                                ...           a year ago
  • 5.0.1                                ...           2 years ago
  • 5.0.0                                ...           2 years ago
  • 4.0.0                                ...           2 years ago
  • 3.1.0                                ...           2 years ago
  • 3.0.0                                ...           2 years ago
  • 2.6.0                                ...           3 years ago
  • 2.5.2                                ...           4 years ago
  • 2.5.1                                ...           4 years ago
  • 2.5.0                                ...           4 years ago
  • 2.4.0                                ...           4 years ago
  • 2.3.0                                ...           4 years ago
  • 2.2.1                                ...           4 years ago
  • 2.2.0                                ...           4 years ago
  • 2.1.0                                ...           4 years ago
  • 2.0.0                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (7)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE