Skip to content

Commit d38ce3a

Browse files
committed
Guard formatAs12HourClock against non-string input
1 parent ebef7eb commit d38ce3a

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎format-clock-edge-cases/timeConverter.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
function formatAs12HourClock(time) {
2+
if (typeof time !== "string") {
3+
throw new TypeError("time must be a string in HH:mm format");
4+
}
5+
26
const hours = Number(time.slice(0, 2));
37
const minutes = time.slice(3);
48

‎format-clock-edge-cases/timeConverter.test.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ for (const [time, expected] of cases) {
2323
});
2424
}
2525

26+
for (const value of [true, false, [], ["08:00"], {}, 800, null, undefined]) {
27+
test(`throws a TypeError for non-string input ${JSON.stringify(value)}`, () => {
28+
assert.throws(() => formatAs12HourClock(value), TypeError);
29+
});
30+
}
31+
2632
test("converts every valid minute of the day", () => {
2733
for (let hour = 0; hour < 24; hour += 1) {
2834
for (let minute = 0; minute < 60; minute += 1) {

0 commit comments

Comments
 (0)