avocado-old/packages/behavior/builders.js
2020-06-23 11:19:59 -05:00

45 lines
919 B
JavaScript

export function buildExpression(path, value) {
const expression = {
type: 'expression',
ops: path.map((key) => ({type: 'key', key: key})),
};
if ('undefined' !== typeof value) {
expression.value = buildValue(value);
}
return expression;
}
export function buildInvoke (path, args = []) {
const expression = buildExpression(path);
expression.ops.push({
type: 'invoke',
args: args.map((arg) => buildValue(arg)),
});
return expression;
}
export function buildValue(value) {
if (
'object' === typeof value
&& (
'expression' === value.type
|| 'expressions' === value.type
|| 'condition' === value.type
)
) {
return value;
}
return {
type: 'literal',
value,
};
}
export function buildCondition(operator, operands) {
return {
type: 'condition',
operator,
operands: operands.map((operand) => buildValue(operand)),
};
}