From 138d6c808b86bb54b4ee89900bae2d0a2c578618 Mon Sep 17 00:00:00 2001 From: Andrew Kirkegaard Date: Tue, 27 Feb 2024 11:29:02 -0500 Subject: [PATCH] Create findByDate function for ViewModels --- lib/cutout/CutoutViewModel.ts | 6 +++--- lib/cutout/PrimalViewModel.ts | 6 +----- lib/time/index.ts | 9 +++++++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/cutout/CutoutViewModel.ts b/lib/cutout/CutoutViewModel.ts index d9bcc257..e6903260 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 70586b90..89d48d69 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 1fbef14b..f131e32e 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 };