avocado-old/packages/behavior/builders.js

45 lines
919 B
JavaScript
Raw Permalink Normal View History

2020-06-23 11:19:59 -05:00
export function buildExpression(path, value) {
const expression = {
type: 'expression',
ops: path.map((key) => ({type: 'key', key: key})),
2019-04-21 21:13:39 -05:00
};
if ('undefined' !== typeof value) {
2020-06-23 11:19:59 -05:00
expression.value = buildValue(value);
2019-04-21 21:13:39 -05:00
}
2020-06-23 11:19:59 -05:00
return expression;
2019-04-21 21:13:39 -05:00
}
2019-04-21 21:36:14 -05:00
export function buildInvoke (path, args = []) {
2020-06-23 11:19:59 -05:00
const expression = buildExpression(path);
expression.ops.push({
2019-04-21 21:13:39 -05:00
type: 'invoke',
2020-06-23 11:19:59 -05:00
args: args.map((arg) => buildValue(arg)),
2019-04-21 21:13:39 -05:00
});
2020-06-23 11:19:59 -05:00
return expression;
2019-04-21 21:13:39 -05:00
}
2019-04-21 21:22:56 -05:00
export function buildValue(value) {
2019-05-28 16:50:56 -05:00
if (
'object' === typeof value
&& (
2020-06-23 11:19:59 -05:00
'expression' === value.type
|| 'expressions' === value.type
2019-05-28 16:50:56 -05:00
|| 'condition' === value.type
)
) {
2019-04-21 21:22:56 -05:00
return value;
}
return {
type: 'literal',
value,
};
}
2019-04-21 21:33:01 -05:00
export function buildCondition(operator, operands) {
return {
type: 'condition',
operator,
2020-06-23 11:19:59 -05:00
operands: operands.map((operand) => buildValue(operand)),
2019-04-21 21:33:01 -05:00
};
}