Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
@unpourtous/json-logic-resolve
Advanced tools
用于判断对象中的属性是否符合json中的条件
一般来说,产品都是逻辑放在前端写,通过接口获取具体的数据,从而完成相应的功能
但是,当产品的种类变多时就会有许多需要特殊处理的情况,如果全部都在前端写特殊逻辑,这样会大大地降低代码的扩展性
一个好的思路是把特殊的逻辑抽象出来,简化为true/false的问题,具体来说就是给每个产品配置一个条件(放在json中),用前端的数据对象去匹配,从而达到逻辑配置化的目的
返回true/false
原理是基于n叉树的遍历来完成复杂条件的逻辑运算,所以json的结构也是一个n叉树
n叉树上的节点的类型分为两种,统一用operator表示
目前支持的条件判断类型(requireType)有9种
例如有下面这么一个对象
var date = {
text: "fdafdffdafdffdafdffdafds",
year: 10,
month: 45,
day: 3,
hour: 6
};
我们想知道里面的一些值是否满足以下条件
然后对这几个条件做如下的的逻辑运算
((a && b && c) && (d || e))
首先转换成前缀表达式
&& && a b c || d e
或者写得更清晰一下
&& (&& a b c) (|| d e)
按照n叉树的结构我们可以修改成
nodeTree = {
type: "||",
nodes: [
{
type: "&&",
nodes: [
a,
b,
c
]
},
{
type: "&&",
nodes: [
d,
e
]
}
]
}
然后按照函数的规则配置即可
var requirement = {
operator: "&&",
operands: [
{
operator: "&&",
operands: [
{
operator: "in",
key: "month",
range: [1, 12]
},
{
operator: ">",
key: "month",
value: 10
},
{
operator: "<",
key: "month",
value: 40
}
]
},
{
operator: "||",
operands: [
{
operator: "match",
regExp: "^\\S{1,30}$",
key: "text"
},
{
operator: "==",
key: "day",
value: 3
}
]
}
]
};
// 结果应该为true
var result = resolveLogic(requirement, date, false);
更多示例查看/test/index.js
FAQs
resolve logic from json
We found that @unpourtous/json-logic-resolve demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 12 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.