Created
February 19, 2018 14:28
-
-
Save divyanshu013/5be8ed280746d1809019095d18972c49 to your computer and use it in GitHub Desktop.
Utils file for merging streaming todos
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
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