diff options
Diffstat (limited to 'date-fns/src/lastDayOfDecade')
-rw-r--r-- | date-fns/src/lastDayOfDecade/benchmark.js | 15 | ||||
-rw-r--r-- | date-fns/src/lastDayOfDecade/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/lastDayOfDecade/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/lastDayOfDecade/index.ts | 34 | ||||
-rw-r--r-- | date-fns/src/lastDayOfDecade/test.ts | 34 |
5 files changed, 139 insertions, 0 deletions
diff --git a/date-fns/src/lastDayOfDecade/benchmark.js b/date-fns/src/lastDayOfDecade/benchmark.js new file mode 100644 index 0000000..c683eca --- /dev/null +++ b/date-fns/src/lastDayOfDecade/benchmark.js @@ -0,0 +1,15 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import lastDayOfDecade from '.' + +suite('lastDayOfYear', function () { + benchmark('date-fns', function () { + return lastDayOfDecade(this.date) + }) +}, { + setup: function () { + this.date = new Date() + } +}) diff --git a/date-fns/src/lastDayOfDecade/index.d.ts b/date-fns/src/lastDayOfDecade/index.d.ts new file mode 100644 index 0000000..05b0306 --- /dev/null +++ b/date-fns/src/lastDayOfDecade/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { lastDayOfDecade } from 'date-fns' +export default lastDayOfDecade diff --git a/date-fns/src/lastDayOfDecade/index.js.flow b/date-fns/src/lastDayOfDecade/index.js.flow new file mode 100644 index 0000000..4b7c616 --- /dev/null +++ b/date-fns/src/lastDayOfDecade/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) => Date diff --git a/date-fns/src/lastDayOfDecade/index.ts b/date-fns/src/lastDayOfDecade/index.ts new file mode 100644 index 0000000..7a50164 --- /dev/null +++ b/date-fns/src/lastDayOfDecade/index.ts @@ -0,0 +1,34 @@ +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name lastDayOfDecade + * @category Decade Helpers + * @summary Return the last day of a decade for the given date. + * + * @description + * Return the last day of a decade for the given date. + * + * ### 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 + * @returns {Date} the last day of a decade + * @throws {TypeError} 1 argument required + * + * @example + * // The last day of a decade for 21 December 2012 21:12:00: + * var result = lastDayOfDecade(new Date(2012, 11, 21, 21, 12, 00)) + * //=> Wed Dec 31 2019 00:00:00 + */ +export default function lastDayOfDecade(dirtyDate: Date | number): Date { + requiredArgs(1, arguments) + + const date = toDate(dirtyDate) + const year = date.getFullYear() + const decade = 9 + Math.floor(year / 10) * 10 + date.setFullYear(decade + 1, 0, 0) + date.setHours(0, 0, 0, 0) + return date +} diff --git a/date-fns/src/lastDayOfDecade/test.ts b/date-fns/src/lastDayOfDecade/test.ts new file mode 100644 index 0000000..75c241e --- /dev/null +++ b/date-fns/src/lastDayOfDecade/test.ts @@ -0,0 +1,34 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import lastDayOfDecade from '.' + +describe('lastDayOfDecade', function() { + it('returns the date with the time set to 00:00:00 and the date set to the last day of a decade', function() { + const date = new Date(1985, 9 /* Oct */, 20) + const result = lastDayOfDecade(date) + assert.deepEqual(result, new Date(1989, 11 /* Dec */, 31)) + }) + + it('accepts a timestamp', function() { + const date = new Date(1975, 0 /* Jan */, 19).getTime() + const result = lastDayOfDecade(date) + assert.deepEqual(result, new Date(1979, 11 /* Dec */, 31)) + }) + + it('does not mutate the original date', function() { + const date = new Date(2013, 3 /* Apr */, 23) + lastDayOfDecade(date) + assert.deepEqual(date, new Date(2013, 3 /* Apr */, 23)) + }) + + it('returns `Invalid Date` if the given date is invalid', function() { + const result = lastDayOfDecade(new Date(NaN)) + assert(result instanceof Date && isNaN(result.getTime())) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + assert.throws(lastDayOfDecade.bind(null), TypeError) + }) +}) |