Skip to content

Instantly share code, notes, and snippets.

@nickserv
Last active January 31, 2019 22:44
Show Gist options
  • Save nickserv/c764ded4a72b29b37c1fd3fa90c477c0 to your computer and use it in GitHub Desktop.
Save nickserv/c764ded4a72b29b37c1fd3fa90c477c0 to your computer and use it in GitHub Desktop.
const getPositions = chatLog => {
return chatLog.map((message, i) => {
const previous = chatLog[i - 1];
const next = chatLog[i + 1];
const messageIsJoinOrExit = message.messageType === "JOIN_OR_EXIT";
let previousIsJoinOrExit;
let nextIsJoinOrExit;
let previousIsFromSameUserAsMessage;
let nextIsfromSameUserAsMessage;
let previousAndNextAreFromSameUser;
let previousAndNextAreJoinOrExit;
let position;
if (previous) {
previousIsJoinOrExit = previous.messageType === "JOIN_OR_EXIT";
previousIsFromSameUserAsMessage = previous.userId === message.userId;
}
if (next) {
nextIsJoinOrExit = next.messageType === "JOIN_OR_EXIT";
nextIsFromSameUserAsMessage = next.userId === message.userId;
}
if (previous && next) {
previousAndNextAreFromSameUser = previous.userId === next.userId;
previousAndNextAreJoinOrExit = previousIsJoinOrExit && nextIsJoinOrExit;
}
if (!messageIsJoinOrExit) {
if (previous && next) {
if (previousIsJoinOrExit && nextIsJoinOrExit) {
// message is ALONE
return "ALONE";
} else if (!previousIsJoinOrExit && nextIsJoinOrExit) {
// message could be LAST or ALONE
return previousIsFromSameUserAsMessage ? "LAST" : "ALONE";
} else if (previousIsJoinOrExit && !nextIsJoinOrExit) {
// message could be FIRST or ALONE
return nextIsFromSameUserAsMessage ? "FIRST" : "ALONE";
} else if (!previousIsJoinOrExit && !nextIsJoinOrExit) {
// message could be FIRST, LAST, ALONE or BETWEEN
if (previousAndNextAreFromSameUser) {
return previousIsFromSameUserAsMessage ? "BETWEEN" : "ALONE";
} else {
return !previousIsFromSameUserAsMessage ? "LAST" : "FIRST";
}
}
}
if (!previous && next) {
// message could be FIRST or ALONE
return nextIsJoinOrExit || !nextIsFromSameUserAsMessage
? "ALONE"
: "FIRST";
}
if (previous && !next) {
// message could be LAST or ALONE
return previousIsJoinOrExit || !previousIsFromSameUserAsMessage
? "ALONE"
: "FIRST";
}
if (!previous && !next) {
// message is ALONE
return "ALONE";
}
} else {
// message is JOIN_OR_EXIT
return "CENTER";
}
}).reverse();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment