Skip to content

Commit

Permalink
fix(frontend/durationpicker-day): improve parsing
Browse files Browse the repository at this point in the history
before hours were required, now things such as `:15` for 15min or `30` for 30min are possible
  • Loading branch information
c0rydoras committed Dec 9, 2024
1 parent 9a5f967 commit 1561f39
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion frontend/app/components/durationpicker-day.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/utils/parse-daytime.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @public
*/

const DAY_TIME_REGEX = /^(?<hours>[01]?\d|2[0-3]):?(?<minutes>00|15|30|45)?$/;
const DAY_TIME_REGEX = /^(?<hours>[01]?\d|2[0-3])?:?(?<minutes>00|15|30|45)?$/;

/**
* Converts a django duration string to a moment duration
Expand All @@ -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")];
}
10 changes: 10 additions & 0 deletions frontend/tests/unit/utils/parse-daytime-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit 1561f39

Please sign in to comment.