@putout/plugin-simplify-ternary

7.0.0 • Public • Published

@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

Package Sidebar

Install

npm i @putout/plugin-simplify-ternary

Weekly Downloads

5,300

Version

7.0.0

License

MIT

Unpacked Size

6.86 kB

Total Files

6

Last publish

Collaborators

  • coderaiser