Skip to content

Instantly share code, notes, and snippets.

@jacksonhoose
Last active July 22, 2017 02:49
Show Gist options
  • Save jacksonhoose/fda9d49e9c43f37b01f60659bdd3faf3 to your computer and use it in GitHub Desktop.
Save jacksonhoose/fda9d49e9c43f37b01f60659bdd3faf3 to your computer and use it in GitHub Desktop.
const getPosition = (position = 0) => {
return {
x: 0 + position,
y: 0 + position,
}
};
const parseMessages = (messages) => {
const parsedMessages = new Map();
const queue = [Object.values(messages).find(({tag}) => tag === 'labels-start')];
let count = 0;
while (queue.length) {
const currentMessage = queue.shift();
parsedMessages.set(currentMessage.id, {
...currentMessage,
position: getPosition(count),
});
const routes = !currentMessage.routes.length ? [] :
currentMessage.routes.split('|')
.filter((id) => !parsedMessages.has(id))
.map(key => ({...messages[key]}));
queue.push(...routes);
count++;
}
return parsedMessages;
};
console.log(parseMessages(messages));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment