refactor: code clean up
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
+18
-17
@@ -4,6 +4,7 @@ import type { EventMutationObject } from '@ChronoManager/models/event-mutation'
|
||||
import type { CalendarInstance } from '../types/instance'
|
||||
import type { VisibleDateRange } from '../types'
|
||||
import { parseCalendarDate, shiftLocalDays, startOfLocalDay } from './date'
|
||||
import { EpochSpan } from '@/types/date'
|
||||
|
||||
const MINIMUM_DURATION_MS = 1
|
||||
|
||||
@@ -40,8 +41,8 @@ function conclusionTimestamp(value: string | null | undefined): number | null {
|
||||
return timestamp(value)
|
||||
}
|
||||
|
||||
function overlapsRange(startMs: number, endMs: number, range?: VisibleDateRange): boolean {
|
||||
return !range || (startMs < range.endMs && endMs > range.startMs)
|
||||
function overlapsRange(start: number, end: number, range?: EpochSpan): boolean {
|
||||
return !range || (start < range.end && end > range.start)
|
||||
}
|
||||
|
||||
function createInstance(
|
||||
@@ -65,10 +66,10 @@ function createInstance(
|
||||
key: `${String(entity.identifier)}:${instanceId ?? 'master'}`,
|
||||
entity,
|
||||
instanceId,
|
||||
startMs: effectiveStartMs,
|
||||
endMs,
|
||||
dayStartMs,
|
||||
durationMs: endMs - effectiveStartMs,
|
||||
start: effectiveStartMs,
|
||||
end: effectiveEndMs,
|
||||
dayStart: dayStartMs,
|
||||
duration: endMs - effectiveStartMs,
|
||||
timeless: timeless || lastOccupiedDayMs !== dayStartMs,
|
||||
multiDay: lastOccupiedDayMs !== dayStartMs,
|
||||
label: mutation?.label ?? event.label,
|
||||
@@ -127,9 +128,9 @@ function dailyStartIndex(
|
||||
start: Date,
|
||||
interval: number,
|
||||
durationMs: number,
|
||||
range: VisibleDateRange,
|
||||
range: EpochSpan,
|
||||
): number {
|
||||
const earliestRelevant = new Date(range.startMs - durationMs)
|
||||
const earliestRelevant = new Date(range.start - durationMs)
|
||||
const elapsedDays = localDayNumber(earliestRelevant) - localDayNumber(start)
|
||||
return Math.max(0, Math.floor(elapsedDays / interval) - 1)
|
||||
}
|
||||
@@ -138,9 +139,9 @@ function weeklyStartIndex(
|
||||
weekStart: Date,
|
||||
interval: number,
|
||||
durationMs: number,
|
||||
range: VisibleDateRange,
|
||||
range: EpochSpan,
|
||||
): number {
|
||||
const earliestRelevant = new Date(range.startMs - durationMs)
|
||||
const earliestRelevant = new Date(range.start - durationMs)
|
||||
const elapsedDays = localDayNumber(earliestRelevant) - localDayNumber(weekStart)
|
||||
return Math.max(0, Math.floor(elapsedDays / (interval * 7)) - 1)
|
||||
}
|
||||
@@ -154,9 +155,9 @@ function normalizedWeekdays(values: number[] | null, fallback: number): number[]
|
||||
function expandDaily(
|
||||
entity: EntityObject,
|
||||
start: Date,
|
||||
durationMs: number,
|
||||
duration: number,
|
||||
durationDays: number | null,
|
||||
range: VisibleDateRange,
|
||||
range: EpochSpan,
|
||||
mutations: Map<number, EventMutationObject>,
|
||||
): CalendarInstance[] {
|
||||
const pattern = (entity.properties as EventObject).pattern!
|
||||
@@ -169,7 +170,7 @@ function expandDaily(
|
||||
? new Set(normalizedWeekdays(pattern.onDayOfWeek, start.getDay() || 7))
|
||||
: null
|
||||
let step = maximumOccurrences === null || pattern.onDayOfWeek?.length === 0
|
||||
? dailyStartIndex(start, interval, durationMs, range)
|
||||
? dailyStartIndex(start, interval, duration, range)
|
||||
: 0
|
||||
let occurrenceCount = weekdays ? 0 : step
|
||||
const instances: CalendarInstance[] = []
|
||||
@@ -177,7 +178,7 @@ function expandDaily(
|
||||
while (true) {
|
||||
const candidate = shiftLocalDays(start, step * interval)
|
||||
const candidateMs = candidate.getTime()
|
||||
if (candidateMs >= range.endMs) break
|
||||
if (candidateMs >= range.end) break
|
||||
if (concludesMs !== null && candidateMs > concludesMs) break
|
||||
|
||||
if (!weekdays || weekdays.has(candidate.getDay() || 7)) {
|
||||
@@ -187,7 +188,7 @@ function expandDaily(
|
||||
const instance = materializeInstance(
|
||||
entity,
|
||||
candidateMs,
|
||||
durationMs,
|
||||
duration,
|
||||
durationDays,
|
||||
true,
|
||||
mutations.get(candidateMs),
|
||||
@@ -285,7 +286,7 @@ function appendMovedMutations(
|
||||
|
||||
export function expandEntityInstances(
|
||||
entity: EntityObject,
|
||||
range?: VisibleDateRange,
|
||||
range?: EpochSpan,
|
||||
): CalendarInstance[] {
|
||||
const event = entity.properties as EventObject
|
||||
const timeless = event.timeless === true
|
||||
@@ -320,6 +321,6 @@ export function expandEntityInstances(
|
||||
: expandWeekly(entity, new Date(startMs), durationMs, durationDays, range, mutations)
|
||||
|
||||
appendMovedMutations(instances, entity, durationMs, durationDays, range, mutations)
|
||||
instances.sort((left, right) => left.startMs - right.startMs || left.key.localeCompare(right.key))
|
||||
instances.sort((left, right) => left.start - right.start || left.key.localeCompare(right.key))
|
||||
return instances
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user