diff options
Diffstat (limited to 'date-fns/src/isThisMinute')
-rw-r--r-- | date-fns/src/isThisMinute/benchmark.js | 20 | ||||
-rw-r--r-- | date-fns/src/isThisMinute/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/isThisMinute/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/isThisMinute/index.ts | 35 | ||||
-rw-r--r-- | date-fns/src/isThisMinute/test.ts | 39 |
5 files changed, 150 insertions, 0 deletions
diff --git a/date-fns/src/isThisMinute/benchmark.js b/date-fns/src/isThisMinute/benchmark.js new file mode 100644 index 0000000..9801f3a --- /dev/null +++ b/date-fns/src/isThisMinute/benchmark.js @@ -0,0 +1,20 @@ +// @flow +/* eslint-env mocha */ + +/* global suite, benchmark */ + +import isThisMinute from '.' + +suite( + 'isThisMinute', + () => { + benchmark('date-fns', function() { + return isThisMinute(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/date-fns/src/isThisMinute/index.d.ts b/date-fns/src/isThisMinute/index.d.ts new file mode 100644 index 0000000..ca1430d --- /dev/null +++ b/date-fns/src/isThisMinute/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isThisMinute } from 'date-fns' +export default isThisMinute diff --git a/date-fns/src/isThisMinute/index.js.flow b/date-fns/src/isThisMinute/index.js.flow new file mode 100644 index 0000000..d3f01ee --- /dev/null +++ b/date-fns/src/isThisMinute/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/isThisMinute/index.ts b/date-fns/src/isThisMinute/index.ts new file mode 100644 index 0000000..0606945 --- /dev/null +++ b/date-fns/src/isThisMinute/index.ts @@ -0,0 +1,35 @@ +import isSameMinute from '../isSameMinute/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name isThisMinute + * @category Minute Helpers + * @summary Is the given date in the same minute as the current date? + * @pure false + * + * @description + * Is the given date in the same minute as the current date? + * + * > ⚠️ 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 this minute + * @throws {TypeError} 1 argument required + * + * @example + * // If now is 25 September 2014 18:30:15.500, + * // is 25 September 2014 18:30:00 in this minute? + * var result = isThisMinute(new Date(2014, 8, 25, 18, 30)) + * //=> true + */ + +export default function isThisMinute(dirtyDate: Date | number): boolean { + requiredArgs(1, arguments) + + return isSameMinute(Date.now(), dirtyDate) +} diff --git a/date-fns/src/isThisMinute/test.ts b/date-fns/src/isThisMinute/test.ts new file mode 100644 index 0000000..e395020 --- /dev/null +++ b/date-fns/src/isThisMinute/test.ts @@ -0,0 +1,39 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isThisMinute from '.' + +describe('isThisMinute', () => { + let clock: sinon.SinonFakeTimers + beforeEach(() => { + clock = sinon.useFakeTimers( + new Date(2014, 8 /* Sep */, 25, 18, 30, 15, 500).getTime() + ) + }) + + afterEach(() => { + clock.restore() + }) + + it('returns true if the given date and the current date have the same minute', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18, 30) + assert(isThisMinute(date) === true) + }) + + it('returns false if the given date and the current date have different minutes', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18, 31) + assert(isThisMinute(date) === false) + }) + + it('accepts a timestamp', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18, 30, 30).getTime() + assert(isThisMinute(date) === true) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + // @ts-expect-error + assert.throws(isThisMinute.bind(null), TypeError) + }) +}) |