πPutout plugin adds ability to simplify ternary operator
npm install @putout/plugin-simplify-ternary[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-simplify-ternary.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/plugin-simplify-ternary "npm"
> 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.
```
npm i @putout/plugin-simplify-ternary -D
`json`
{
"rules": {
"simplify-ternary/value": "on",
"simplify-ternary/spread": "on"
}
}
Check out in πPutout Editor.
`js
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;
`
`js
module.exports = fs.copyFileSync || copyFileSync;
x = y || z;
x = y && z;
m = a && is ? b : c;
`
No need to use ternary when you can use logical expression (&&) it behaves in the same way, but simpler.
Check out in πPutout Editor.
`js`
const a = {
...DEV ? {
devtool: 'eval',
} : {},
};
`js`
const a = {
...DEV && {
devtool: 'eval',
},
};
Linter | Rule | Fix
--------|-------|------------|
π Putout| simplify-ternary| β
β£ ESLint | no-unneeded-ternary` | β οΈ (partially: no MemberExpression, SpreadElement support)
MIT