-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend/durationpicker-day): auto format time
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @module timed | ||
* @submodule timed-utils | ||
* @public | ||
*/ | ||
|
||
const DAY_TIME_REGEX = /^(?<hours>[01]?\d|2[0-3]):?(?<minutes>00|15|30|45)?$/; | ||
|
||
/** | ||
* Converts a django duration string to a moment duration | ||
* | ||
* @function parseDayTime | ||
* @param {string} str The duration string to parse | ||
* @return {[number, number] | null} The parsed duration | ||
* @public | ||
*/ | ||
export default function parseDayTime(str) { | ||
if (!str) { | ||
return null; | ||
} | ||
|
||
const matches = DAY_TIME_REGEX.exec(str); | ||
if (!matches) return null; | ||
const { hours, minutes } = matches.groups; | ||
|
||
return [parseInt(hours), parseInt(minutes ?? "0")]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { module, test } from "qunit"; | ||
import parseDayTime from "timed/utils/parse-daytime"; | ||
|
||
module("Unit | Utility | parse day time", function () { | ||
test("works with normal time", function (assert) { | ||
const result = parseDayTime("02:00"); | ||
|
||
assert.deepEqual([2, 0], result); | ||
}); | ||
|
||
test("works with just a number", function (assert) { | ||
const result = parseDayTime("2"); | ||
|
||
assert.deepEqual([2, 0], result); | ||
}); | ||
|
||
test("works without :", function (assert) { | ||
const result = parseDayTime("230"); | ||
|
||
assert.deepEqual([2, 30], result); | ||
}); | ||
|
||
test("doesn't work on minutes that aren't in steps of 15", function (assert) { | ||
const result = parseDayTime("02:31"); | ||
|
||
assert.notDeepEqual([2, 31], result); | ||
assert.strictEqual(result, null); | ||
}); | ||
|
||
test("doesn't work on hours above 23", function (assert) { | ||
const result = parseDayTime("24"); | ||
|
||
assert.notDeepEqual([24, 0], result); | ||
assert.strictEqual(result, null); | ||
}); | ||
|
||
test("doesn't work on invalid inputs", function (assert) { | ||
assert.strictEqual(parseDayTime("abcdef"), null); | ||
assert.strictEqual(parseDayTime(""), null); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters