diff options
Diffstat (limited to 'date-fns/src/isPast')
-rw-r--r-- | date-fns/src/isPast/benchmark.js | 19 | ||||
-rw-r--r-- | date-fns/src/isPast/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/isPast/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/isPast/index.ts | 33 | ||||
-rw-r--r-- | date-fns/src/isPast/test.ts | 42 |
5 files changed, 150 insertions, 0 deletions
diff --git a/date-fns/src/isPast/benchmark.js b/date-fns/src/isPast/benchmark.js new file mode 100644 index 0000000..d5403bd --- /dev/null +++ b/date-fns/src/isPast/benchmark.js @@ -0,0 +1,19 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import isPast from '.' + +suite( + 'isPast', + () => { + benchmark('date-fns', function() { + return isPast(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/date-fns/src/isPast/index.d.ts b/date-fns/src/isPast/index.d.ts new file mode 100644 index 0000000..8ae92ca --- /dev/null +++ b/date-fns/src/isPast/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isPast } from 'date-fns' +export default isPast diff --git a/date-fns/src/isPast/index.js.flow b/date-fns/src/isPast/index.js.flow new file mode 100644 index 0000000..d3f01ee --- /dev/null +++ b/date-fns/src/isPast/index.js.flow @@ -0,0 +1,52 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +export type Interval = { + start: Date | number, + end: Date | number, +} + +export type Locale = { + code?: string, + formatDistance?: (...args: Array<any>) => any, + formatRelative?: (...args: Array<any>) => any, + localize?: { + ordinalNumber: (...args: Array<any>) => any, + era: (...args: Array<any>) => any, + quarter: (...args: Array<any>) => any, + month: (...args: Array<any>) => any, + day: (...args: Array<any>) => any, + dayPeriod: (...args: Array<any>) => any, + }, + formatLong?: { + date: (...args: Array<any>) => any, + time: (...args: Array<any>) => any, + dateTime: (...args: Array<any>) => any, + }, + match?: { + ordinalNumber: (...args: Array<any>) => any, + era: (...args: Array<any>) => any, + quarter: (...args: Array<any>) => any, + month: (...args: Array<any>) => any, + day: (...args: Array<any>) => any, + dayPeriod: (...args: Array<any>) => any, + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7, + }, +} + +export type Duration = { + years?: number, + months?: number, + weeks?: number, + days?: number, + hours?: number, + minutes?: number, + seconds?: number, +} + +export type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6 + +declare module.exports: (date: Date | number) => boolean diff --git a/date-fns/src/isPast/index.ts b/date-fns/src/isPast/index.ts new file mode 100644 index 0000000..b0c251e --- /dev/null +++ b/date-fns/src/isPast/index.ts @@ -0,0 +1,33 @@ +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name isPast + * @category Common Helpers + * @summary Is the given date in the past? + * @pure false + * + * @description + * Is the given date in the past? + * + * > ⚠️ Please note that this function is not present in the FP submodule as + * > it uses `Date.now()` internally hence impure and can't be safely curried. + * + * ### v2.0.0 breaking changes: + * + * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). + * + * @param {Date|Number} date - the date to check + * @returns {Boolean} the date is in the past + * @throws {TypeError} 1 argument required + * + * @example + * // If today is 6 October 2014, is 2 July 2014 in the past? + * var result = isPast(new Date(2014, 6, 2)) + * //=> true + */ +export default function isPast(dirtyDate: Date | number): boolean { + requiredArgs(1, arguments) + + return toDate(dirtyDate).getTime() < Date.now() +} diff --git a/date-fns/src/isPast/test.ts b/date-fns/src/isPast/test.ts new file mode 100644 index 0000000..eb3a947 --- /dev/null +++ b/date-fns/src/isPast/test.ts @@ -0,0 +1,42 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isPast from '.' + +describe('isPast', () => { + let clock: sinon.SinonFakeTimers + beforeEach(() => { + clock = sinon.useFakeTimers(new Date(2014, 8 /* Sep */, 25).getTime()) + }) + + afterEach(() => { + clock.restore() + }) + + it('returns true if the given date is in the past', () => { + const result = isPast(new Date(2014, 6 /* Jul */, 2)) + assert(result === true) + }) + + it('returns false if the given date is in the future', () => { + const result = isPast(new Date(2014, 11 /* Dec */, 31)) + assert(result === false) + }) + + it('returns false if the given date is now', () => { + const result = isPast(new Date(2014, 8 /* Sep */, 25)) + assert(result === false) + }) + + it('accepts a timestamp', () => { + const result = isPast(new Date(2014, 6 /* Jul */, 2).getTime()) + assert(result === true) + }) + + it('throws TypeError exception if passed less than 1 argument', () => { + // @ts-expect-error + assert.throws(isPast.bind(null), TypeError) + }) +}) |