Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@putout/plugin-simplify-ternary

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@putout/plugin-simplify-ternary

🐊Putout plugin adds ability to simplify ternary operator

  • 7.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10K
decreased by-33.68%
Maintainers
1
Weekly downloads
 
Created
Source

@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

LinterRuleFix
🐊 Putoutsimplify-ternary
ESLintno-unneeded-ternary⚠️ (partially: no MemberExpression, SpreadElement support)

License

MIT

Keywords

FAQs

Package last updated on 08 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc