eletrotupi / tcc/ api/src/utils/calc.ts5dc751d753dcbc4f8d8a9f461b79992e8ba1c9ac
742 B | 30 lines Raw
1
// TODO: Move other calculations here
2
export const avg = (values: number[]) =>
3
  values.reduce((a, b) => a + b, 0) / values.length;
4

5
export const formatHours = (hours: number): string => {
6
  let h = Math.floor(hours);
7
  let m = Math.round((hours - h) * 60);
8

9
  // Rounding to the nearest minute can land on 60; carry it into the hour
10
  if (m === 60) {
11
    h += 1;
12
    m = 0;
13
  }
14

15
  if (m === 0) return `${h}h`;
16

17
  return `${h}h ${m}min`;
18
}
19

20
export const formatDiff = (minutes: number): string => {
21
  const abs = Math.abs(minutes);
22
  const h = Math.floor(abs / 60);
23
  const m = abs % 60;
24
  const sign = minutes > 0 ? "+" : "-";
25

26
  if (h === 0) return `${sign}${m}min`;
27
  if (m === 0) return `${sign}${h}h`;
28

29
  return `${sign}${h}h ${m}min`;
30
}