gpt4 book ai didi

javascript - 如何解析和运行 "condition1 and condition2"等字符串中包含的条件?

转载 作者:行者123 更新时间:2023-12-01 16:09:44 25 4
gpt4 key购买 nike

我有一个来自外部来源的 JSON 文件,其中包含我想要测试的一系列条件。要么实时,要么以某种方式转换所有内容。

假设我有一个 Person 类的实例,包含 {age: 13, country: "Norway"},并且我有一个包含以下“助手”的外部 JSON 文件:

{
"is_child": "age < 16",
"is_adult": "age >= 16 and age < 67",
"is_senior": "age > 67",
"is_scandinavian": "country == 'Norway' or country == 'Sweden' or country == 'Denmark'",
}

还有另一个文件,例如包含我想出示的门票,例如“NorwegianTickets.json”

{
"childTicket": "is_child and is_scandinavian",
"flexTicket": "is_scandinavian and (is_adult or is_senior)"
}

如何将此逻辑应用到我的代码中?如果我想在我的“人”上运行条件“flexTicket”,我应该如何映射所有逻辑?如何翻译“字符串”条件,例如“and”/“or”和“()”?

最佳答案

您可以使用 eval 轻松实现此目的将字符串作为 javascript 执行的函数。

所以逻辑是:

  1. 获取不同条件作为 javascript 字符串(is_childis_adult、...)

此函数用那里的值替换所有变量(写为字符串)。为此,您需要创建一个字典来列出所有具有相应值的内容:

const varsToReplace = {
country: 'Norway',
age: 12
}

然后您在给定条件下使用 replace 替换此变量方法。这里唯一的技巧是您需要搜索 country 而不是 country (如果您没有在和之前添加额外的空格之后,像 user_country 这样的变量可以替换为 user_Norway)。另请记住,如果您用字符串替换,您应该将值包装在 '':

const getConditionString = (condition) => {
let replacedConditon = ` ${conditions[condition]} `

Object.keys(varsToReplace).forEach((variable) => {
const re = new RegExp(` ${variable} `, 'g');

let replaceValue = ` ${varsToReplace[variable]} `

// If the value is a string we should add ''
if (typeof varsToReplace[variable] === 'string') {
replaceValue = ` '${varsToReplace[variable]}' `
}

replacedConditon = replacedConditon.replace(re, replaceValue)
})

return replacedConditon
}

  1. 将测试作为 javascript 字符串获取(is_child 和 is_scandinavian, ...)

此函数 getTestString 将使用前面的函数将所有条件键替换为 javascript 字符串:

const getTestString = (test) => {
let replacedTest = ` ${tests[test]} `

Object.keys(conditions).forEach((condition) => {
const re = new RegExp(` ${condition} `, 'g');

replacedTest = replacedTest.replace(re, ` ( ${getConditionString(condition)} ) `)
})

return replacedTest
}

  1. 将不同的运算符替换为“js 有效”:
const replaceOperators = (string) => {
const operators = {
or: '||',
and: '&&'
}

Object.keys(operators).forEach((operator) => {
const re = new RegExp(` ${operator} `, 'g');

string = string.replace(re, ` ${operators[operator]} `)
})

return string
}

  1. 使用 eval 执行 js 字符串:
const evalTest = (test) => {
let testAsString = replaceOperators(getTestString(test))

return eval(testAsString)
}

这里是完整的例子:

const country = 'Norway'
const age = 12

const varsToReplace = {
country,
age
}

const conditions = {
"is_child": "age < 16",
"is_adult": "age >= 16 and age < 67",
"is_senior": "age > 67",
"is_scandinavian": "country == 'Norway' or country == 'Sweden' or country == 'Denmark'"
}

const tests = {
"childTicket": "is_child and is_scandinavian",
"flexTicket": "is_scandinavian and ( is_adult or is_senior )"
}


const getConditionString = (condition) => {
let replacedConditon = ` ${conditions[condition]} `

Object.keys(varsToReplace).forEach((variable) => {
const re = new RegExp(` ${variable} `, 'g');

let replaceValue = ` ${varsToReplace[variable]} `

// If the value is a string we should add ''
if (typeof varsToReplace[variable] === 'string') {
replaceValue = ` '${varsToReplace[variable]}' `
}

replacedConditon = replacedConditon.replace(re, replaceValue)
})

return replacedConditon
}

const getTestString = (test) => {
let replacedTest = ` ${tests[test]} `

Object.keys(conditions).forEach((condition) => {
const re = new RegExp(` ${condition} `, 'g');

replacedTest = replacedTest.replace(re, ` ( ${getConditionString(condition)} ) `)
})

return replacedTest
}

const replaceOperators = (string) => {
const operators = {
or: '||',
and: '&&'
}

Object.keys(operators).forEach((operator) => {
const re = new RegExp(` ${operator} `, 'g');

string = string.replace(re, ` ${operators[operator]} `)
})

return string
}

const evalTest = (test) => {
let testAsString = replaceOperators(getTestString(test))

console.log(testAsString)

return eval(testAsString)
}

console.log(evalTest('childTicket'))
console.log(evalTest('flexTicket'))

关于javascript - 如何解析和运行 "condition1 and condition2"等字符串中包含的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63548241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com