Skip to content

Commit

Permalink
feat(dates): allow to set granularity for isInFuture and isInPast
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhaenisch committed Nov 5, 2020
1 parent 92906d5 commit b889880
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
28 changes: 22 additions & 6 deletions src/dates/dates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
setMomentReference,
} from '.';
import { Time } from '../time';
import { wait } from '../utils';

setMomentReference(moment);

Expand Down Expand Up @@ -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);
});
});
});
8 changes: 5 additions & 3 deletions src/dates/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Moment } from 'moment-timezone';
import { Moment, unitOfTime } from 'moment-timezone';
import { createRange } from '../arrays';
import { Time, TimeLike } from '../time';

Expand Down Expand Up @@ -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);

0 comments on commit b889880

Please sign in to comment.