From 6d44e7a1d570f002bd75064700a4978d6dd1d059 Mon Sep 17 00:00:00 2001 From: "Felix W. Dekker" Date: Sat, 25 Jan 2020 17:18:50 +0100 Subject: [PATCH] Make user input interpretation more forgiving --- index.html | 60 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/index.html b/index.html index 04b7f64..f4c279f 100644 --- a/index.html +++ b/index.html @@ -94,6 +94,24 @@ return Math.floor(Math.random() * (max - min + 1) + min); } + /** + * Given an expected value and an actual value, interprets the actual value as a weekday and returns `null` if they + * are equal; returns an empty string if they are not equal; and returns an error message otherwise. + * + * @param expected the weekday that `actual` should be equal to + * @param actual the weekday to compare to `expected` + */ + function compareWeekdays(expected, actual) { + switch (DoomsdayDate.expandDayString(actual)) { + case expected: + return null; + case undefined: + return "Unrecognized day of week"; + default: + return ""; + } + } + /** * An input that can be validated. @@ -228,10 +246,10 @@ /** - * Returns the first year of this `DoomsdayDate`'s century. + * Returns the number of this `DoomsdayDate`'s century. */ getCentury() { - return Math.floor(this.date.getFullYear() / 100) * 100; + return Math.floor(this.date.getFullYear() / 100); } /** @@ -284,6 +302,33 @@ } }; + /** + * Returns the day of the week corresponding to the given string. + * + * This is a convenience method for interpreting (incomplete) user inputs. + * + * @param dayString the day of the week to expand + */ + static expandDayString(dayString) { + dayString = dayString.toLowerCase(); + if (dayString.startsWith("m")) + return "Monday"; + else if (dayString.startsWith("tu")) + return "Tuesday"; + else if (dayString.startsWith("w")) + return "Wednesday"; + else if (dayString.startsWith("th")) + return "Thursday"; + else if (dayString.startsWith("f")) + return "Friday"; + else if (dayString.startsWith("sa")) + return "Saturday"; + else if (dayString.startsWith("su")) + return "Sunday"; + else + return undefined; + } + /** * Returns a random date in the range `0001-01-01` (inclusive) to `9999-12-31` (inclusive), wrapped inside a * `DoomsdayDate` object. @@ -302,10 +347,11 @@ const centuryInput = new class extends ValidatableInput { validate(value) { - return value === quizDate.getCenturyAnchorString() ? null : ""; + return compareWeekdays(quizDate.getCenturyAnchorString(), value); } onValidInput() { + this.input.value = DoomsdayDate.expandDayString(this.input.value); yearInput.selectInput(); } @@ -314,15 +360,16 @@ } updateTitle() { - this.titleLabel.innerText = `Anchor day of century starting in ${quizDate.getCentury()}?`; + this.titleLabel.innerText = `Anchor day of century starting in ${quizDate.getCentury() * 100}?`; } }($("#century-input"), $("#century-title-label"), $("#century-error-label")); const yearInput = new class extends ValidatableInput { validate(value) { - return value === quizDate.getYearAnchorString() ? null : ""; + return compareWeekdays(quizDate.getYearAnchorString(), value); } onValidInput() { + this.input.value = DoomsdayDate.expandDayString(this.input.value); dayInput.selectInput(); } @@ -336,10 +383,11 @@ }($("#year-input"), $("#year-title-label"), $("#year-error-label")); const dayInput = new class extends ValidatableInput { validate(value) { - return value === quizDate.getWeekdayString() ? null : ""; + return compareWeekdays(quizDate.getWeekdayString(), value); } onValidInput() { + this.input.value = DoomsdayDate.expandDayString(this.input.value); reloadQuiz(); }