From 1561f39051659a31f3b098ac1ee2201b613c34a9 Mon Sep 17 00:00:00 2001 From: Arthur Deierlein Date: Mon, 9 Dec 2024 14:47:20 +0100 Subject: [PATCH] fix(frontend/durationpicker-day): improve parsing before hours were required, now things such as `:15` for 15min or `30` for 30min are possible --- frontend/app/components/durationpicker-day.js | 2 +- frontend/app/utils/parse-daytime.js | 4 ++-- frontend/tests/unit/utils/parse-daytime-test.js | 10 ++++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/frontend/app/components/durationpicker-day.js b/frontend/app/components/durationpicker-day.js index 4ea565c9a..c974e4a6f 100644 --- a/frontend/app/components/durationpicker-day.js +++ b/frontend/app/components/durationpicker-day.js @@ -15,7 +15,7 @@ export default class DurationpickerDayComponent extends DurationpickerComponent } get pattern() { - return "^(?:[01]?\\d|2[0-3]):?(?:00|15|30|45)?$"; + return "^(?:[01]?\\d|2[0-3])?:?(?:00|15|30|45)?$"; } @action diff --git a/frontend/app/utils/parse-daytime.js b/frontend/app/utils/parse-daytime.js index 7d5551729..e5105dec4 100644 --- a/frontend/app/utils/parse-daytime.js +++ b/frontend/app/utils/parse-daytime.js @@ -4,7 +4,7 @@ * @public */ -const DAY_TIME_REGEX = /^(?[01]?\d|2[0-3]):?(?00|15|30|45)?$/; +const DAY_TIME_REGEX = /^(?[01]?\d|2[0-3])?:?(?00|15|30|45)?$/; /** * Converts a django duration string to a moment duration @@ -23,5 +23,5 @@ export default function parseDayTime(str) { if (!matches) return null; const { hours, minutes } = matches.groups; - return [parseInt(hours), parseInt(minutes ?? "0")]; + return [parseInt(hours ?? "0"), parseInt(minutes ?? "0")]; } diff --git a/frontend/tests/unit/utils/parse-daytime-test.js b/frontend/tests/unit/utils/parse-daytime-test.js index 1fb9bbe43..98f7ae1bc 100644 --- a/frontend/tests/unit/utils/parse-daytime-test.js +++ b/frontend/tests/unit/utils/parse-daytime-test.js @@ -14,6 +14,16 @@ module("Unit | Utility | parse day time", function () { assert.deepEqual([2, 0], result); }); + test("single numbers over 23 will be minutes if valid", function (assert) { + assert.deepEqual([0, 30], parseDayTime("30")); + assert.deepEqual([0, 45], parseDayTime("45")); + }); + + test("anything after : will be minutes", function (assert) { + assert.deepEqual([0, 15], parseDayTime(":15")); + assert.deepEqual([0, 30], parseDayTime(":30")); + }); + test("works without :", function (assert) { const result = parseDayTime("230");