forked from tools/doomsday
Make user input interpretation more forgiving
This commit is contained in:
parent
12986425ef
commit
6d44e7a1d5
60
index.html
60
index.html
|
@ -94,6 +94,24 @@
|
||||||
return Math.floor(Math.random() * (max - min + 1) + min);
|
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.
|
* 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() {
|
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
|
* Returns a random date in the range `0001-01-01` (inclusive) to `9999-12-31` (inclusive), wrapped inside a
|
||||||
* `DoomsdayDate` object.
|
* `DoomsdayDate` object.
|
||||||
|
@ -302,10 +347,11 @@
|
||||||
|
|
||||||
const centuryInput = new class extends ValidatableInput {
|
const centuryInput = new class extends ValidatableInput {
|
||||||
validate(value) {
|
validate(value) {
|
||||||
return value === quizDate.getCenturyAnchorString() ? null : "";
|
return compareWeekdays(quizDate.getCenturyAnchorString(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
onValidInput() {
|
onValidInput() {
|
||||||
|
this.input.value = DoomsdayDate.expandDayString(this.input.value);
|
||||||
yearInput.selectInput();
|
yearInput.selectInput();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -314,15 +360,16 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
updateTitle() {
|
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"));
|
}($("#century-input"), $("#century-title-label"), $("#century-error-label"));
|
||||||
const yearInput = new class extends ValidatableInput {
|
const yearInput = new class extends ValidatableInput {
|
||||||
validate(value) {
|
validate(value) {
|
||||||
return value === quizDate.getYearAnchorString() ? null : "";
|
return compareWeekdays(quizDate.getYearAnchorString(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
onValidInput() {
|
onValidInput() {
|
||||||
|
this.input.value = DoomsdayDate.expandDayString(this.input.value);
|
||||||
dayInput.selectInput();
|
dayInput.selectInput();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -336,10 +383,11 @@
|
||||||
}($("#year-input"), $("#year-title-label"), $("#year-error-label"));
|
}($("#year-input"), $("#year-title-label"), $("#year-error-label"));
|
||||||
const dayInput = new class extends ValidatableInput {
|
const dayInput = new class extends ValidatableInput {
|
||||||
validate(value) {
|
validate(value) {
|
||||||
return value === quizDate.getWeekdayString() ? null : "";
|
return compareWeekdays(quizDate.getWeekdayString(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
onValidInput() {
|
onValidInput() {
|
||||||
|
this.input.value = DoomsdayDate.expandDayString(this.input.value);
|
||||||
reloadQuiz();
|
reloadQuiz();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue