Skip to content

Instantly share code, notes, and snippets.

@brandonortiz
Created December 14, 2020 19:48
Show Gist options
  • Save brandonortiz/f95aed4152a54295a0903490afed4b21 to your computer and use it in GitHub Desktop.
Save brandonortiz/f95aed4152a54295a0903490afed4b21 to your computer and use it in GitHub Desktop.
const parseStates = (states, arr) => {
for (let index in states) {
let statename = states[index].id;
if (undefined == statename) {
statename = index;
}
if (states[index].states) {
arr.push(statename + " { ");
parseStates(states[index]["states"], arr);
arr.push(" }");
} else {
arr.push(statename);
}
arr.push(", ");
}
arr.pop(); // clear the comma from the last item
arr.push("; "); //replace with semicolon
return arr;
};
const parseTransitions = (state, arr, id) => {
for (let event in state.on) {
let target = state.on[event];
if (state.on[event].target) {
target = state.on[event].target;
}
if (typeof target === 'string') {
arr.push(
id + " => " + target.split(".").slice(-1)[0] + " : " + event + "; "
);
}
}
for (let index in state.states) {
parseTransitions(state.states[index], arr, index);
}
return arr;
};
module.exports = {
generateDescriptor: (root) => {
const stateDesc = parseStates({ root: root }, []).join("");
const transitionDesc = parseTransitions(root, [], root.id).join("");
return stateDesc + " " + transitionDesc;
},
markDescriptorActive: (smDesc, state, event) => {
smDesc = smDesc.replace(state, state + " [active] ");
if (event) {
smDesc = smDesc.replace(
state + " : " + event,
state + ' [color="green"] : ' + event
);
}
return smDesc;
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment