diff --git a/lib/cutout/CutoutViewModel.ts b/lib/cutout/CutoutViewModel.ts index d9bcc25..e690326 100644 --- a/lib/cutout/CutoutViewModel.ts +++ b/lib/cutout/CutoutViewModel.ts @@ -1,7 +1,7 @@ import ObservableState from "../async/ObservableState"; import { extentBy } from "../itertools/accumulate"; import flatten from "../itertools/flatten"; -import { today } from "../time"; +import { findByDate, today } from "../time"; import Series, { type Data, type Observation } from "../time/Series"; import Stat from "../Stat"; import CutoutIndex from "./CutoutIndex"; @@ -18,7 +18,7 @@ class CutoutViewModel { public readonly stats: ObservableState; private constructor(series: Series[]) { - const [cutout, index] = series.map(series => Series.find(series, today())); + const [cutout, index] = series.map(findByDate(today())); this.#series = series; this.selected = new ObservableState(cutout); @@ -45,7 +45,7 @@ class CutoutViewModel { } public selectDate = (date = today()): void => { - const [cutout, index] = this.#series.map(series => Series.find(series, date)); + const [cutout, index] = this.#series.map(findByDate(date)); this.stats.state = [ Stat.from("Cutout", cutout.value), diff --git a/lib/cutout/PrimalViewModel.ts b/lib/cutout/PrimalViewModel.ts index 70586b9..89d48d6 100644 --- a/lib/cutout/PrimalViewModel.ts +++ b/lib/cutout/PrimalViewModel.ts @@ -1,9 +1,8 @@ -import type { UnaryOperator } from ".."; import ObservableState from "../async/ObservableState"; import { extentBy } from "../itertools/accumulate"; import map from "../itertools/map"; import zip from "../itertools/zip"; -import { today } from "../time"; +import { findByDate, today } from "../time"; import Series, { type Data } from "../time/Series"; import Stat from "../Stat"; import type Cutout from "."; @@ -13,9 +12,6 @@ interface PrimalStat extends Stat { selected: boolean; } -const findByDate = (date: Date): UnaryOperator => - (series: Series) => Series.find(series, date); - class PrimalViewModel { public static from = (cutout: Iterable, primal = Primal.Belly): PrimalViewModel => new PrimalViewModel(primal, [ diff --git a/lib/time/index.ts b/lib/time/index.ts index 1fbef14..f131e32 100644 --- a/lib/time/index.ts +++ b/lib/time/index.ts @@ -1,4 +1,6 @@ -import { parse, format } from "date-fns"; +import { format, parse } from "date-fns"; +import type { UnaryOperator } from ".."; +import Series, { type Data } from "./Series"; const dateFormat = "yyyy-MM-dd"; @@ -13,4 +15,7 @@ const getDate = (date: string): Date => const formatDate = (date: Date): string => format(date, dateFormat); -export { formatDate, getDate, today }; +const findByDate = (date: Date): UnaryOperator => + (series: Series) => Series.find(series, date); + +export { formatDate, getDate, today, findByDate };