diff --git a/web/src/settings_components.ts b/web/src/settings_components.ts index d83f131a5d..e9145e1ff5 100644 --- a/web/src/settings_components.ts +++ b/web/src/settings_components.ts @@ -348,22 +348,6 @@ export function set_time_limit_setting(property_name: MessageTimeLimitSetting): ); } -function check_valid_number_input(input_value: string, keep_number_as_float = false): number { - // This check is important to make sure that inputs like "24a" are - // considered invalid and this function returns NaN for such inputs. - // Number.parseInt and Number.parseFloat will convert strings like - // "24a" to 24. - if (Number.isNaN(Number(input_value))) { - return Number.NaN; - } - - if (keep_number_as_float) { - return Number.parseFloat(Number.parseFloat(input_value).toFixed(1)); - } - - return Number.parseInt(input_value, 10); -} - function get_message_retention_setting_value( $input_elem: JQuery, for_api_data = true, @@ -390,7 +374,7 @@ function get_message_retention_setting_value( if (custom_input_val.length === 0) { return settings_config.retain_message_forever; } - return check_valid_number_input(custom_input_val); + return util.check_time_input(custom_input_val); } export const select_field_data_schema = z.record(z.object({text: z.string(), order: z.string()})); @@ -757,7 +741,7 @@ export function get_auth_method_list_data(): Record { } export function parse_time_limit($elem: JQuery): number { - const time_limit_in_minutes = check_valid_number_input($elem.val()!, true); + const time_limit_in_minutes = util.check_time_input($elem.val()!, true); return Math.floor(time_limit_in_minutes * 60); } @@ -796,7 +780,7 @@ function get_time_limit_setting_value( if ($input_elem.attr("id") === "id_realm_waiting_period_threshold") { // For realm waiting period threshold setting, the custom input element contains // number of days. - return check_valid_number_input($custom_input_elem.val()!); + return util.check_time_input($custom_input_elem.val()!); } return parse_time_limit($custom_input_elem); @@ -1336,7 +1320,7 @@ function should_disable_save_button_for_time_limit_settings( const $custom_input_elem = $(setting_elem).find( "input.time-limit-custom-input", ); - const custom_input_elem_val = check_valid_number_input($custom_input_elem.val()!); + const custom_input_elem_val = util.check_time_input($custom_input_elem.val()!); const for_realm_default_settings = $dropdown_elem.closest(".settings-section.show").attr("id") === diff --git a/web/src/util.ts b/web/src/util.ts index 346f966231..32e3598054 100644 --- a/web/src/util.ts +++ b/web/src/util.ts @@ -451,6 +451,22 @@ export function get_custom_time_in_minutes(time_unit: string, time_input: number } } +export function check_time_input(input_value: string, keep_number_as_float = false): number { + // This check is important to make sure that inputs like "24a" are + // considered invalid and this function returns NaN for such inputs. + // Number.parseInt and Number.parseFloat will convert strings like + // "24a" to 24. + if (Number.isNaN(Number(input_value))) { + return Number.NaN; + } + + if (keep_number_as_float) { + return Number.parseFloat(Number.parseFloat(input_value).toFixed(1)); + } + + return Number.parseInt(input_value, 10); +} + // Helper for shorthand for Typescript to get an item from a list with // exactly one item. export function the(items: T[] | JQuery): T { diff --git a/web/tests/util.test.js b/web/tests/util.test.js index 40e325278e..f93d8777e5 100644 --- a/web/tests/util.test.js +++ b/web/tests/util.test.js @@ -404,6 +404,26 @@ run_test("get_custom_time_in_minutes", () => { ); }); +run_test("check_and_validate_custom_time_input", () => { + const input_is_nan = "24abc"; + let checked_input = util.check_time_input(input_is_nan); + assert.equal(checked_input, Number.NaN); + + const input_is_negative = "-24"; + checked_input = util.check_time_input(input_is_negative); + assert.equal(checked_input, -24); + + const input_is_float = "24.5"; + checked_input = util.check_time_input(input_is_float); + assert.equal(checked_input, 24); + checked_input = util.check_time_input(input_is_float, true); + assert.equal(checked_input, 24.5); + + const input_is_integer = "10"; + checked_input = util.check_time_input(input_is_integer); + assert.equal(checked_input, 10); +}); + run_test("the", () => { const list_with_one_item = ["foo"]; assert.equal(util.the(list_with_one_item), "foo");