Created
December 14, 2020 19:48
-
-
Save brandonortiz/f95aed4152a54295a0903490afed4b21 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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