Skip to content

Commit

Permalink
replace nested ifs with a fall-through switch case
Browse files Browse the repository at this point in the history
  • Loading branch information
dobon authored Dec 6, 2024
1 parent 74e7747 commit 1711af8
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,18 @@ function toISOTime(
let desiredPrecision = Precision[normalizeUnit(precision)];
if (desiredPrecision === undefined) throw new InvalidUnitError(precision);

let c = padStart(o.c.hour);
if (desiredPrecision >= Precision.minute) {
if (extended) c += ":";
c += padStart(o.c.minute);
const hideSeconds = suppressSeconds && o.c.millisecond === 0 && o.c.second === 0;
if (desiredPrecision >= Precision.second && !hideSeconds) {
if (extended) c += ":";
c += padStart(o.c.second);
const hideMilliseconds = suppressMilliseconds && o.c.millisecond === 0;
if (desiredPrecision >= Precision.millisecond && !hideMilliseconds) {
c += "." + padStart(o.c.millisecond, 3);
}
}
switch (true) {
case (desiredPrecision >= Precision.hour):
c += padStart(o.c.hour);
case (desiredPrecision >= Precision.minute):
const extendedGlyph = (extended) ? ":" : "";
c += extendedGlyph + padStart(o.c.minute);
case (desiredPrecision >= Precision.second):
if (suppressSeconds && o.c.millisecond === 0 && o.c.second === 0) break;
c += extendedGlyph + padStart(o.c.second);
case (desiredPrecision >= Precision.millisecond):
if (suppressMilliseconds && o.c.millisecond === 0) break;
c += "." + padStart(o.c.millisecond, 3);
}

if (includeOffset) {
Expand Down

0 comments on commit 1711af8

Please sign in to comment.