diff options
Diffstat (limited to 'date-fns/src/isEqual')
-rw-r--r-- | date-fns/src/isEqual/benchmark.js | 23 | ||||
-rw-r--r-- | date-fns/src/isEqual/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/isEqual/index.js.flow | 55 | ||||
-rw-r--r-- | date-fns/src/isEqual/index.ts | 35 | ||||
-rw-r--r-- | date-fns/src/isEqual/test.ts | 51 |
5 files changed, 168 insertions, 0 deletions
diff --git a/date-fns/src/isEqual/benchmark.js b/date-fns/src/isEqual/benchmark.js new file mode 100644 index 0000000..c2276b6 --- /dev/null +++ b/date-fns/src/isEqual/benchmark.js @@ -0,0 +1,23 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import isEqual from '.' +import moment from 'moment' + +suite('isEqual', function () { + benchmark('date-fns', function () { + return isEqual(this.dateA, this.dateB) + }) + + benchmark('Moment.js', function () { + return this.momentA.isSame(this.momentB) + }) +}, { + setup: function () { + this.dateA = new Date() + this.momentA = moment() + this.dateB = new Date(this.dateA.getTime() + 604800000) + this.momentB = this.momentA.clone().add(7, 'days') + } +}) diff --git a/date-fns/src/isEqual/index.d.ts b/date-fns/src/isEqual/index.d.ts new file mode 100644 index 0000000..87865db --- /dev/null +++ b/date-fns/src/isEqual/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isEqual } from 'date-fns' +export default isEqual diff --git a/date-fns/src/isEqual/index.js.flow b/date-fns/src/isEqual/index.js.flow new file mode 100644 index 0000000..55ea578 --- /dev/null +++ b/date-fns/src/isEqual/index.js.flow @@ -0,0 +1,55 @@ +// @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: ( + dateLeft: Date | number, + dateRight: Date | number +) => boolean diff --git a/date-fns/src/isEqual/index.ts b/date-fns/src/isEqual/index.ts new file mode 100644 index 0000000..1157e62 --- /dev/null +++ b/date-fns/src/isEqual/index.ts @@ -0,0 +1,35 @@ +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name isEqual + * @category Common Helpers + * @summary Are the given dates equal? + * + * @description + * Are the given dates equal? + * + * ### 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} dateLeft - the first date to compare + * @param {Date|Number} dateRight - the second date to compare + * @returns {Boolean} the dates are equal + * @throws {TypeError} 2 arguments required + * + * @example + * // Are 2 July 2014 06:30:45.000 and 2 July 2014 06:30:45.500 equal? + * var result = isEqual( + * new Date(2014, 6, 2, 6, 30, 45, 0), + * new Date(2014, 6, 2, 6, 30, 45, 500) + * ) + * //=> false + */ +export default function isEqual(dirtyLeftDate: Date | number, dirtyRightDate: Date | number): boolean { + requiredArgs(2, arguments) + + const dateLeft = toDate(dirtyLeftDate) + const dateRight = toDate(dirtyRightDate) + return dateLeft.getTime() === dateRight.getTime() +} diff --git a/date-fns/src/isEqual/test.ts b/date-fns/src/isEqual/test.ts new file mode 100644 index 0000000..111ae4c --- /dev/null +++ b/date-fns/src/isEqual/test.ts @@ -0,0 +1,51 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import isEqual from '.' + +describe('isEqual', function() { + it('returns true if the given dates are equal', function() { + const result = isEqual( + new Date(1987, 1 /* Feb */, 11), + new Date(1987, 1 /* Feb */, 11) + ) + assert(result === true) + }) + + it('returns false if the given dates are not equal', function() { + const result = isEqual( + new Date(1989, 6 /* Jul */, 10), + new Date(1987, 1 /* Feb */, 11) + ) + assert(result === false) + }) + + it('accepts a timestamp', function() { + const result = isEqual( + new Date(1987, 1 /* Feb */, 11).getTime(), + new Date(1987, 1 /* Feb */, 11).getTime() + ) + assert(result === true) + }) + + it('returns false if the first date is `Invalid Date`', function() { + const result = isEqual(new Date(NaN), new Date(1989, 6 /* Jul */, 10)) + assert(result === false) + }) + + it('returns false if the second date is `Invalid Date`', function() { + const result = isEqual(new Date(1987, 1 /* Feb */, 11), new Date(NaN)) + assert(result === false) + }) + + it('returns false if the both dates are `Invalid Date`', function() { + const result = isEqual(new Date(NaN), new Date(NaN)) + assert(result === false) + }) + + it('throws TypeError exception if passed less than 2 arguments', function() { + assert.throws(isEqual.bind(null), TypeError) + assert.throws(isEqual.bind(null, 1), TypeError) + }) +}) |