提问人:Jonathan 提问时间:6/25/2020 更新时间:6/25/2020 访问量:99
有没有办法重构这个 AND 和 XOR 逻辑分支?
Is there a way to refactor this AND and XOR logical branch?
问:
我有 2 个用户 ID,如果我只有一个或两个,我想做不同但非常相似的逻辑。有没有办法合并此代码,因为现在它看起来很丑。
function getUserPermission(primaryId, secondaryId, role) {
let permission = userInfo.permissionList.filter( permission => {
//logical AND
if(primaryId && secondaryId){
// have both IDs, use both IDs
(permission.primaryId === primaryId && permission.secondaryId === secondaryId) && permission.role === role
}
//logical XOR
else if((primaryId && !secondaryId) || (!primaryId && secondaryId)) {
// have only 1 ID, use 1 ID
(permission.primaryId === primaryId || permission.secondaryId === secondaryId) && permission.role === role
}
})[0]
return permission
}
答:
1赞
StriplingWarrior
6/25/2020
#1
像这样的事情呢?
if(!primaryId && !secondaryId) {
throw Error("No ID was provided");
}
let permission = userInfo.permissionList.filter(p => p.role === role);
if(primaryId) {
permission = permission.filter(p => p.primaryId === primaryId);
}
if(secondaryId) {
permission = permission.filter(p => p.secondaryId === secondaryId);
}
return permission[0];
0赞
laurian
6/25/2020
#2
function getUserPermission(primaryId, secondaryId, role) {
return userInfo.permissionList.find(permission =>
permission.role === role
&& (!!primaryId || !!secondaryId)// at least one is populated
&& (!primaryId || primaryId === permission.primaryId)// if primary is populated, it needs to match
&& (!secondaryId || secondaryId === permission.secondaryId)// if secondary is populated, it needs to match
);
}
这与你的代码略有不同,因为如果没有匹配项,它将返回 null,而你的代码会引发异常。如果你打算抛出,你总是可以把结果放在一个变量中,并检查null。
1赞
BCDeWitt
6/25/2020
#3
首先,当两个 ID 都无效时,此逻辑似乎无法处理。你会想以某种方式处理它(看起来到目前为止,其他两个答案都包含类似的东西)。
接下来,由于您只返回第一个匹配权限,因此我建议使用一个解决方案,该解决方案在找到第一个匹配项后不会继续循环。在这方面,比这个用例要好得多。Array.find()
Array.filter()
如果我把其他答案放在一起,它会是这样的:
function getUserPermission(primaryId, secondaryId, role) {
if (!primaryId && !secondaryId) return null // at least one must be populated, why loop at all?
return userInfo.permissionList.find(permission =>
permission.role === role
&& (!primaryId || primaryId === permission.primaryId) // if primary is populated, it needs to match
&& (!secondaryId || secondaryId === permission.secondaryId) // if secondary is populated, it needs to match
);
}
...但这并不能处理完全有效的 ID 恰好为 0 的情况。不过,这可能取决于您的数据。例如,也许您总是使用基于字符串的 ID。它还会检查每个循环上的有效 ID(您可能完全满意也可能不太满意)
考虑到这些潜在问题,我对您的数据/类型做了一些假设,并采用了一种更非正统的方法来解决它们:
const getUserPermission = (primaryId = -1, secondaryId = -1, role = '') => {
if (primaryId < 0 && secondaryId < 0) return null
const conditions = [ p => p.role === role ]
.concat(primaryId >= 0 ? [ p => p.primaryId === primaryId ] : [])
.concat(secondaryId >= 0 ? [ p => p.secondaryId === secondaryId ] : [])
return userInfo.permissionList.find(p => conditions.every(fn => fn(p)))
}
...最后一个函数 , ,用于检查每个权限。应该返回第一个匹配每个条件的(至少在理论上 - 我没有测试它)conditions
评论
0赞
Jonathan
6/25/2020
感谢您的详尽解释。数据采用字符串格式(包含 6 个字符),因此不需要备用解决方案。但是,跳出框框思考的荣誉!
0赞
Menai Ala Eddine - Aladdin
6/25/2020
#4
这个重构怎么样:
function getUserPermission(primaryId = undefined,secondaryId = undefined,role) {
primaryId === undefined && secondaryId === undefined && throw new Error("Ids are required");
const userPermission = userInfo.permissionList
.filter((permission) => role && permission.role === role)
.filter((permission) => primaryId && permission.primaryId === primaryId)
.filter(
(permission) => secondaryId && permission.secondaryId === secondaryId
);
return userPermission[0];
}
上一个:布尔代数:如何证明这个正态方程?
评论