Skip to content

Commit

Permalink
Create findByDate function for ViewModels
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkedev committed Feb 27, 2024
1 parent 7ce3aa8 commit 68565f6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/cutout/CutoutViewModel.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -18,7 +18,7 @@ class CutoutViewModel {
public readonly stats: ObservableState<Stat[]>;

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);

Expand All @@ -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),
Expand Down
6 changes: 1 addition & 5 deletions lib/cutout/PrimalViewModel.ts
Original file line number Diff line number Diff line change
@@ -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 ".";
Expand All @@ -13,9 +12,6 @@ interface PrimalStat extends Stat {
selected: boolean;
}

const findByDate = (date: Date): UnaryOperator<Series, Data> =>
(series: Series) => Series.find(series, date);

class PrimalViewModel {
public static from = (cutout: Iterable<Cutout>, primal = Primal.Belly): PrimalViewModel =>
new PrimalViewModel(primal, [
Expand Down
9 changes: 7 additions & 2 deletions lib/time/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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, Data> =>
(series: Series) => Series.find(series, date);

export { formatDate, getDate, today, findByDate };

0 comments on commit 68565f6

Please sign in to comment.