diff options
Diffstat (limited to 'date-fns/src/lastDayOfQuarter')
-rw-r--r-- | date-fns/src/lastDayOfQuarter/benchmark.js | 15 | ||||
-rw-r--r-- | date-fns/src/lastDayOfQuarter/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/lastDayOfQuarter/index.js.flow | 57 | ||||
-rw-r--r-- | date-fns/src/lastDayOfQuarter/index.ts | 38 | ||||
-rw-r--r-- | date-fns/src/lastDayOfQuarter/test.ts | 34 |
5 files changed, 148 insertions, 0 deletions
diff --git a/date-fns/src/lastDayOfQuarter/benchmark.js b/date-fns/src/lastDayOfQuarter/benchmark.js new file mode 100644 index 0000000..72d5780 --- /dev/null +++ b/date-fns/src/lastDayOfQuarter/benchmark.js @@ -0,0 +1,15 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import lastDayOfQuarter from '.' + +suite('lastDayOfQuarter', function () { + benchmark('date-fns', function () { + return lastDayOfQuarter(this.date) + }) +}, { + setup: function () { + this.date = new Date() + } +}) diff --git a/date-fns/src/lastDayOfQuarter/index.d.ts b/date-fns/src/lastDayOfQuarter/index.d.ts new file mode 100644 index 0000000..3387b64 --- /dev/null +++ b/date-fns/src/lastDayOfQuarter/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { lastDayOfQuarter } from 'date-fns' +export default lastDayOfQuarter diff --git a/date-fns/src/lastDayOfQuarter/index.js.flow b/date-fns/src/lastDayOfQuarter/index.js.flow new file mode 100644 index 0000000..364a3bb --- /dev/null +++ b/date-fns/src/lastDayOfQuarter/index.js.flow @@ -0,0 +1,57 @@ +// @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, + options?: { + additionalDigits?: 0 | 1 | 2, + } +) => Date diff --git a/date-fns/src/lastDayOfQuarter/index.ts b/date-fns/src/lastDayOfQuarter/index.ts new file mode 100644 index 0000000..578cf12 --- /dev/null +++ b/date-fns/src/lastDayOfQuarter/index.ts @@ -0,0 +1,38 @@ +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name lastDayOfQuarter + * @category Quarter Helpers + * @summary Return the last day of a year quarter for the given date. + * + * @description + * Return the last day of a year quarter for the given date. + * The result will be in the local timezone. + * + * ### 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 original date + * @param {Object} [options] - an object with options. + * @param {0|1|2} [options.additionalDigits=2] - passed to `toDate`. See [toDate]{@link https://date-fns.org/docs/toDate} + * @returns {Date} the last day of a quarter + * @throws {TypeError} 1 argument required + * @throws {RangeError} `options.additionalDigits` must be 0, 1 or 2 + * + * @example + * // The last day of a quarter for 2 September 2014 11:55:00: + * var result = lastDayOfQuarter(new Date(2014, 8, 2, 11, 55, 0)) + * //=> Tue Sep 30 2014 00:00:00 + */ +export default function lastDayOfQuarter(dirtyDate: Date | number): Date { + requiredArgs(1, arguments) + + const date = toDate(dirtyDate) + const currentMonth = date.getMonth() + const month = currentMonth - (currentMonth % 3) + 3 + date.setMonth(month, 0) + date.setHours(0, 0, 0, 0) + return date +} diff --git a/date-fns/src/lastDayOfQuarter/test.ts b/date-fns/src/lastDayOfQuarter/test.ts new file mode 100644 index 0000000..0812953 --- /dev/null +++ b/date-fns/src/lastDayOfQuarter/test.ts @@ -0,0 +1,34 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import lastDayOfQuarter from '.' + +describe('lastDayOfQuarter', function() { + it('returns the date with the time set to 00:00:00 and the date set to the last day of a quarter', function() { + const date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0) + const result = lastDayOfQuarter(date) + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 30)) + }) + + it('accepts a timestamp', function() { + const date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0).getTime() + const result = lastDayOfQuarter(date) + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 30)) + }) + + it('does not mutate the original date', function() { + const date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0) + lastDayOfQuarter(date) + assert.deepEqual(date, new Date(2014, 8 /* Sep */, 2, 11, 55, 0)) + }) + + it('returns `Invalid Date` if the given date is invalid', function() { + const result = lastDayOfQuarter(new Date(NaN)) + assert(result instanceof Date && isNaN(result.getTime())) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + assert.throws(lastDayOfQuarter.bind(null), TypeError) + }) +}) |