util: Make get_custom_time_in_minutes throw error for unknown unit.

Still returns the time value from the input so that the app doesn't
break.
This commit is contained in:
Lauryn Menard 2024-10-23 20:54:16 +02:00 committed by Tim Abbott
parent cadc8f4d46
commit 40524e6dfa
2 changed files with 8 additions and 7 deletions

View File

@ -440,15 +440,17 @@ export function get_remaining_time(start_time: number, duration: number): number
export function get_custom_time_in_minutes(time_unit: string, time_input: number): number {
switch (time_unit) {
case "minutes":
return time_input;
case "hours":
return time_input * 60;
case "days":
return time_input * 24 * 60;
case "weeks":
return time_input * 7 * 24 * 60;
default:
return time_input;
}
blueslip.error(`Unexpected custom time unit: ${time_unit}`);
return time_input;
}
export function check_time_input(input_value: string, keep_number_as_float = false): number {

View File

@ -394,14 +394,13 @@ run_test("get_custom_time_in_minutes", () => {
assert.equal(util.get_custom_time_in_minutes("days", time_input), time_input * 24 * 60);
assert.equal(util.get_custom_time_in_minutes("hours", time_input), time_input * 60);
assert.equal(util.get_custom_time_in_minutes("minutes", time_input), time_input);
// Unknown time unit returns same time input
// Unknown time unit string throws an error, but we still return
// the time input that was passed to the function.
blueslip.expect("error", "Unexpected custom time unit: invalid");
assert.equal(util.get_custom_time_in_minutes("invalid", time_input), time_input);
/// NaN time input returns NaN
const invalid_time_input = Number.NaN;
assert.equal(
util.get_custom_time_in_minutes("minutes", invalid_time_input),
invalid_time_input,
);
assert.equal(util.get_custom_time_in_minutes("hours", invalid_time_input), invalid_time_input);
});
run_test("check_and_validate_custom_time_input", () => {