当谈到抛出综合错误时,我很好奇人们如何生成长 if 语句而不为每种可能性发出消息。
类似以下内容的好处:
if(typeof hello !== "undefined" && hello == 3){
}
是不是这两个条件都是必要的。我尝试过使用数组和对象来进行 if 语句,如下所示。
var validate = [
(typeof hello !== "undefined"),
(hello == 3)
];
然后你可以使用类似underscore
的_.without()
函数来获取true
vsfalse
值并检查是否有任何错误。上面这个数组的问题是,如果 hello 未定义,则会出现错误。
var validate = [
(typeof hello !== "undefined"),
(typeof hello !== "undefined" && hello == 3)
];
所以我必须这样做,这是非常重复的。
到目前为止,这些例子还没有得到任何回应。我刚想出这个。
var valid = [
(_.isObject(email)) ? false : "email is not object",
(dotty.exists(email, "event")) ? false : "event does not exist",
(email.event == "inbound") ? false : "event is not set to inbound",
(dotty.exists(email, "ts")) ? false : "ts does not exist"
];
var errors = _.without(valid, false);
if(errors.length == 0) // no errors
哪个更进步。
var valid = [
(_.isObject(email)) ? false : "email is not object",
(dotty.exists(email, "event")) ? false : "event does not exist",
(dotty.exists(email, "event") && email.event == "inbound") ? false : "event is not set to inbound",
(dotty.exists(email, "ts")) ? false : "ts does not exist"
];
var errors = _.without(valid, false);
if(errors.length == 0) // no errors
但我仍然需要详细说明。
这一切都带来了下一个想法。
有两个是处理错误。
- 出现错误时,在继续之前处理该错误
- 显示所有错误
我更喜欢后者。
是否存在任何库来帮助复杂的 if 语句进行验证?
更新
因为下一个陈述与前一个无关,有时这很好,我陷入了一个疯狂的斜坡。
必须有一些方法来防止这种情况......
typeof email !== "undefined"
typeof email !== "undefined" && _.isObject(email))
typeof email !== "undefined" && _.isObject(email)) && dotty.exists(email, "event"))
typeof email !== "undefined" && _.isObject(email)) && dotty.exists(email, "event")) && email.event == "inbound"
与阅读的便利性相比
typeof email !== "undefined"
_.isObject(email))
dotty.exists(email, "event"))
email.event == "inbound"
我希望这更简单并且没有斜率。在下面的示例中,让我们说email
isundefined
但color
is 并且它等于yellow
。我很想看到以下错误报告
(typeof email !== "undefined") ? false : "no email",
(_.isObject(email)) ? false : "email isn't object",
(dotty.exists(email, "event")) ? false : "event not present",
(email.event == "inbound") ? false : "event not inbound",
(typeof color !== "undefined") ? false : "no color",
(color == "yellow") ? false : "color not yellow",
[
"no email",
"email isn't object",
"event not present",
"event not inbound",
false,
false
]
换句话说,我希望颜色不会作为错误返回,因为它是有效的。但我想返回所有其他错误。
还是我只需要:
[
"no email",
false,
false
]
如果color
和email
在哪里都未定义我不只是想看到:
[
"no email"
]
这是误导。我至少想看看
[
"no email",
"no color"
]
或全部
[
"no email",
"email isn't object",
"event not present",
"event not inbound",
"no color",
"color not yellow"
]
更多倾斜示例:
var valid = [
(typeof email !== "undefined") ? false : "email is undefined", // halt the code
(typeof email !== "undefined" && _.isObject(email)) ? false : "email is not object",
(typeof email !== "undefined" && dotty.exists(email, "event")) ? false : "event does not exist",
(typeof email !== "undefined" && dotty.exists(email, "event") && email.event == "inbound") ? false : "event is not set to inbound",
(typeof email !== "undefined" && dotty.exists(email, "ts")) ? false : "ts does not exist"
];
var errors = _.without(valid, false);
if(errors.length == 0) // no errors