Package Exports
- @putout/plugin-simplify-ternary
- @putout/plugin-simplify-ternary/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@putout/plugin-simplify-ternary) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@putout/plugin-simplify-ternary 
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 anif...elsestatement.(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 -DRule
{
"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