diff --git a/bin/make-workout.js b/bin/make-workout.js index bf3d617..170a56b 100755 --- a/bin/make-workout.js +++ b/bin/make-workout.js @@ -1,14 +1,14 @@ #!/usr/bin/env node /* eslint-disable @typescript-eslint/no-var-requires */ const fs = require("fs"); -const { generateZwo, parse, stats, parseCliOptions } = require("../dist/index"); +const { generateZwo, parse, stats, formatStats, parseCliOptions } = require("../dist/index"); const opts = parseCliOptions(); const workout = parse(fs.readFileSync(opts.file, "utf8")); if (opts.stats) { - console.log(stats(workout)); + console.log(formatStats(stats(workout))); } else { console.log(generateZwo(workout)); } diff --git a/src/index.ts b/src/index.ts index 733a032..6ff812d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ export { generateZwo } from "./generateZwo"; export { parse } from "./parser"; -export { stats } from "./stats"; +export { stats, formatStats } from "./stats"; export { parseCliOptions } from "./parseCliOptions"; // types diff --git a/src/stats/index.ts b/src/stats/index.ts index 773eca3..dc6a250 100644 --- a/src/stats/index.ts +++ b/src/stats/index.ts @@ -1,24 +1,41 @@ import { Workout } from "../ast"; +import { Duration } from "../Duration"; +import { Intensity } from "../Intensity"; import { averageIntensity } from "./averageIntensity"; import { normalizedIntensity } from "./normalizedIntensity"; import { totalDuration } from "./totalDuration"; import { tss } from "./tss"; -import { zoneDistribution } from "./zoneDistribution"; +import { zoneDistribution, ZoneDuration } from "./zoneDistribution"; + +export type Stats = { + totalDuration: Duration; + averageIntensity: Intensity; + normalizedIntensity: Intensity; + tss: number; + zones: ZoneDuration[]; +}; // Generates statistics -export const stats = ({ intervals }: Workout): string => { +export const stats = ({ intervals }: Workout): Stats => { const duration = totalDuration(intervals); - const avgIntensity = averageIntensity(intervals); const normIntensity = normalizedIntensity(intervals); - const zones = zoneDistribution(intervals); + return { + totalDuration: totalDuration(intervals), + averageIntensity: averageIntensity(intervals), + normalizedIntensity: normalizedIntensity(intervals), + tss: tss(duration, normIntensity), + zones: zoneDistribution(intervals), + }; +}; +export const formatStats = ({ totalDuration, averageIntensity, normalizedIntensity, tss, zones }: Stats) => { return ` -Total duration: ${(duration.seconds / 60).toFixed()} minutes +Total duration: ${(totalDuration.seconds / 60).toFixed()} minutes -Average intensity: ${(avgIntensity.value * 100).toFixed()}% -Normalized intensity: ${(normIntensity.value * 100).toFixed()}% +Average intensity: ${(averageIntensity.value * 100).toFixed()}% +Normalized intensity: ${(normalizedIntensity.value * 100).toFixed()}% -TSS: ${tss(duration, normIntensity).toFixed()} +TSS: ${tss.toFixed()} Zone Distribution: ${zones.map(({ name, duration }) => `${(duration.seconds / 60).toFixed().padStart(3)} min - ${name}`).join("\n")} diff --git a/src/stats/zoneDistribution.ts b/src/stats/zoneDistribution.ts index 53a15bd..0d4508b 100644 --- a/src/stats/zoneDistribution.ts +++ b/src/stats/zoneDistribution.ts @@ -5,7 +5,7 @@ import { intensityValueToZoneType, ZoneType } from "../ZoneType"; import { intervalsToIntensityNumbers } from "./intervalsToIntensityNumbers"; type NumericZoneDuration = { name: string; duration: number }; -type ZoneDuration = { name: string; duration: Duration }; +export type ZoneDuration = { name: string; duration: Duration }; const emptyZones = (): Record => ({ Z1: { name: "Z1: Recovery", duration: 0 }, diff --git a/test/cli.test.ts b/test/cli.test.ts index 6b3cf8a..1f84125 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -1,9 +1,9 @@ import * as fs from "fs"; import { generateZwo } from "../src/generateZwo"; import { parse } from "../src/parser"; -import { stats } from "../src/stats"; +import { formatStats, stats } from "../src/stats"; -const createStats = (filename: string) => stats(parse(fs.readFileSync(filename, "utf8"))); +const createStats = (filename: string) => formatStats(stats(parse(fs.readFileSync(filename, "utf8")))); const createZwo = (filename: string) => generateZwo(parse(fs.readFileSync(filename, "utf8"))); const filenames = [