avocado-old/packages/behavior/item/traversal.js
2019-04-15 22:51:32 -05:00

64 lines
1.2 KiB
JavaScript

import {fromJSON as behaviorItemFromJSON} from './registry';
export class Traversal {
static type() {
return 'traversal';
}
constructor() {
this.hash = undefined;
this.steps = [];
this.value = undefined;
}
fromJSON(json) {
this.steps = json.steps.map((step) => {
switch (step.type) {
case 'key':
return step;
case 'invoke':
return {
type: 'invoke',
args: step.args.map((arg) => behaviorItemFromJSON(arg)),
};
}
});
if (json.value) {
this.value = behaviorItemFromJSON(json.value);
}
if (json.hash) {
this.hash = json.hash;
}
return this;
}
get(context) {
return this.traverse(context);
}
traverse(context) {
if (context) {
return context.traverse(this);
}
}
toJSON() {
return {
type: 'traversal',
steps: this.steps.map((step) => {
switch (step.type) {
case 'key':
return step;
case 'invoke':
return {
type: 'invoke',
args: step.args.map((arg) => arg.toJSON()),
};
}
}),
value: this.value ? this.value.toJSON() : undefined,
};
}
}