diff options
Diffstat (limited to 'date-fns/src/isExists')
-rw-r--r-- | date-fns/src/isExists/benchmark.js | 21 | ||||
-rw-r--r-- | date-fns/src/isExists/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/isExists/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/isExists/index.ts | 38 | ||||
-rw-r--r-- | date-fns/src/isExists/test.ts | 21 |
5 files changed, 136 insertions, 0 deletions
diff --git a/date-fns/src/isExists/benchmark.js b/date-fns/src/isExists/benchmark.js new file mode 100644 index 0000000..167c979 --- /dev/null +++ b/date-fns/src/isExists/benchmark.js @@ -0,0 +1,21 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import isExists from '.' + +suite( + 'isExisting', + function() { + benchmark('date-fns', function() { + return isExists(this.invalidYear, this.invalidMonth, this.invalidDay) + }) + }, + { + setup: function() { + this.invalidYear = 2018 + this.invalidMonth = 1 + this.invalidDay = 31 + } + } +) diff --git a/date-fns/src/isExists/index.d.ts b/date-fns/src/isExists/index.d.ts new file mode 100644 index 0000000..af919be --- /dev/null +++ b/date-fns/src/isExists/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isExists } from 'date-fns' +export default isExists diff --git a/date-fns/src/isExists/index.js.flow b/date-fns/src/isExists/index.js.flow new file mode 100644 index 0000000..810828e --- /dev/null +++ b/date-fns/src/isExists/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: (year: number, month: number, day: number) => boolean diff --git a/date-fns/src/isExists/index.ts b/date-fns/src/isExists/index.ts new file mode 100644 index 0000000..a15e60a --- /dev/null +++ b/date-fns/src/isExists/index.ts @@ -0,0 +1,38 @@ +/** + * @name isExists + * @category Common Helpers + * @summary Is the given date exists? + * + * @description + * Checks if the given arguments convert to an existing date. + * + * @param {Number} year of the date to check + * @param {Number} month of the date to check + * @param {Number} day of the date to check + * @returns {Boolean} the date exists + * @throws {TypeError} 3 arguments required + * + * @example + * // For the valid date: + * var result = isExists(2018, 0, 31) + * //=> true + * + * @example + * // For the invalid date: + * var result = isExists(2018, 1, 31) + * //=> false + */ +export default function isExists(year: number, month: number, day: number): boolean { + if (arguments.length < 3) { + throw new TypeError( + '3 argument required, but only ' + arguments.length + ' present' + ) + } + + const date = new Date(year, month, day) + return ( + date.getFullYear() === year && + date.getMonth() === month && + date.getDate() === day + ) +} diff --git a/date-fns/src/isExists/test.ts b/date-fns/src/isExists/test.ts new file mode 100644 index 0000000..3df2470 --- /dev/null +++ b/date-fns/src/isExists/test.ts @@ -0,0 +1,21 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import isExists from '.' + +describe('isValid', function() { + it('returns true if the given date is valid', function() { + const result = isExists(2018, 0, 31) + assert(result === true) + }) + + it('returns false if the given date is invalid', function() { + const result = isExists(2018, 1 /* Feb */, 31) + assert(result === false) + }) + + it('throws TypeError exception if passed less than 3 arguments', function() { + assert.throws(isExists.bind(null), TypeError) + }) +}) |