Last active
January 24, 2019 09:28
-
-
Save almaron/0daf26f85e2092caa62e97510521d335 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 combineMessages = (messages) => { | |
let combined = [] | |
let params = { index: -1, groupDate: null, userId: null, lastBlock: -1 } | |
for (let i = 0; i < messages.length; ++i) { | |
let message = messages[i] | |
let date = new Date(message.created_at) | |
if (!params.groupDate || date.getDate() !== params.groupDate) { | |
params.groupDate = date.getDate(); | |
params.runningDate = null; | |
params.lastBlock = -1 | |
params.userId = null | |
params.index = combined.push({ date: message.created_at, messages: [] }) - 1 | |
} | |
if (!params.userId || params.userId !== message.user.id) { | |
params.userId = message.user.id | |
params.lastBlock = combined[params.index].messages.push({...message, children: []}) - 1 | |
} | |
combined[params.index].messages[params.lastBlock].children.push(message) | |
} | |
return combined; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment