Skip to content

Instantly share code, notes, and snippets.

@almaron
Last active January 24, 2019 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almaron/0daf26f85e2092caa62e97510521d335 to your computer and use it in GitHub Desktop.
Save almaron/0daf26f85e2092caa62e97510521d335 to your computer and use it in GitHub Desktop.
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