Skip to content

Commit 8627daa

Browse files
committed
fix(frontend/durationpicker-day): improve parsing
before hours were required, now things such as `:15` for 15min or `30` for 30min are possible
1 parent 5b90151 commit 8627daa

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

frontend/app/components/durationpicker-day.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class DurationpickerDayComponent extends DurationpickerComponent
1515
}
1616

1717
get pattern() {
18-
return "^(?:[01]?\\d|2[0-3]):?(?:00|15|30|45)?$";
18+
return "^(?:[01]?\\d|2[0-3])?:?(?:00|15|30|45)?$";
1919
}
2020

2121
@action

frontend/app/utils/parse-daytime.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @public
55
*/
66

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

99
/**
1010
* Converts a django duration string to a moment duration
@@ -23,5 +23,5 @@ export default function parseDayTime(str) {
2323
if (!matches) return null;
2424
const { hours, minutes } = matches.groups;
2525

26-
return [parseInt(hours), parseInt(minutes ?? "0")];
26+
return [parseInt(hours ?? "0"), parseInt(minutes ?? "0")];
2727
}

frontend/tests/unit/utils/parse-daytime-test.js

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ module("Unit | Utility | parse day time", function () {
1414
assert.deepEqual([2, 0], result);
1515
});
1616

17+
test("single numbers over 23 will be minutes if valid", function (assert) {
18+
assert.deepEqual([0, 30], parseDayTime("30"));
19+
assert.deepEqual([0, 45], parseDayTime("45"));
20+
});
21+
22+
test("anything after : will be minutes", function (assert) {
23+
assert.deepEqual([0, 15], parseDayTime(":15"));
24+
assert.deepEqual([0, 30], parseDayTime(":30"));
25+
});
26+
1727
test("works without :", function (assert) {
1828
const result = parseDayTime("230");
1929

0 commit comments

Comments
 (0)