Implement Seconds.add()

This commit is contained in:
Rene Saarsoo 2020-09-25 09:42:55 +03:00
parent 6170796433
commit be5b9a5146
3 changed files with 7 additions and 4 deletions

View File

@ -1,3 +1,7 @@
export class Seconds { export class Seconds {
constructor(readonly value: number) {} constructor(readonly value: number) {}
add(other: Seconds): Seconds {
return new Seconds(this.value + other.value);
}
} }

View File

@ -34,7 +34,7 @@ const countRepetitions = (reference: Interval[], intervals: Interval[], startInd
const offsetComments = (interval: Interval, baseOffset: Seconds): Comment[] => { const offsetComments = (interval: Interval, baseOffset: Seconds): Comment[] => {
return interval.comments.map(({ offset, ...rest }) => ({ return interval.comments.map(({ offset, ...rest }) => ({
offset: new Seconds(baseOffset.value + offset.value), offset: baseOffset.add(offset),
...rest, ...rest,
})); }));
}; };
@ -44,7 +44,7 @@ const collectComments = (intervals: Interval[]): Comment[] => {
return flatten( return flatten(
intervals.map((interval) => { intervals.map((interval) => {
const comments = offsetComments(interval, previousIntervalsDuration); const comments = offsetComments(interval, previousIntervalsDuration);
previousIntervalsDuration = new Seconds(previousIntervalsDuration.value + interval.duration.value); previousIntervalsDuration = previousIntervalsDuration.add(interval.duration);
return comments; return comments;
}), }),
); );

View File

@ -1,6 +1,5 @@
import { map, sum } from "ramda";
import { Interval } from "../ast"; import { Interval } from "../ast";
import { Seconds } from "../Seconds"; import { Seconds } from "../Seconds";
export const totalDuration = (intervals: Interval[]): Seconds => export const totalDuration = (intervals: Interval[]): Seconds =>
new Seconds(sum(map((interval) => interval.duration.value, intervals))); intervals.reduce((total, interval) => total.add(interval.duration), new Seconds(0));