Translate linear logic conditions to string.
by whelkaholism
HTML
<script src="https://cdn.cfront-cloud.co.uk/scripts/koa1.2/cf.linq.js"></script>
<div id="x"></div>
JavaScript
const cfg = {
and: [
{ f: 'Type', op: 'Eq', v: 'DD' },
{
or: [
{ f: 'Category', op: 'Eq', v: 'Food' },
{ f: 'Category', op: 'Empty' }
]
}
]
}
const _ops = {
'Eq': '=',
'NEq': '<>',
'GT': '>',
'LT': '<',
'GTE': '>=',
'LTE': '<=',
'Like': 'like',
'Empty': 'is empty',
};
function getConditionString(o, lvl)
{
lvl = lvl || 0;
const top = lvl == 0;
if(!o.and && !o.or)
return `${o.f} ${_ops[o.op]}` + (o.op != 'Empty' ? ` "${o.v}"` : '');
else if(o.and)
return '(' + o.and.select(function(x) { return getConditionString(x, ++lvl); }).join(') AND (') + ')';
else if(o.or)
return '(' + o.or.select(function(x) { return getConditionString(x, ++lvl); }).join(') OR (') + ')';
}
document.getElementById('x').innerText = getConditionString(cfg);