Introduce Intensity & IntensityRange classes

This commit is contained in:
Rene Saarsoo 2020-09-25 14:28:52 +03:00
parent c516b80aff
commit 212df4e748
13 changed files with 172 additions and 145 deletions

31
src/Intensity.ts Normal file
View File

@ -0,0 +1,31 @@
export class Intensity {
constructor(private _value: number) {}
get value() {
return this._value;
}
get start() {
return this._value;
}
get end() {
return this._value;
}
}
export class IntensityRange {
constructor(private _start: number, private _end: number) {}
get value() {
return this._start;
}
get start() {
return this._start;
}
get end() {
return this._end;
}
}

View File

@ -1,5 +1,6 @@
import { IntervalType } from "./parser/tokenizer"; import { IntervalType } from "./parser/tokenizer";
import { Duration } from "./Duration"; import { Duration } from "./Duration";
import { Intensity, IntensityRange } from "./Intensity";
export type Workout = { export type Workout = {
name: string; name: string;
@ -11,7 +12,7 @@ export type Workout = {
export type Interval = { export type Interval = {
type: IntervalType; type: IntervalType;
duration: Duration; duration: Duration;
intensity: { from: number; to: number }; intensity: Intensity | IntensityRange;
cadence?: number; cadence?: number;
comments: Comment[]; comments: Comment[];
}; };

View File

@ -1,6 +1,7 @@
import { Interval } from "./ast"; import { Interval } from "./ast";
import { detectRepeats } from "./detectRepeats"; import { detectRepeats } from "./detectRepeats";
import { Duration } from "./Duration"; import { Duration } from "./Duration";
import { Intensity, IntensityRange } from "./Intensity";
describe("detectRepeats()", () => { describe("detectRepeats()", () => {
it("does nothing with empty array", () => { it("does nothing with empty array", () => {
@ -9,32 +10,32 @@ describe("detectRepeats()", () => {
it("does nothing when no interval repeats", () => { it("does nothing when no interval repeats", () => {
const intervals: Interval[] = [ const intervals: Interval[] = [
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
{ type: "Interval", duration: new Duration(60), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(60), intensity: new Intensity(1), comments: [] },
{ type: "Interval", duration: new Duration(30), intensity: { from: 1.2, to: 1.2 }, comments: [] }, { type: "Interval", duration: new Duration(30), intensity: new Intensity(1.2), comments: [] },
{ type: "Cooldown", duration: new Duration(60), intensity: { from: 1, to: 0.5 }, comments: [] }, { type: "Cooldown", duration: new Duration(60), intensity: new Intensity(1), comments: [] },
]; ];
expect(detectRepeats(intervals)).toEqual(intervals); expect(detectRepeats(intervals)).toEqual(intervals);
}); });
it("detects whole workout consisting of repetitions", () => { it("detects whole workout consisting of repetitions", () => {
const intervals: Interval[] = [ const intervals: Interval[] = [
{ type: "Interval", duration: new Duration(120), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(120), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
{ type: "Interval", duration: new Duration(120), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(120), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
{ type: "Interval", duration: new Duration(120), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(120), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
{ type: "Interval", duration: new Duration(120), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(120), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
]; ];
expect(detectRepeats(intervals)).toEqual([ expect(detectRepeats(intervals)).toEqual([
{ {
type: "repeat", type: "repeat",
times: 4, times: 4,
intervals: [ intervals: [
{ type: "Interval", duration: new Duration(120), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(120), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
], ],
comments: [], comments: [],
}, },
@ -43,54 +44,54 @@ describe("detectRepeats()", () => {
it("detects repetitions in the middle of workout", () => { it("detects repetitions in the middle of workout", () => {
const intervals: Interval[] = [ const intervals: Interval[] = [
{ type: "Warmup", duration: new Duration(60), intensity: { from: 0.5, to: 1 }, comments: [] }, { type: "Warmup", duration: new Duration(60), intensity: new IntensityRange(0.5, 1), comments: [] },
{ type: "Rest", duration: new Duration(120), intensity: { from: 0.2, to: 0.2 }, comments: [] }, { type: "Rest", duration: new Duration(120), intensity: new Intensity(0.2), comments: [] },
{ type: "Interval", duration: new Duration(60), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(60), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
{ type: "Interval", duration: new Duration(60), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(60), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
{ type: "Interval", duration: new Duration(60), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(60), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
{ type: "Interval", duration: new Duration(60), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(60), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
{ type: "Rest", duration: new Duration(120), intensity: { from: 0.2, to: 0.2 }, comments: [] }, { type: "Rest", duration: new Duration(120), intensity: new Intensity(0.2), comments: [] },
{ type: "Cooldown", duration: new Duration(60), intensity: { from: 1, to: 0.5 }, comments: [] }, { type: "Cooldown", duration: new Duration(60), intensity: new IntensityRange(1, 0.5), comments: [] },
]; ];
expect(detectRepeats(intervals)).toEqual([ expect(detectRepeats(intervals)).toEqual([
{ type: "Warmup", duration: new Duration(60), intensity: { from: 0.5, to: 1 }, comments: [] }, { type: "Warmup", duration: new Duration(60), intensity: new IntensityRange(0.5, 1), comments: [] },
{ type: "Rest", duration: new Duration(120), intensity: { from: 0.2, to: 0.2 }, comments: [] }, { type: "Rest", duration: new Duration(120), intensity: new Intensity(0.2), comments: [] },
{ {
type: "repeat", type: "repeat",
times: 4, times: 4,
intervals: [ intervals: [
{ type: "Interval", duration: new Duration(60), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(60), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
], ],
comments: [], comments: [],
}, },
{ type: "Rest", duration: new Duration(120), intensity: { from: 0.2, to: 0.2 }, comments: [] }, { type: "Rest", duration: new Duration(120), intensity: new Intensity(0.2), comments: [] },
{ type: "Cooldown", duration: new Duration(60), intensity: { from: 1, to: 0.5 }, comments: [] }, { type: "Cooldown", duration: new Duration(60), intensity: new IntensityRange(1, 0.5), comments: [] },
]); ]);
}); });
it("detects multiple repetitions", () => { it("detects multiple repetitions", () => {
const intervals: Interval[] = [ const intervals: Interval[] = [
{ type: "Interval", duration: new Duration(60), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(60), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
{ type: "Interval", duration: new Duration(60), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(60), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
{ type: "Interval", duration: new Duration(100), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(100), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(100), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(100), intensity: new Intensity(0.5), comments: [] },
{ type: "Interval", duration: new Duration(100), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(100), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(100), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(100), intensity: new Intensity(0.5), comments: [] },
]; ];
expect(detectRepeats(intervals)).toEqual([ expect(detectRepeats(intervals)).toEqual([
{ {
type: "repeat", type: "repeat",
times: 2, times: 2,
intervals: [ intervals: [
{ type: "Interval", duration: new Duration(60), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(60), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
], ],
comments: [], comments: [],
}, },
@ -98,8 +99,8 @@ describe("detectRepeats()", () => {
type: "repeat", type: "repeat",
times: 2, times: 2,
intervals: [ intervals: [
{ type: "Interval", duration: new Duration(100), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(100), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(100), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(100), intensity: new Intensity(0.5), comments: [] },
], ],
comments: [], comments: [],
}, },
@ -108,22 +109,22 @@ describe("detectRepeats()", () => {
it("takes cadence differences into account", () => { it("takes cadence differences into account", () => {
const intervals: Interval[] = [ const intervals: Interval[] = [
{ type: "Interval", duration: new Duration(120), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(120), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
{ type: "Interval", duration: new Duration(120), intensity: { from: 1, to: 1 }, cadence: 100, comments: [] }, { type: "Interval", duration: new Duration(120), intensity: new Intensity(1), cadence: 100, comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, cadence: 80, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), cadence: 80, comments: [] },
{ type: "Interval", duration: new Duration(120), intensity: { from: 1, to: 1 }, cadence: 100, comments: [] }, { type: "Interval", duration: new Duration(120), intensity: new Intensity(1), cadence: 100, comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, cadence: 80, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), cadence: 80, comments: [] },
]; ];
expect(detectRepeats(intervals)).toEqual([ expect(detectRepeats(intervals)).toEqual([
{ type: "Interval", duration: new Duration(120), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(120), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), comments: [] },
{ {
type: "repeat", type: "repeat",
times: 2, times: 2,
intervals: [ intervals: [
{ type: "Interval", duration: new Duration(120), intensity: { from: 1, to: 1 }, cadence: 100, comments: [] }, { type: "Interval", duration: new Duration(120), intensity: new Intensity(1), cadence: 100, comments: [] },
{ type: "Rest", duration: new Duration(60), intensity: { from: 0.5, to: 0.5 }, cadence: 80, comments: [] }, { type: "Rest", duration: new Duration(60), intensity: new Intensity(0.5), cadence: 80, comments: [] },
], ],
comments: [], comments: [],
}, },
@ -132,14 +133,14 @@ describe("detectRepeats()", () => {
it("does not consider warmup/cooldown-range intervals to be repeatable", () => { it("does not consider warmup/cooldown-range intervals to be repeatable", () => {
const intervals: Interval[] = [ const intervals: Interval[] = [
{ type: "Warmup", duration: new Duration(60), intensity: { from: 0.1, to: 1 }, comments: [] }, { type: "Warmup", duration: new Duration(60), intensity: new IntensityRange(0.1, 1), comments: [] },
{ type: "Cooldown", duration: new Duration(120), intensity: { from: 1, to: 0.5 }, comments: [] }, { type: "Cooldown", duration: new Duration(120), intensity: new IntensityRange(1, 0.5), comments: [] },
{ type: "Warmup", duration: new Duration(60), intensity: { from: 0.1, to: 1 }, comments: [] }, { type: "Warmup", duration: new Duration(60), intensity: new IntensityRange(0.1, 1), comments: [] },
{ type: "Cooldown", duration: new Duration(120), intensity: { from: 1, to: 0.5 }, comments: [] }, { type: "Cooldown", duration: new Duration(120), intensity: new IntensityRange(1, 0.5), comments: [] },
{ type: "Warmup", duration: new Duration(60), intensity: { from: 0.1, to: 1 }, comments: [] }, { type: "Warmup", duration: new Duration(60), intensity: new IntensityRange(0.1, 1), comments: [] },
{ type: "Cooldown", duration: new Duration(120), intensity: { from: 1, to: 0.5 }, comments: [] }, { type: "Cooldown", duration: new Duration(120), intensity: new IntensityRange(1, 0.5), comments: [] },
{ type: "Warmup", duration: new Duration(60), intensity: { from: 0.1, to: 1 }, comments: [] }, { type: "Warmup", duration: new Duration(60), intensity: new IntensityRange(0.1, 1), comments: [] },
{ type: "Cooldown", duration: new Duration(120), intensity: { from: 1, to: 0.5 }, comments: [] }, { type: "Cooldown", duration: new Duration(120), intensity: new IntensityRange(1, 0.5), comments: [] },
]; ];
expect(detectRepeats(intervals)).toEqual(intervals); expect(detectRepeats(intervals)).toEqual(intervals);
}); });
@ -149,7 +150,7 @@ describe("detectRepeats()", () => {
{ {
type: "Interval", type: "Interval",
duration: new Duration(100), duration: new Duration(100),
intensity: { from: 1, to: 1 }, intensity: new Intensity(1),
comments: [ comments: [
{ offset: new Duration(0), text: "Let's start" }, { offset: new Duration(0), text: "Let's start" },
{ offset: new Duration(20), text: "Stay strong!" }, { offset: new Duration(20), text: "Stay strong!" },
@ -159,7 +160,7 @@ describe("detectRepeats()", () => {
{ {
type: "Rest", type: "Rest",
duration: new Duration(100), duration: new Duration(100),
intensity: { from: 0.5, to: 0.5 }, intensity: new Intensity(0.5),
comments: [ comments: [
{ offset: new Duration(0), text: "Huh... have a rest" }, { offset: new Duration(0), text: "Huh... have a rest" },
{ offset: new Duration(80), text: "Ready for next?" }, { offset: new Duration(80), text: "Ready for next?" },
@ -168,7 +169,7 @@ describe("detectRepeats()", () => {
{ {
type: "Interval", type: "Interval",
duration: new Duration(100), duration: new Duration(100),
intensity: { from: 1, to: 1 }, intensity: new Intensity(1),
comments: [ comments: [
{ offset: new Duration(0), text: "Bring it on again!" }, { offset: new Duration(0), text: "Bring it on again!" },
{ offset: new Duration(50), text: "Half way" }, { offset: new Duration(50), text: "Half way" },
@ -178,7 +179,7 @@ describe("detectRepeats()", () => {
{ {
type: "Rest", type: "Rest",
duration: new Duration(100), duration: new Duration(100),
intensity: { from: 0.5, to: 0.5 }, intensity: new Intensity(0.5),
comments: [ comments: [
{ offset: new Duration(30), text: "Wow... you did it!" }, { offset: new Duration(30), text: "Wow... you did it!" },
{ offset: new Duration(40), text: "Nice job." }, { offset: new Duration(40), text: "Nice job." },
@ -191,8 +192,8 @@ describe("detectRepeats()", () => {
type: "repeat", type: "repeat",
times: 2, times: 2,
intervals: [ intervals: [
{ type: "Interval", duration: new Duration(100), intensity: { from: 1, to: 1 }, comments: [] }, { type: "Interval", duration: new Duration(100), intensity: new Intensity(1), comments: [] },
{ type: "Rest", duration: new Duration(100), intensity: { from: 0.5, to: 0.5 }, comments: [] }, { type: "Rest", duration: new Duration(100), intensity: new Intensity(0.5), comments: [] },
], ],
comments: [ comments: [
{ offset: new Duration(0), text: "Let's start" }, { offset: new Duration(0), text: "Let's start" },

View File

@ -1,6 +1,7 @@
import { eqProps, flatten, zip } from "ramda"; import { eqProps, flatten, zip } from "ramda";
import { Interval, Comment } from "./ast"; import { Interval, Comment } from "./ast";
import { Duration } from "./Duration"; import { Duration } from "./Duration";
import { IntensityRange } from "./Intensity";
export type RepeatedInterval = { export type RepeatedInterval = {
type: "repeat"; type: "repeat";
@ -54,7 +55,7 @@ const stripComments = (intervals: Interval[]): Interval[] => {
return intervals.map(({ comments, ...rest }) => ({ comments: [], ...rest })); return intervals.map(({ comments, ...rest }) => ({ comments: [], ...rest }));
}; };
const isRangeInterval = (interval: Interval): boolean => interval.intensity.from !== interval.intensity.to; const isRangeInterval = (interval: Interval): boolean => interval.intensity instanceof IntensityRange;
export const detectRepeats = (intervals: Interval[]): (Interval | RepeatedInterval)[] => { export const detectRepeats = (intervals: Interval[]): (Interval | RepeatedInterval)[] => {
if (intervals.length < windowSize) { if (intervals.length < windowSize) {

View File

@ -19,8 +19,8 @@ const generateRangeInterval = (
{ {
_attr: { _attr: {
Duration: duration.seconds, Duration: duration.seconds,
PowerLow: intensity.from, PowerLow: intensity.start,
PowerHigh: intensity.to, PowerHigh: intensity.end,
...(cadence ? { Cadence: cadence } : {}), ...(cadence ? { Cadence: cadence } : {}),
}, },
}, },
@ -35,7 +35,7 @@ const generateSteadyStateInterval = ({ duration, intensity, cadence, comments }:
{ {
_attr: { _attr: {
Duration: duration.seconds, Duration: duration.seconds,
Power: intensity.from, Power: intensity.value,
...(cadence ? { Cadence: cadence } : {}), ...(cadence ? { Cadence: cadence } : {}),
}, },
}, },
@ -53,11 +53,11 @@ const generateRepeatInterval = (repInterval: RepeatedInterval): xml.XmlObject =>
Repeat: repInterval.times, Repeat: repInterval.times,
OnDuration: on.duration.seconds, OnDuration: on.duration.seconds,
OnPower: on.intensity.from, OnPower: on.intensity.start,
...(on.cadence ? { Cadence: on.cadence } : {}), ...(on.cadence ? { Cadence: on.cadence } : {}),
OffDuration: off.duration.seconds, OffDuration: off.duration.seconds,
OffPower: off.intensity.from, OffPower: off.intensity.end,
...(off.cadence ? { CadenceResting: off.cadence } : {}), ...(off.cadence ? { CadenceResting: off.cadence } : {}),
}, },
}, },
@ -72,9 +72,9 @@ const generateInterval = (interval: Interval | RepeatedInterval): xml.XmlObject
} }
const { intensity } = interval; const { intensity } = interval;
if (intensity.from < intensity.to) { if (intensity.start < intensity.end) {
return generateRangeInterval("Warmup", interval); return generateRangeInterval("Warmup", interval);
} else if (intensity.from > intensity.to) { } else if (intensity.start > intensity.end) {
return generateRangeInterval("Cooldown", interval); return generateRangeInterval("Cooldown", interval);
} else { } else {
return generateSteadyStateInterval(interval); return generateSteadyStateInterval(interval);

View File

@ -74,9 +74,8 @@ Rest: 5:00 45%
"duration": Duration { "duration": Duration {
"seconds": 300, "seconds": 300,
}, },
"intensity": Object { "intensity": Intensity {
"from": 0.5, "_value": 0.5,
"to": 0.5,
}, },
"type": "Rest", "type": "Rest",
}, },
@ -86,9 +85,8 @@ Rest: 5:00 45%
"duration": Duration { "duration": Duration {
"seconds": 600, "seconds": 600,
}, },
"intensity": Object { "intensity": Intensity {
"from": 0.8, "_value": 0.8,
"to": 0.8,
}, },
"type": "Interval", "type": "Interval",
}, },
@ -98,9 +96,8 @@ Rest: 5:00 45%
"duration": Duration { "duration": Duration {
"seconds": 300, "seconds": 300,
}, },
"intensity": Object { "intensity": Intensity {
"from": 0.45, "_value": 0.45,
"to": 0.45,
}, },
"type": "Rest", "type": "Rest",
}, },
@ -139,9 +136,8 @@ Interval: 5:00 50%
"duration": Duration { "duration": Duration {
"seconds": 300, "seconds": 300,
}, },
"intensity": Object { "intensity": Intensity {
"from": 0.5, "_value": 0.5,
"to": 0.5,
}, },
"type": "Interval", "type": "Interval",
}, },
@ -151,9 +147,8 @@ Interval: 5:00 50%
"duration": Duration { "duration": Duration {
"seconds": 600, "seconds": 600,
}, },
"intensity": Object { "intensity": Intensity {
"from": 1, "_value": 1,
"to": 1,
}, },
"type": "Interval", "type": "Interval",
}, },
@ -163,9 +158,8 @@ Interval: 5:00 50%
"duration": Duration { "duration": Duration {
"seconds": 300, "seconds": 300,
}, },
"intensity": Object { "intensity": Intensity {
"from": 0.5, "_value": 0.5,
"to": 0.5,
}, },
"type": "Interval", "type": "Interval",
}, },
@ -191,9 +185,9 @@ Cooldown: 5:30 70%..45%
"duration": Duration { "duration": Duration {
"seconds": 330, "seconds": 330,
}, },
"intensity": Object { "intensity": IntensityRange {
"from": 0.5, "_end": 0.8,
"to": 0.8, "_start": 0.5,
}, },
"type": "Warmup", "type": "Warmup",
}, },
@ -203,9 +197,9 @@ Cooldown: 5:30 70%..45%
"duration": Duration { "duration": Duration {
"seconds": 330, "seconds": 330,
}, },
"intensity": Object { "intensity": IntensityRange {
"from": 0.7, "_end": 0.45,
"to": 0.45, "_start": 0.7,
}, },
"type": "Cooldown", "type": "Cooldown",
}, },
@ -235,9 +229,8 @@ Cooldown: 5:30 70%..45%
"duration": Duration { "duration": Duration {
"seconds": 10, "seconds": 10,
}, },
"intensity": Object { "intensity": Intensity {
"from": 0.5, "_value": 0.5,
"to": 0.5,
}, },
"type": "Interval", "type": "Interval",
} }
@ -249,9 +242,8 @@ Cooldown: 5:30 70%..45%
"duration": Duration { "duration": Duration {
"seconds": 10, "seconds": 10,
}, },
"intensity": Object { "intensity": Intensity {
"from": 0.5, "_value": 0.5,
"to": 0.5,
}, },
"type": "Interval", "type": "Interval",
} }
@ -263,9 +255,8 @@ Cooldown: 5:30 70%..45%
"duration": Duration { "duration": Duration {
"seconds": 10, "seconds": 10,
}, },
"intensity": Object { "intensity": Intensity {
"from": 0.5, "_value": 0.5,
"to": 0.5,
}, },
"type": "Interval", "type": "Interval",
} }
@ -280,9 +271,8 @@ Cooldown: 5:30 70%..45%
"duration": Duration { "duration": Duration {
"seconds": 10, "seconds": 10,
}, },
"intensity": Object { "intensity": Intensity {
"from": 0.5, "_value": 0.5,
"to": 0.5,
}, },
"type": "Interval", "type": "Interval",
} }
@ -294,9 +284,8 @@ Cooldown: 5:30 70%..45%
"duration": Duration { "duration": Duration {
"seconds": 10, "seconds": 10,
}, },
"intensity": Object { "intensity": Intensity {
"from": 0.5, "_value": 0.5,
"to": 0.5,
}, },
"type": "Interval", "type": "Interval",
} }
@ -406,9 +395,8 @@ Rest: 5:00 50%
"duration": Duration { "duration": Duration {
"seconds": 600, "seconds": 600,
}, },
"intensity": Object { "intensity": Intensity {
"from": 0.9, "_value": 0.9,
"to": 0.9,
}, },
"type": "Interval", "type": "Interval",
}, },
@ -431,9 +419,8 @@ Rest: 5:00 50%
"duration": Duration { "duration": Duration {
"seconds": 300, "seconds": 300,
}, },
"intensity": Object { "intensity": Intensity {
"from": 0.5, "_value": 0.5,
"to": 0.5,
}, },
"type": "Rest", "type": "Rest",
}, },

View File

@ -1,5 +1,6 @@
import { Interval, Workout, Comment } from "../ast"; import { Interval, Workout, Comment } from "../ast";
import { Duration } from "../Duration"; import { Duration } from "../Duration";
import { Intensity, IntensityRange } from "../Intensity";
import { ParseError } from "./ParseError"; import { ParseError } from "./ParseError";
import { SourceLocation, Token } from "./tokenizer"; import { SourceLocation, Token } from "./tokenizer";
@ -91,10 +92,10 @@ const parseIntervalParams = (tokens: Token[], loc: SourceLocation): [IntervalDat
data.cadence = token.value; data.cadence = token.value;
tokens.shift(); tokens.shift();
} else if (token.type === "intensity") { } else if (token.type === "intensity") {
data.intensity = { from: token.value, to: token.value }; data.intensity = new Intensity(token.value);
tokens.shift(); tokens.shift();
} else if (token.type === "intensity-range") { } else if (token.type === "intensity-range") {
data.intensity = { from: token.value[0], to: token.value[1] }; data.intensity = new IntensityRange(token.value[0], token.value[1]);
tokens.shift(); tokens.shift();
} else { } else {
break; break;

View File

@ -1,8 +1,9 @@
import { pipe } from "ramda"; import { pipe } from "ramda";
import { Interval } from "../ast"; import { Interval } from "../ast";
import { Intensity } from "../Intensity";
import { average } from "./average"; import { average } from "./average";
import { intervalsToIntensities } from "./intervalsToIntensities"; import { intervalsToIntensities } from "./intervalsToIntensities";
export const averageIntensity = (intervals: Interval[]): number => { export const averageIntensity = (intervals: Interval[]): Intensity => {
return pipe(intervalsToIntensities, average)(intervals); return new Intensity(pipe(intervalsToIntensities, average)(intervals));
}; };

View File

@ -14,8 +14,8 @@ export const stats = ({ intervals }: Workout): string => {
return ` return `
Total duration: ${(duration.seconds / 60).toFixed()} minutes Total duration: ${(duration.seconds / 60).toFixed()} minutes
Average intensity: ${(avgIntensity * 100).toFixed()}% Average intensity: ${(avgIntensity.value * 100).toFixed()}%
Normalized intensity: ${(normIntensity * 100).toFixed()}% Normalized intensity: ${(normIntensity.value * 100).toFixed()}%
TSS #1: ${tss(intervals).toFixed()} TSS #1: ${tss(intervals).toFixed()}
TSS #2: ${tss2(duration, normIntensity).toFixed()} TSS #2: ${tss2(duration, normIntensity).toFixed()}

View File

@ -3,13 +3,13 @@ import { Interval } from "../ast";
// Converts interval to array of intensity values for each second // Converts interval to array of intensity values for each second
const intervalToIntensities = ({ duration, intensity }: Interval): number[] => { const intervalToIntensities = ({ duration, intensity }: Interval): number[] => {
const seconds = []; const intensities: number[] = [];
const { from, to } = intensity; const [from, to] = [intensity.start, intensity.end];
for (let i = 0; i < duration.seconds; i++) { for (let i = 0; i < duration.seconds; i++) {
// Intensity in a single second // Intensity in a single second
seconds.push(from + (to - from) * (i / duration.seconds)); intensities.push(from + (to - from) * (i / duration.seconds));
} }
return seconds; return intensities;
}; };
export const intervalsToIntensities = chain(intervalToIntensities); export const intervalsToIntensities = chain(intervalToIntensities);

View File

@ -1,5 +1,6 @@
import { pipe, sum } from "ramda"; import { pipe, sum } from "ramda";
import { Interval } from "../ast"; import { Interval } from "../ast";
import { Intensity } from "../Intensity";
import { average } from "./average"; import { average } from "./average";
import { intervalsToIntensities } from "./intervalsToIntensities"; import { intervalsToIntensities } from "./intervalsToIntensities";
@ -24,12 +25,14 @@ const fourthPower = (x: number) => Math.pow(x, 4);
const fourthRoot = (x: number) => Math.pow(x, 1 / 4); const fourthRoot = (x: number) => Math.pow(x, 1 / 4);
export const normalizedIntensity = (intervals: Interval[]): number => { export const normalizedIntensity = (intervals: Interval[]): Intensity => {
return pipe( return new Intensity(
intervalsToIntensities, pipe(
rollingAverages, intervalsToIntensities,
(averages) => averages.map(fourthPower), rollingAverages,
average, (averages) => averages.map(fourthPower),
fourthRoot, average,
)(intervals); fourthRoot,
)(intervals),
);
}; };

View File

@ -24,10 +24,10 @@ const rangeTss = (duration: Duration, from: number, to: number): number => {
}; };
const intervalTss = ({ duration, intensity }: Interval): number => { const intervalTss = ({ duration, intensity }: Interval): number => {
if (intensity.from === intensity.to) { if (intensity.start === intensity.end) {
return steadyTss(duration, intensity.from); return steadyTss(duration, intensity.value);
} else { } else {
return rangeTss(duration, intensity.from, intensity.to); return rangeTss(duration, intensity.start, intensity.end);
} }
}; };

View File

@ -1,4 +1,5 @@
import { Duration } from "../Duration"; import { Duration } from "../Duration";
import { Intensity } from "../Intensity";
// Training Stress Score formula from Training and Racing with a Power Meter: // Training Stress Score formula from Training and Racing with a Power Meter:
// //
@ -13,6 +14,6 @@ import { Duration } from "../Duration";
// TSS = (s * (FTP * IF) * IF) / (FTP * 3600) * 100 // TSS = (s * (FTP * IF) * IF) / (FTP * 3600) * 100
// TSS = (s * IF * IF) / 3600 * 100 // TSS = (s * IF * IF) / 3600 * 100
export const tss2 = (duration: Duration, intensity: number): number => { export const tss2 = (duration: Duration, intensity: Intensity): number => {
return ((duration.seconds * intensity * intensity) / 3600) * 100; return ((duration.seconds * Math.pow(intensity.value, 2)) / 3600) * 100;
}; };