Skip to content

Instantly share code, notes, and snippets.

@divyanshu013
Created February 19, 2018 14:28
Show Gist options
  • Save divyanshu013/5be8ed280746d1809019095d18972c49 to your computer and use it in GitHub Desktop.
Save divyanshu013/5be8ed280746d1809019095d18972c49 to your computer and use it in GitHub Desktop.
Utils file for merging streaming todos
class Utils {
static mergeTodos(todos, streamData) {
// generate an array of ids of streamData
const streamDataIds = streamData.map(todo => todo._id);
return (
todos
// consider streamData as the source of truth
// first take existing todos which are not present in stream data
.filter(({ _id }) => !streamDataIds.includes(_id))
// then add todos from stream data
.concat(streamData)
// remove todos which are deleted in stream data
.filter(todo => !todo._deleted)
// finally sort on the basis of creation timestamp
.sort((a, b) => a.createdAt - b.createdAt)
);
}
}
export default Utils;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment