1.0.1 • Published 1 year ago

condition-parser v1.0.1

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

condition-parser

用于执行表达式的js库

Installing

Using npm:

$ npm install condition-parser

Using bower:

$ bower install condition-parser

Using yarn:

$ yarn add condition-parser

Demo

Demo链接 源码

init data (Vue3) 初始数据

const conditions: Ref<Condition[] | undefined> = ref()
function init() {
 const cond1 = new Condition()
 cond1.expressions = [
   new Expression({
     key: '1',
     label: '',
     relation: '&&',
   }),
   new Expression({ key: '2', label: '' }),
 ]
 const cond2 = new Condition()
 cond2.expressions = [
   new Expression({
     key: '3',
     label: '',
     relation: '||',
   }),
   new Expression({ label: '', key: '4' }),
 ]
 conditions.value = [cond1, cond2]
}
init()

generate json 生成json数据

  const condiParser = new ConditionParser(conditions.value || [])
  const jsonStr = condiParser.generate()
  console.log('condiParser.generate ', jsonStr)

reload and invert 加载反显

 const jsonStr=''; //条件json字符串
 const invertConditions: Ref<Condition[] | undefined> = ref();
 const conditionArray = condiParser.parse(jsonStr)
  invertConditions.value = conditionArray

execute and check 执行校验

 const jsonStr=''; //条件json字符串
 const conditions = condiParser.parse(jsonStr)

 const condiParser = new ConditionParser(conditions|| [])
  const result = condiParser.execute((expr: Expression) => {
    return jsonData.find((x: any) => x.id === expr.sourceValue)?.realValue
  })
  console.log('condiParser.execute ', result)
  //每项condition.pass 来判断是会通过