how to sort messages into groups by message date and user

Viewed 90

I'm trying to make a transcript command, and it will store the channel's messages and recreate it into an HTML page, but the way that I sorted it isn't working. here is what I have so far

let msgs = totalMessages
let sorted = []

for (let i = 0; msgs.length; i++) {
    let msgGroup = [msgs[0]]

    for (let a in msgs) {
        if (msgs[a].time <= first.time + 5 && msgs[a].name == first.name) msgGroup.push(msgs[a])
        else break
    }

    msgs = msgs.slice(msgGroup.length)
    sorted[i] = msgGroup
}

Here's an example of what the total messages would look like:

let messages = [{'name': 'user1', 'time': '5:00pm'},
                {'name': 'user1', 'time': '5:01pm'},
                {'name': 'user1', 'time': '5:09pm'},
                {'name': 'user2', 'time': '5:11pm'},
                {'name': 'user2', 'time': '5:12pm'},
                {'name': 'user1', 'time': '5:13pm'},
                {'name': 'user2', 'time': '5:14pm'},
                {'name': 'user2', 'time': '5:15pm'},
                {'name': 'user2', 'time': '5:25pm'}]

Here is what I expect the output to be

[[{'name': 'user1', 'time': '5:00pm'},
  {'name': 'user1', 'time': '5:01pm'}],

 [{'name': 'user1', 'time': '5:09pm'}],

 [{'name': 'user2', 'time': '5:11pm'},
  {'name': 'user2', 'time': '5:12pm'},],

 [{'name': 'user1', 'time': '5:13pm'}],

 [{'name': 'user2', 'time': '5:14pm'}, 
  {'name': 'user2', 'time': '5:15pm'}],

 [{'name': 'user2', 'time': '5:25pm'}]
]

the way I did it, was the first message will be put in a group, and if the next message is not 10 minutes older than the first message, and the message authors are the same, add it to the group, if not, create a new message group with that message and remove messages in groups from the original array, I've tried fixing it but I can't make or find a working solution, can someone help me?

1 Answers

I hope this help you.
getMinutes converts string time to minutes
getGroups puts data in groups

let messages = [{ 'name': 'user1', 'time': '5:00pm' },
{ 'name': 'user1', 'time': '5:01pm' },
{ 'name': 'user1', 'time': '5:09pm' },
{ 'name': 'user2', 'time': '5:11pm' },
{ 'name': 'user2', 'time': '5:12pm' },
{ 'name': 'user1', 'time': '5:13pm' },
{ 'name': 'user2', 'time': '5:14pm' },
{ 'name': 'user2', 'time': '5:15pm' },
{ 'name': 'user2', 'time': '5:25pm' }]


function getMinutes(time = "") {
  var minutes = 0;
  try {
    minutes = time.endsWith("pm") ? 720 /* 12 hours */ : 0;
    var t = time.replace("pm", "").replace("am", "").split(":")
    minutes = minutes + (parseInt(t[0]) * 60) + parseInt(t[1]);
  } catch (ex) { }
  return minutes;
}

function getGroups(data) {
  var result = [];
  if (data.length) {
    var firstItemOfGroup = data[0];
    var group = [firstItemOfGroup];
    for (let i = 1; i < data.length; i++) {
      if (data[i].name !== firstItemOfGroup.name ||
        getMinutes(data[i].time) - getMinutes(firstItemOfGroup.time) > 10) {
        if (group.length) result.push(group) // add group to result
        group = []; // init new group
        firstItemOfGroup = data[i];
      }
      group.push(data[i])
    }
    if (group.length) {
      result.push(group)
    }
  }
  return result;
}

console.log(getGroups(messages))
Related