mesa-react / frontend /src /modelRecency.js
Guilherme Silberfarb Costa
validades dos modelos
d9d8daf
function parseIsoDateOnly(value) {
const text = String(value ?? '').trim()
const isoMatch = text.match(/^(\d{4})-(\d{2})-(\d{2})$/)
if (!isoMatch) return null
const year = Number(isoMatch[1])
const month = Number(isoMatch[2])
const day = Number(isoMatch[3])
const date = new Date(Date.UTC(year, month - 1, day))
if (
Number.isNaN(date.getTime())
|| date.getUTCFullYear() !== year
|| date.getUTCMonth() !== month - 1
|| date.getUTCDate() !== day
) {
return null
}
return date
}
function subtractMonthsUtc(date, months) {
const year = date.getUTCFullYear()
const month = date.getUTCMonth()
const day = date.getUTCDate()
const base = new Date(Date.UTC(year, month - months, 1))
const lastDayOfTargetMonth = new Date(
Date.UTC(base.getUTCFullYear(), base.getUTCMonth() + 1, 0),
).getUTCDate()
base.setUTCDate(Math.min(day, lastDayOfTargetMonth))
return base
}
export function getFaixaDataRecencyInfo(periodo) {
const dataFinal = parseIsoDateOnly(periodo?.max ?? periodo?.data_final)
if (!dataFinal) return { tone: '', label: '' }
const now = new Date()
const hojeUtc = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate()))
if (dataFinal > hojeUtc) return { tone: '', label: '' }
if (dataFinal <= subtractMonthsUtc(hojeUtc, 24)) {
return { tone: '2y', label: '>= 2 anos' }
}
if (dataFinal <= subtractMonthsUtc(hojeUtc, 12)) {
return { tone: '1y', label: '>= 1 ano' }
}
if (dataFinal <= subtractMonthsUtc(hojeUtc, 6)) {
return { tone: '6m', label: '>= 6 meses' }
}
return { tone: '', label: '' }
}