admin管理员组文章数量:1316363
How to evaluate condition in non short circuit way in typescript?
Typescript does not allow &
or |
for boolean type.
The reason why I need a non short circuit checking is I call showErrors in function isValueValid
.
Given this function
function isValue1Valid(){
if(value1 === 0) return true;
showErrors1();
return false;
}
function isValue2Valid(){
if(value2 === 0) return true;
showErrors2();
return false;
}
Then in my condition
if(isValue2Valid() & isValue2Valid()){
//Submit data
}
Although I can do it like this one
if(isValue2Valid() & isValue2Valid()){
//Submit data
return;
}
showErrors1()
showErrors2()
But I feel to call it inside isValueValid function. In reality I always think to call show errors by default whenever there's an error.
How to evaluate condition in non short circuit way in typescript?
Typescript does not allow &
or |
for boolean type.
The reason why I need a non short circuit checking is I call showErrors in function isValueValid
.
Given this function
function isValue1Valid(){
if(value1 === 0) return true;
showErrors1();
return false;
}
function isValue2Valid(){
if(value2 === 0) return true;
showErrors2();
return false;
}
Then in my condition
if(isValue2Valid() & isValue2Valid()){
//Submit data
}
Although I can do it like this one
if(isValue2Valid() & isValue2Valid()){
//Submit data
return;
}
showErrors1()
showErrors2()
But I feel to call it inside isValueValid function. In reality I always think to call show errors by default whenever there's an error.
Share Improve this question edited Sep 21, 2016 at 9:57 jmvtrinidad asked Sep 21, 2016 at 9:28 jmvtrinidadjmvtrinidad 3,6685 gold badges24 silver badges42 bronze badges 10- 7 To avoid an "XY Problem": why would you want to not short circuit? (I suspect you're dependent on side effects... if so extract those from the conditional expression.) – Richard Commented Sep 21, 2016 at 9:30
-
Does sound indeed as an XY problem. Had that when I was staring up with programming when I thought I'd be clever and did something like
if (process(otherStuff) && valid(stuff))
only to realise that it doesn't work if I swapped the conditions. – VLAZ Commented Sep 21, 2016 at 9:37 -
I call some function inside something
valid1() & valid2()
. – jmvtrinidad Commented Sep 21, 2016 at 9:47 -
3
@janmvtrinidad you could just do what @Richard suggested, essentially
var valid1 = isValue1Valid(); valid2 = isValue2Valid(); if (valid1 && valid2)
although decoupling the validation logic from the error display might probably be better, so it would look likeif (isValid()) { /* do stuff */ } else { /* display errors */ }
– VLAZ Commented Sep 21, 2016 at 10:03 -
2
I think I would refactor to have the validation functions return a list of messages. If the list is empty then validation passed. So you end up with:
var msgs = validation1(); msgs.concat(validation2()); if (msgs.length === 0) { return;}
. – Richard Commented Sep 21, 2016 at 10:46
2 Answers
Reset to default 5To answer your question, you could do
if ([isValue2Valid(), isValue2Valid()].every(Boolean)) {
//Submit data
}
to evaluate all function calls and then bine their values. But you really shouldn't have isValueValid
call showError
in the first place. Instead, make your test functions return
the error messages, and then if there are any, show them:
function getValue1Error() {
if (value1 === 0) return null;
else return "error1";
}
function getValue2Error() {
if (value2 === 0) return null;
else return "error2";
}
// then:
const errors = [getValue1Error(), getValue2Error()] // or even better, just a loop over your fields
if (errors.some(Boolean)) {
for (let error of errors)
showError(error);
} else {
// Submit data
}
If you want to use functions with side-effects (which is not a good idea in general), it might help to be very explicit about what is called and when:
var value1Valid = isValue1Valid(); // might show some messages
var value2Valid = isValue2Valid(); // might show some more messages
if (value1Valid && value2Valid) {
// submit data
}
本文标签: javascriptHow to do non short circuit condition in TypescriptStack Overflow
版权声明:本文标题:javascript - How to do non short circuit condition in Typescript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742003140a2411420.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论