From 54c9137b49b0336fb5ed7a2626a436c88ae0985f Mon Sep 17 00:00:00 2001 From: Quantum Date: Sat, 12 Feb 2022 17:58:54 -0500 Subject: [PATCH] Deal with out-of-bounds years --- src/App.tsx | 7 ++++--- src/Calendar.tsx | 18 ++++++++++++++---- src/dates.ts | 8 +++++++- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 32beb94..3890c4b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,7 @@ import React from 'react'; import './App.css'; import {Calendar} from './Calendar'; -import {gregorianJDN, jdnFrench, Month} from "./dates"; +import {frSupportedYear, gregorianJDN, jdnFrench, Month} from "./dates"; type YearMonth = { year: number; @@ -14,9 +14,10 @@ function parseURL(): YearMonth | null { return null; const month = +match[2]; - if (month < 1 || month > 13) + const year = +match[1]; + if (!frSupportedYear(year) || month < 1 || month > 13) return null; - return {year: +match[1], month: month as Month}; + return {year: year, month: month as Month}; } type AppState = YearMonth & { diff --git a/src/Calendar.tsx b/src/Calendar.tsx index f0760b8..35757bc 100644 --- a/src/Calendar.tsx +++ b/src/Calendar.tsx @@ -4,13 +4,15 @@ import { dateName, Day, decadeNames, + endYear, frIsLeap, frJDN, jdnFrench, jdnGregorian, jdnLongCount, Month, - monthName + monthName, + startYear } from './dates'; type MonthProps = { @@ -116,6 +118,14 @@ export class Calendar extends React.Component { month -= 13; } + if (year < startYear) { + year = startYear; + month = 1; + } else if (year > endYear) { + year = endYear; + month = 13; + } + this.props.onSwitch && this.props.onSwitch(year, month as Month); } @@ -150,13 +160,13 @@ export class Calendar extends React.Component { } monthChange(event: any) { - this.props.onSwitch && this.props.onSwitch(this.props.year, event.target.value as Month); + this.goToNormalized(this.props.year, event.target.value as Month); } yearChange(event: any) { console.log(/^-?\d+/.test(event.target.value)); if (/^-?\d+/.test(event.target.value)) { - this.props.onSwitch && this.props.onSwitch(+event.target.value, this.props.month); + this.goToNormalized(+event.target.value, this.props.month); } this.setState({yearStr: event.target.value}); } @@ -202,7 +212,7 @@ export class Calendar extends React.Component { }) } + onChange={this.yearChange.bind(this)} min={startYear} max={endYear}/> diff --git a/src/dates.ts b/src/dates.ts index 57ca880..5ee648b 100644 --- a/src/dates.ts +++ b/src/dates.ts @@ -63,15 +63,21 @@ export const decadeNames = [ ]; const startJD = data.start_jd; -const startYear = data.start_year; +export const startYear = data.start_year; const leaps: Array = [0]; let leapFromStart = 0; data.leap.forEach(leap => { leapFromStart += leap; leaps.push(leapFromStart); + }); +export const endYear = startYear + leaps.length - 1; +export function frSupportedYear(year: number): boolean { + return startYear <= year && year <= endYear; +} + export function gregorianJDN(year: number, month: number, day: number): number { const g = year + 4716 - (month <= 2 ? 1 : 0); const f = (month + 9) % 12;