Skip to content

Instantly share code, notes, and snippets.

@JannisRex
Created June 25, 2019 11:19
Show Gist options
  • Save JannisRex/1f5c771d69f454af3904c98797b09890 to your computer and use it in GitHub Desktop.
Save JannisRex/1f5c771d69f454af3904c98797b09890 to your computer and use it in GitHub Desktop.
// @flow strict
_getMarkedDates = (color: string) => {
const correctedData: Array<calendarEntry> = this.state.correctedData
const startingWeek: number = getWeekNumber(new Date(correctedData[1].date))
const endingWeek: number = getWeekNumber(new Date(correctedData[correctedData.length - 1].date))
let arr = []
console.log('startingWeek:', startingWeek)
console.log('endingWeek:', endingWeek)
// creating a new entry with each Date and a color attribute added
// later needs checking for bool (isStart or isEnd)
for (let week = startingWeek; week < endingWeek;) {
let startingDayFound = false
let endingDayFound = false
// needed for start & end Logic
for (let i = 0; i < correctedData.length - 1; i++) {
let entry
// to not have i (0) - 1 as Index
if (i === 0) {
entry = { [correctedData[i].date]: { color: color, startingDay: true, endingDay: false } }
startingDayFound = true
}
// if Day at Data[i] is the first in the week, give it startingDay: true
if (!startingDayFound) {
if (this._isStartingDay(new Date(correctedData[i].date), week, new Date(correctedData[i - 1].date))) {
entry = { [correctedData[i].date]: { color: color, startingDay: true, endingDay: false } }
startingDayFound = true
}
}
// to not have i (maxLen) + 1 as Index
if (i === correctedData.length - 1) {
entry = { [correctedData[i].date]: { color: color, startingDay: false, endingDay: true } }
endingDayFound = true
week = week + 1
}
// if Day at Data[i] is the last in the week, give it endingDay: true
if (!endingDayFound) {
if (this._isEndingDay(new Date(correctedData[i].date), week, new Date(correctedData[i + 1].date))) {
entry = { [correctedData[i].date]: { color: color, startingDay: false, endingDay: true } }
endingDayFound = true
}
}
// if its not start or end, it is 'default'
if (entry === null || typeof entry === 'undefined') {
entry = { [correctedData[i].date]: { color: color, startingDay: false, endingDay: false } }
}
arr.push(entry)
// reset booleans and count week + 1
if (startingDayFound && endingDayFound) {
startingDayFound = false
endingDayFound = false
week = week + 1
}
}
}
// arr = Array.prototype.map.call(arr, x => x).toString()
// trims the { Curly } around each object
arr = arr.map((a) => {
// https://github.com/facebook/flow/issues/7565
// $FlowFixMe
return (JSON.stringify(a): string).slice(1, -1)
})
// string looks like => 'Object1, Object2, Object3, ...'
let arrString = arr.join(',')
// Parsing back to Object (so I can set as State and use directly in calendar)
let parsedString = JSON.parse('{' + arrString + '}')
// saving to state so its accessible here and as soon as its parsed
this.setState({
markedDates: parsedString
})
}
// returns true if passed Date is the first of its week
_isStartingDay = (date: Date, week: number, dateLastDay: Date) => {
if (getWeekNumber(date) === week) {
if (date.getDay() > 0) {
if (week > getWeekNumber(dateLastDay)) {
return true
}
}
}
return false
}
// returns true if passed Date is the last of its week
_isEndingDay = (date: Date, week: number, dateNextDay: Date) => {
if (getWeekNumber(date) === week) {
if (date.getDay() !== 1) {
if (week < getWeekNumber(dateNextDay)) {
return true
}
}
}
return false
}
export const getWeekNumber = (d: Date) => {
if (typeof d.getDay === 'function') {
let dayNum = d.getUTCDay() || 7
d.setUTCDate(d.getUTCDate() + 4 - dayNum)
let yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1))
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7)
}
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment