Created helper curried function to compare two values

This commit is contained in:
Alejandro Celaya 2022-12-05 17:29:59 +01:00
parent 1654784471
commit 5d712d7d78
2 changed files with 10 additions and 7 deletions

View file

@ -1,6 +1,7 @@
import { subDays, startOfDay, endOfDay } from 'date-fns'; import { subDays, startOfDay, endOfDay } from 'date-fns';
import { cond, filter, isEmpty, T } from 'ramda'; import { cond, filter, isEmpty, T } from 'ramda';
import { dateOrNull, DateOrString, formatInternational, isBeforeOrEqual, now, parseISO } from './date'; import { dateOrNull, DateOrString, formatInternational, isBeforeOrEqual, now, parseISO } from './date';
import { equals } from '../utils';
export interface DateRange { export interface DateRange {
startDate?: Date | null; startDate?: Date | null;
@ -68,13 +69,13 @@ const startOfDaysAgo = (daysAgo: number) => startOfDay(subDays(now(), daysAgo));
const endingToday = (startDate: Date): DateRange => ({ startDate, endDate: endOfDay(now()) }); const endingToday = (startDate: Date): DateRange => ({ startDate, endDate: endOfDay(now()) });
export const intervalToDateRange = cond<[DateInterval | undefined], DateRange>([ export const intervalToDateRange = cond<[DateInterval | undefined], DateRange>([
[(dateInterval) => dateInterval === 'today', () => endingToday(startOfDay(now()))], [equals('today'), () => endingToday(startOfDay(now()))],
[(dateInterval) => dateInterval === 'yesterday', () => ({ startDate: startOfDaysAgo(1), endDate: endOfDay(subDays(now(), 1)) })], [equals('yesterday'), () => ({ startDate: startOfDaysAgo(1), endDate: endOfDay(subDays(now(), 1)) })],
[(dateInterval) => dateInterval === 'last7Days', () => endingToday(startOfDaysAgo(7))], [equals('last7Days'), () => endingToday(startOfDaysAgo(7))],
[(dateInterval) => dateInterval === 'last30Days', () => endingToday(startOfDaysAgo(30))], [equals('last30Days'), () => endingToday(startOfDaysAgo(30))],
[(dateInterval) => dateInterval === 'last90Days', () => endingToday(startOfDaysAgo(90))], [equals('last90Days'), () => endingToday(startOfDaysAgo(90))],
[(dateInterval) => dateInterval === 'last180Days', () => endingToday(startOfDaysAgo(180))], [equals('last180Days'), () => endingToday(startOfDaysAgo(180))],
[(dateInterval) => dateInterval === 'last365Days', () => endingToday(startOfDaysAgo(365))], [equals('last365Days'), () => endingToday(startOfDaysAgo(365))],
[T, () => ({})], [T, () => ({})],
]); ]);

View file

@ -24,3 +24,5 @@ export type OptionalString = Optional<string>;
export const nonEmptyValueOrNull = <T>(value: T): T | null => (isEmpty(value) ? null : value); export const nonEmptyValueOrNull = <T>(value: T): T | null => (isEmpty(value) ? null : value);
export const capitalize = <T extends string>(value: T): string => `${value.charAt(0).toUpperCase()}${value.slice(1)}`; export const capitalize = <T extends string>(value: T): string => `${value.charAt(0).toUpperCase()}${value.slice(1)}`;
export const equals = (value: any) => (otherValue: any) => value === otherValue;