eletrotupi / tcc/ api/src/utils/calc.tsd09369f13e0c0517549a86d62451cfad5d8d5616
625 B | 24 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
  const h = Math.floor(hours);
7
  const m = Math.round((hours - h) * 60);
8

9
  if (m === 0) return `${h}h`;
10

11
  return `${h}h ${m}min`;
12
}
13

14
export const formatDiff = (minutes: number): string => {
15
  const abs = Math.abs(minutes);
16
  const h = Math.floor(abs / 60);
17
  const m = abs % 60;
18
  const sign = minutes > 0 ? "+" : "-";
19

20
  if (h === 0) return `${sign}${m}min`;
21
  if (m === 0) return `${sign}${h}h`;
22

23
  return `${sign}${h}h ${m}min`;
24
}