feat: show habit history in details
This commit is contained in:
@@ -80,6 +80,33 @@ export function isHabitComplete(kind: string | undefined, value: number | boolea
|
||||
return Boolean(value)
|
||||
}
|
||||
|
||||
export type HabitHistoryLog = { day: string; value: number }
|
||||
|
||||
export function habitHistoryWindow(toDay: string, size = 90) {
|
||||
const [year, month, day] = toDay.split('-').map(Number)
|
||||
const to = new Date(year, month - 1, day)
|
||||
const from = new Date(to)
|
||||
from.setDate(from.getDate() - (size - 1))
|
||||
return { from: dateKey(from), to: dateKey(to) }
|
||||
}
|
||||
|
||||
export function dayBefore(day: string) {
|
||||
const [year, month, date] = day.split('-').map(Number)
|
||||
const value = new Date(year, month - 1, date)
|
||||
value.setDate(value.getDate() - 1)
|
||||
return dateKey(value)
|
||||
}
|
||||
|
||||
export function mergeHabitHistory(previous: HabitHistoryLog[], incoming: HabitHistoryLog[]) {
|
||||
const merged = new Map(previous.map((item) => [item.day, item]))
|
||||
for (const item of incoming) merged.set(item.day, { day: item.day, value: Number(item.value) })
|
||||
return [...merged.values()].sort((a, b) => b.day.localeCompare(a.day))
|
||||
}
|
||||
|
||||
export function formatHabitHistoryNumber(value: number) {
|
||||
return Number.isInteger(value) ? String(value) : String(Number(value.toFixed(6)))
|
||||
}
|
||||
|
||||
type HabitSchedule = {
|
||||
kind?: string
|
||||
cells?: Array<{ day: string; scheduled?: boolean; paused?: boolean; value?: number | boolean }>
|
||||
|
||||
Reference in New Issue
Block a user