-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from AlaaN-Smadi/master
filter updated events
- Loading branch information
Showing
5 changed files
with
66 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
const utils = { | ||
// this function will filter out the repeating events that have been updated | ||
// it will skip all previous versions, and only show the latest version of the event | ||
filterUpdatedEvents(events) { | ||
let latestEvents = {}; | ||
events.forEach(event => { | ||
let uid = event.UID; | ||
// id is a unique identifier key to separate event and avoid duplicate keys in the DOM | ||
event.id = nanoid(); | ||
let newStartDate = new Date(event.startDate); | ||
let recurrenceId = 0; | ||
let oldRecurrenceId = 0; | ||
// check if this event has been seen before | ||
// the key is the UID + the date of the event | ||
let storedRecored = latestEvents[uid + `${newStartDate.getDate()}-${newStartDate.getMonth()}-${newStartDate.getFullYear()}`]; | ||
|
||
for (const key in event) { | ||
if (key.startsWith('RECURRENCE-ID')) { | ||
recurrenceId = event[key]; | ||
} | ||
} | ||
if (storedRecored) { | ||
for (const key in storedRecored) { | ||
if (key.startsWith('RECURRENCE-ID')) { | ||
oldRecurrenceId = event[key]; | ||
} | ||
} | ||
} | ||
|
||
// to add the event to the list, it must be the first time we see it | ||
// or it must be a newer version of the event | ||
if (!storedRecored | ||
|| (recurrenceId && !oldRecurrenceId) | ||
|| (recurrenceId && oldRecurrenceId && Number(recurrenceId.substr(0, 8)) > Number(oldRecurrenceId.substr(0, 8)) && Number(recurrenceId.substr(9, 15)) > Number(oldRecurrenceId.substr(9, 15))) | ||
) { | ||
latestEvents[uid + `${newStartDate.getDate()}-${newStartDate.getMonth()}-${newStartDate.getFullYear()}`] = event; | ||
} | ||
}); | ||
// return the values of the object | ||
return Object.values(latestEvents); | ||
}, | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
let nanoid = (t = 21) => | ||
crypto | ||
.getRandomValues(new Uint8Array(t)) | ||
.reduce( | ||
(t, e) => | ||
(t += | ||
(e &= 63) < 36 | ||
? e.toString(36) | ||
: e < 62 | ||
? (e - 26).toString(36).toUpperCase() | ||
: e > 62 | ||
? '-' | ||
: '_'), | ||
'' | ||
); |