From b889880b59744e03821634bf0b263d07c3675ed4 Mon Sep 17 00:00:00 2001 From: simonhaenisch Date: Thu, 5 Nov 2020 15:41:45 +0100 Subject: [PATCH] feat(dates): allow to set granularity for isInFuture and isInPast --- src/dates/dates.spec.ts | 28 ++++++++++++++++++++++------ src/dates/index.ts | 8 +++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/dates/dates.spec.ts b/src/dates/dates.spec.ts index 65b97b6..4431cdc 100644 --- a/src/dates/dates.spec.ts +++ b/src/dates/dates.spec.ts @@ -12,6 +12,7 @@ import { setMomentReference, } from '.'; import { Time } from '../time'; +import { wait } from '../utils'; setMomentReference(moment); @@ -139,25 +140,40 @@ describe('Date Helpers', () => { }); }); + const yesterday = moment().subtract(1, 'day'); + const tomorrow = moment().add(1, 'day'); + describe('isInFuture(date)', () => { it('should tell whether the given date is in the future', () => { - const yesterday = moment().subtract(1, 'day'); - const tomorrow = moment().add(1, 'day'); - expect(isInFuture(yesterday)).toBe(false); expect(isInFuture(moment())).toBe(false); expect(isInFuture(tomorrow)).toBe(true); }); + + it('should respect the given granularity', async () => { + if (moment().seconds() >= 59) { + await wait(2000); + } + + expect(isInFuture(moment().add(1, 'second'), 'second')).toBe(true); + expect(isInFuture(moment().add(1, 'second'), 'minute')).toBe(false); + }); }); describe('isInPast(date)', () => { it('should tell whether the given date is in the past', () => { - const yesterday = moment().subtract(1, 'day'); - const tomorrow = moment().add(1, 'day'); - expect(isInPast(yesterday)).toBe(true); expect(isInPast(moment())).toBe(false); expect(isInPast(tomorrow)).toBe(false); }); + + it('should respect the given granularity', async () => { + if (moment().seconds() <= 1) { + await wait(2000); + } + + expect(isInPast(moment().subtract(1, 'second'), 'second')).toBe(true); + expect(isInPast(moment().subtract(1, 'second'), 'minute')).toBe(false); + }); }); }); diff --git a/src/dates/index.ts b/src/dates/index.ts index 7dd476f..e44e3fc 100644 --- a/src/dates/index.ts +++ b/src/dates/index.ts @@ -1,4 +1,4 @@ -import { Moment } from 'moment-timezone'; +import { Moment, unitOfTime } from 'moment-timezone'; import { createRange } from '../arrays'; import { Time, TimeLike } from '../time'; @@ -145,9 +145,11 @@ export const getCalendarMonthBoundaries = (month: Moment, weekStartsOnSunday = f /** * Check whether the given moment is in the future. */ -export const isInFuture = (date: Moment | string) => moment(date).isAfter(moment()); +export const isInFuture = (date: Moment | string, granularity?: unitOfTime.StartOf) => + moment(date).isAfter(moment(), granularity); /** * Check whether the given moment is in the past. */ -export const isInPast = (date: Moment | string) => moment(date).isBefore(moment()); +export const isInPast = (date: Moment | string, granularity?: unitOfTime.StartOf) => + moment(date).isBefore(moment(), granularity);