avocado-old/packages/behavior/builders.js
2019-06-06 00:09:51 -05:00

54 lines
998 B
JavaScript

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