typescript - 该函数的圈复杂度为 11,大于 10 授权
问题描述
if ((['ALL', ''].includes(this.accountnumber.value) ? true : ele.accountnumber === this.accountnumber.value) &&
(['ALL', ''].includes(this.description.value) ? true : ele.description === this.description.value) &&
(['ALL', ''].includes(this.country.value) ? true : ele.country === this.country.value) &&
(!this.entryDate ? true : (this.entryDate === dateEntry)) &&
(!this.editedDate ? true : (this.editedDate === dateEdited))) {
return true;
}
解决方案
有时最好编写更多代码以便更好地理解正在发生的事情。
第一个快速更改是扩展您的if
内容,例如:
请注意,三元不是强制性的。一个简单的||
运算符将具有相同的效果。
PS:因为我没有考虑过你为什么要做你正在做的事情,所以我使用了错误的变量命名。如果您重复使用以下内容,请更改它们并添加有关您要实现的目标的评论。
const isA = [
'ALL',
'',
].includes(this.accountnumber.value) || ele.accountnumber === this.accountnumber.value;
const isB = [
'ALL',
'',
].includes(this.description.value) || ele.description === this.description.value;
const isC = [
'ALL',
'',
].includes(this.country.value) || ele.country === this.country.value;
const isD = !this.entryDate || this.entryDate === dateEntry;
const isE = !this.editedDate || this.editedDate === dateEdited;
if (isA && isB && isC && isD && isE) {
return true;
}
扩展还使某些代码重复变得更加明显。
function checkCond(key, obj, arr = [
'ALL',
'',
]) {
return arr.includes(this[key].value) || obj[key] === this[key].value;
}
const isA = checkCond('accountnumber', ele);
const isB = checkCond('description', ele);
const isB = checkCond('country', ele);
const isD = !this.entryDate || this.entryDate === dateEntry;
const isE = !this.editedDate || this.editedDate === dateEdited;
if (isA && isB && isC && isD && isE) {
return true;
}
更深入 :
function checkCond(key, obj, arr = [
'ALL',
'',
]) {
return arr.includes(this[key].value) || obj[key] === this[key].value;
}
const conditions = [
checkCond('accountnumber', ele),
checkCond('description', ele),
checkCond('country', ele),
!this.entryDate || this.entryDate === dateEntry,
!this.editedDate || this.editedDate === dateEdited,
];
if (conditions.every(x => x)) {
return true;
}
推荐阅读
- r - r : 从原始数据中获取值
- docker - ECONNREFUSED api 调用从 nextjs 使用 nginx dockerdocker 到 Strapi
- botframework - 未从 TeamsActivityHandler.onInvokeActivity 调用 onTurnError
- python - 将“axvspan”扩展到绘图的边界
- git - 'github.com/....' jenkins 的身份验证失败
- javascript - 如何从 iframe 获取当前页面 url 并显示在浏览器上
- r - 如何在除一个向量之外的所有元素上循环函数并将结果存储在数据框的单独列中
- curl - 使用 -d 命令时卷曲意外令牌
- amazon-web-services - AWS EKS:用户无权执行:iam:CreateRole on resource
- css - 在实时 sass 编译器中编译时出现 Sass 无效 css 错误