summaryrefslogtreecommitdiff
path: root/date-fns/src/isToday
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/isToday')
-rw-r--r--date-fns/src/isToday/benchmark.js19
-rw-r--r--date-fns/src/isToday/index.d.ts4
-rw-r--r--date-fns/src/isToday/index.js.flow52
-rw-r--r--date-fns/src/isToday/index.ts33
-rw-r--r--date-fns/src/isToday/test.ts37
5 files changed, 145 insertions, 0 deletions
diff --git a/date-fns/src/isToday/benchmark.js b/date-fns/src/isToday/benchmark.js
new file mode 100644
index 0000000..e31d41a
--- /dev/null
+++ b/date-fns/src/isToday/benchmark.js
@@ -0,0 +1,19 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import isToday from '.'
+
+suite(
+ 'isToday',
+ () => {
+ benchmark('date-fns', function() {
+ return isToday(this.date)
+ })
+ },
+ {
+ setup: function() {
+ this.date = new Date()
+ }
+ }
+)
diff --git a/date-fns/src/isToday/index.d.ts b/date-fns/src/isToday/index.d.ts
new file mode 100644
index 0000000..ed0cd15
--- /dev/null
+++ b/date-fns/src/isToday/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { isToday } from 'date-fns'
+export default isToday
diff --git a/date-fns/src/isToday/index.js.flow b/date-fns/src/isToday/index.js.flow
new file mode 100644
index 0000000..d3f01ee
--- /dev/null
+++ b/date-fns/src/isToday/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/isToday/index.ts b/date-fns/src/isToday/index.ts
new file mode 100644
index 0000000..91514d1
--- /dev/null
+++ b/date-fns/src/isToday/index.ts
@@ -0,0 +1,33 @@
+import isSameDay from '../isSameDay/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name isToday
+ * @category Day Helpers
+ * @summary Is the given date today?
+ * @pure false
+ *
+ * @description
+ * Is the given date today?
+ *
+ * > ⚠️ 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 today
+ * @throws {TypeError} 1 argument required
+ *
+ * @example
+ * // If today is 6 October 2014, is 6 October 14:00:00 today?
+ * var result = isToday(new Date(2014, 9, 6, 14, 0))
+ * //=> true
+ */
+export default function isToday(dirtyDate: Date | number): boolean {
+ requiredArgs(1, arguments)
+
+ return isSameDay(dirtyDate, Date.now())
+}
diff --git a/date-fns/src/isToday/test.ts b/date-fns/src/isToday/test.ts
new file mode 100644
index 0000000..c68191f
--- /dev/null
+++ b/date-fns/src/isToday/test.ts
@@ -0,0 +1,37 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'assert'
+import sinon from 'sinon'
+import isToday from '.'
+
+describe('isToday', function() {
+ let clock: sinon.SinonFakeTimers
+ beforeEach(function() {
+ clock = sinon.useFakeTimers(new Date(2014, 8 /* Sep */, 25).getTime())
+ })
+
+ afterEach(function() {
+ clock.restore()
+ })
+
+ it('returns true if the given date is today', function() {
+ const result = isToday(new Date(2014, 8 /* Sep */, 25))
+ assert(result === true)
+ })
+
+ it('returns false if the given date is not today', function() {
+ const result = isToday(new Date(2014, 8 /* Sep */, 26))
+ assert(result === false)
+ })
+
+ it('accepts a timestamp', function() {
+ const result = isToday(new Date(2014, 8 /* Sep */, 25).getTime())
+ assert(result === true)
+ })
+
+ it('throws TypeError exception if passed less than 1 argument', () => {
+ // @ts-expect-error
+ assert.throws(isToday.bind(null), TypeError)
+ })
+})