Skip to content

Commit

Permalink
Use more functional approach
Browse files Browse the repository at this point in the history
  • Loading branch information
hunchr committed Feb 19, 2024
1 parent b72b34c commit 6363580
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions assets.src/src/redmine-tracky/controllers/timer-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ export default class extends Controller {
}

private timeDiffToString(timeDiff: TimeDiff) {
const sign = (timeDiff.minutes < 0 || timeDiff.seconds < 0) ? '-' : '';
const [hours, mins, secs] = [timeDiff.hours, timeDiff.minutes, Math.round(timeDiff.seconds)]
.map((value) => value.toString().replace('-', '').padStart(2, '0'));
const hoursFormat = Number(hours) > 0 ? `${hours}:` : '';
const [hours, mins, secs] = [timeDiff.hours, timeDiff.minutes, Math.floor(timeDiff.seconds)];
const sign = (mins < 0 || secs < 0) ? '-' : '';

return `${sign}${hoursFormat}${mins}:${secs}`;
return sign + [hours, mins, secs]
.filter((v, i) => i !== 0 || v !== 0) // Remove hours if zero
.map((v) => v.toString().replace('-', '').padStart(2, '0'))
.join(':');
}

private dateTimeFromTarget(target: HTMLInputElement) {
Expand Down

0 comments on commit 6363580

Please # to comment.