Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(frontend/durationpicker-day): improve parsing #544

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading