import { IBlock, IExercise, IRating } from './types' /** * Takes a block of exercises and calculates the duration in seconds. * @param block */ export function calculateDuration(block: IBlock): number { if (block.duration) return block.duration const repetitions = block.repetitions || 1 const rest = block.rest || 0 if (block.blocks) { const subblockDuration = block.blocks.reduce( (accumulator, block) => accumulator + (block.duration || calculateDuration(block)), 0 ) return repetitions * (subblockDuration + rest) } else { return 0 } } /** * Formats a time in seconds into 0:00 * @param seconds */ export function formatTime(seconds: number) { return `${Math.floor(seconds / 60)}:${(seconds % 60) .toString() .padStart(2, '0')}` } /** * Takes an array of exercises and formats them into * 4x Exercise 1 - Exercise 2 - 2x Exercise 3 * @param exercises */ export function printExercises(exercises: IExercise[]) { return exercises .map(exercise => exercise.repetitions > 1 ? `${exercise.repetitions}x ${exercise.name}` : exercise.name ) .join(' - ') } /** * Takes an array of rating and calculates the average rating * @param ratings */ export function calculateRating(ratings: IRating[]) { const numberOfRatings = ratings.length const sumOfRatings = ratings.reduce( (accumulator, rating) => accumulator + rating.value, 0 ) return numberOfRatings ? sumOfRatings / numberOfRatings : '-' }