diff options
Diffstat (limited to 'date-fns/src/isBefore')
-rw-r--r-- | date-fns/src/isBefore/benchmark.js | 23 | ||||
-rw-r--r-- | date-fns/src/isBefore/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/isBefore/index.js.flow | 55 | ||||
-rw-r--r-- | date-fns/src/isBefore/index.ts | 32 | ||||
-rw-r--r-- | date-fns/src/isBefore/test.ts | 59 |
5 files changed, 173 insertions, 0 deletions
diff --git a/date-fns/src/isBefore/benchmark.js b/date-fns/src/isBefore/benchmark.js new file mode 100644 index 0000000..d70f2e8 --- /dev/null +++ b/date-fns/src/isBefore/benchmark.js @@ -0,0 +1,23 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import isBefore from '.' +import moment from 'moment' + +suite('isBefore', function () { + benchmark('date-fns', function () { + return isBefore(this.dateA, this.dateB) + }) + + benchmark('Moment.js', function () { + return this.momentA.isBefore(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/isBefore/index.d.ts b/date-fns/src/isBefore/index.d.ts new file mode 100644 index 0000000..127ed7e --- /dev/null +++ b/date-fns/src/isBefore/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isBefore } from 'date-fns' +export default isBefore diff --git a/date-fns/src/isBefore/index.js.flow b/date-fns/src/isBefore/index.js.flow new file mode 100644 index 0000000..3cb525c --- /dev/null +++ b/date-fns/src/isBefore/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: ( + date: Date | number, + dateToCompare: Date | number +) => boolean diff --git a/date-fns/src/isBefore/index.ts b/date-fns/src/isBefore/index.ts new file mode 100644 index 0000000..344b887 --- /dev/null +++ b/date-fns/src/isBefore/index.ts @@ -0,0 +1,32 @@ +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name isBefore + * @category Common Helpers + * @summary Is the first date before the second one? + * + * @description + * Is the first date before the second one? + * + * ### 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 that should be before the other one to return true + * @param {Date|Number} dateToCompare - the date to compare with + * @returns {Boolean} the first date is before the second date + * @throws {TypeError} 2 arguments required + * + * @example + * // Is 10 July 1989 before 11 February 1987? + * var result = isBefore(new Date(1989, 6, 10), new Date(1987, 1, 11)) + * //=> false + */ +export default function isBefore(dirtyDate: Date | number, dirtyDateToCompare: Date | number): boolean { + requiredArgs(2, arguments) + + const date = toDate(dirtyDate) + const dateToCompare = toDate(dirtyDateToCompare) + return date.getTime() < dateToCompare.getTime() +} diff --git a/date-fns/src/isBefore/test.ts b/date-fns/src/isBefore/test.ts new file mode 100644 index 0000000..c4d0084 --- /dev/null +++ b/date-fns/src/isBefore/test.ts @@ -0,0 +1,59 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import isBefore from '.' + +describe('isBefore', function() { + it('returns true if the first date is before the second one', function() { + const result = isBefore( + new Date(1987, 1 /* Feb */, 11), + new Date(1989, 6 /* Jul */, 10) + ) + assert(result === true) + }) + + it('returns false if the first date is after the second one', function() { + const result = isBefore( + new Date(1989, 6 /* Jul */, 10), + new Date(1987, 1 /* Feb */, 11) + ) + assert(result === false) + }) + + it('returns false if the first date is equal to the second one', function() { + const result = isBefore( + new Date(1989, 6 /* Jul */, 10), + new Date(1989, 6 /* Jul */, 10) + ) + assert(result === false) + }) + + it('accepts a timestamp', function() { + const result = isBefore( + new Date(1987, 1 /* Feb */, 11).getTime(), + new Date(1989, 6 /* Jul */, 10).getTime() + ) + assert(result === true) + }) + + it('returns false if the first date is `Invalid Date`', function() { + const result = isBefore(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 = isBefore(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 = isBefore(new Date(NaN), new Date(NaN)) + assert(result === false) + }) + + it('throws TypeError exception if passed less than 2 arguments', function() { + assert.throws(isBefore.bind(null), TypeError) + assert.throws(isBefore.bind(null, 1), TypeError) + }) +}) |