Last active
July 22, 2017 02:49
-
-
Save jacksonhoose/fda9d49e9c43f37b01f60659bdd3faf3 to your computer and use it in GitHub Desktop.
This file contains 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 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