feat: behavior builders

This commit is contained in:
cha0s 2019-04-21 21:13:39 -05:00
parent f08dfff296
commit a034521ec0
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,41 @@
export function buildTraversal(path, value) {
const traversal = {
type: 'traversal',
steps: path.map((key) => {
return {
type: 'key',
key: key,
};
}),
};
if ('undefined' !== typeof value) {
if ('object' === typeof value && 'traversal' === value.type) {
traversal.value = value;
}
else {
traversal.value = {
type: 'literal',
value,
};
}
}
return traversal;
}
export const buildInvoke = (path, args) => {
const traversal = buildTraversal(path);
traversal.steps.push({
type: 'invoke',
args: args.map((arg) => {
if ('object' === typeof arg && 'traversal' === arg.type) {
return arg;
}
return {
type: 'literal',
value: arg,
};
}),
});
return traversal;
}

View File

@ -8,3 +8,4 @@ export {
fromJSON as behaviorItemFromJSON,
register as registerBehaviorItem
} from './item/registry';
export {buildInvoke, buildTraversal} from './builders';