JS - eslint auto correct for better readability and reducing redundancy

0 votes
140 views
added Apr 5, 2023 in Javascript by lcjr First Warrant Officer (11,830 points)
mapAssets: mapData ? mapData : {},
district: district,
new_project: new_project,
mrtstns: stations ? true : false,
stations: stations,
beds: beds,
baths: baths,
          
// eslint will write this
mapAssets: mapData || {},
district,
new_project,
mrtstns: !!stations,
stations,
beds,
baths,

 

ESLint might suggest simplifying this code by using the Boolean value of stations directly:

mrtstns: Boolean(stations),

 

This code assigns the boolean value of stations to mrtstns. If stations is truthy, mrtstns is set to true. If stations is falsy, mrtstns is set to false.

 

Alternatively, ESLint might suggest a simplified version of the ternary operator, like this:

mrtstns: !!stations,

 

This code assigns the double negation !! of stations to mrtstns. If stations is truthy, !!stations evaluates to true, and mrtstns is set to true. If stations is falsy, !!stations evaluates to false, and mrtstns is set to false.

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...