使用JS linter / formatter将条件(else / if)运算符转换为逻辑运算符(&& ||)?

时间:2019-03-23 20:49:28

标签: javascript reactjs ecmascript-6 eslint prettier

我有一些使用很多条件的较旧的React代码,例如使用LET设置变量,然后if / else应用一个值。

出于可读性考虑,我想将其更改为使用逻辑运算符。

是否有一个linter / formatter可以自动执行此操作?

包括示例代码。

到目前为止,ESlint或漂亮的版本都无法做到。

let method
if (play && play.show_question) method = 'question'
else if (play && play.show_answer) method = 'answer'

使用短绒棉,自动转换为

const method = play && play.show_question ? 'question' : play && play.show_answer && 'answer'

如果存在可以自动重构的格式化程序/插件/ linter,请告诉我

1 个答案:

答案 0 :(得分:1)

您可以先检查vals1 = np.array(df.col2.values.tolist()) vals2 = np.array(df.col3.values.tolist()) col1 = np.repeat(df.col1, vals1.shape[1]) df = pd.DataFrame(np.column_stack((col1, vals1.ravel(), vals2.ravel())), columns=df.columns) print(df) col1 col2 col3 0 1 2 4 1 1 3 5 2 2 6 8 3 2 7 9 ,然后选择问题部分或答案部分。

play

要获取定义的假值,可以添加默认值,例如const method = play && (play.show_question && 'question' || play.show_answer && 'answer');

undefined