diff options
Diffstat (limited to 'date-fns/src/lastDayOfISOWeekYear')
-rw-r--r-- | date-fns/src/lastDayOfISOWeekYear/benchmark.js | 15 | ||||
-rw-r--r-- | date-fns/src/lastDayOfISOWeekYear/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/lastDayOfISOWeekYear/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/lastDayOfISOWeekYear/index.ts | 45 | ||||
-rw-r--r-- | date-fns/src/lastDayOfISOWeekYear/test.ts | 45 |
5 files changed, 161 insertions, 0 deletions
diff --git a/date-fns/src/lastDayOfISOWeekYear/benchmark.js b/date-fns/src/lastDayOfISOWeekYear/benchmark.js new file mode 100644 index 0000000..9623d55 --- /dev/null +++ b/date-fns/src/lastDayOfISOWeekYear/benchmark.js @@ -0,0 +1,15 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import lastDayOfISOWeekYear from '.' + +suite('lastDayOfISOWeekYear', function () { + benchmark('date-fns', function () { + return lastDayOfISOWeekYear(this.date) + }) +}, { + setup: function () { + this.date = new Date() + } +}) diff --git a/date-fns/src/lastDayOfISOWeekYear/index.d.ts b/date-fns/src/lastDayOfISOWeekYear/index.d.ts new file mode 100644 index 0000000..b30859a --- /dev/null +++ b/date-fns/src/lastDayOfISOWeekYear/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { lastDayOfISOWeekYear } from 'date-fns' +export default lastDayOfISOWeekYear diff --git a/date-fns/src/lastDayOfISOWeekYear/index.js.flow b/date-fns/src/lastDayOfISOWeekYear/index.js.flow new file mode 100644 index 0000000..4b7c616 --- /dev/null +++ b/date-fns/src/lastDayOfISOWeekYear/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/lastDayOfISOWeekYear/index.ts b/date-fns/src/lastDayOfISOWeekYear/index.ts new file mode 100644 index 0000000..425095f --- /dev/null +++ b/date-fns/src/lastDayOfISOWeekYear/index.ts @@ -0,0 +1,45 @@ +import getISOWeekYear from '../getISOWeekYear/index' +import startOfISOWeek from '../startOfISOWeek/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name lastDayOfISOWeekYear + * @category ISO Week-Numbering Year Helpers + * @summary Return the last day of an ISO week-numbering year for the given date. + * + * @description + * Return the last day of an ISO week-numbering year, + * which always starts 3 days before the year's first Thursday. + * The result will be in the local timezone. + * + * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_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). + * + * - The function was renamed from `lastDayOfISOYear` to `lastDayOfISOWeekYear`. + * "ISO week year" is short for [ISO week-numbering year](https://en.wikipedia.org/wiki/ISO_week_date). + * This change makes the name consistent with + * locale-dependent week-numbering year helpers, e.g., `getWeekYear`. + * + * @param {Date|Number} date - the original date + * @returns {Date} the end of an ISO week-numbering year + * @throws {TypeError} 1 argument required + * + * @example + * // The last day of an ISO week-numbering year for 2 July 2005: + * var result = lastDayOfISOWeekYear(new Date(2005, 6, 2)) + * //=> Sun Jan 01 2006 00:00:00 + */ +export default function lastDayOfISOWeekYear(dirtyDate: Date | number): Date { + requiredArgs(1, arguments) + + const year = getISOWeekYear(dirtyDate) + const fourthOfJanuary = new Date(0) + fourthOfJanuary.setFullYear(year + 1, 0, 4) + fourthOfJanuary.setHours(0, 0, 0, 0) + const date = startOfISOWeek(fourthOfJanuary) + date.setDate(date.getDate() - 1) + return date +} diff --git a/date-fns/src/lastDayOfISOWeekYear/test.ts b/date-fns/src/lastDayOfISOWeekYear/test.ts new file mode 100644 index 0000000..9a06b16 --- /dev/null +++ b/date-fns/src/lastDayOfISOWeekYear/test.ts @@ -0,0 +1,45 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import lastDayOfISOWeekYear from '.' + +describe('lastDayOfISOWeekYear', function() { + it('returns the date with the time set to 00:00:00 and the date set to the last day of an ISO year', function() { + const result = lastDayOfISOWeekYear(new Date(2009, 0 /* Jan */, 1, 16, 0)) + assert.deepEqual(result, new Date(2010, 0 /* Jan */, 3)) + }) + + it('accepts a timestamp', function() { + const result = lastDayOfISOWeekYear( + new Date(2005, 0 /* Jan */, 1, 6, 0).getTime() + ) + assert.deepEqual(result, new Date(2005, 0 /* Jan */, 2)) + }) + + it('does not mutate the original date', function() { + const date = new Date(2014, 6 /* Jul */, 2) + lastDayOfISOWeekYear(date) + assert.deepEqual(date, new Date(2014, 6 /* Jul */, 2)) + }) + + it('handles dates before 100 AD', function() { + const initialDate = new Date(0) + initialDate.setFullYear(5, 0 /* Jan */, 4) + initialDate.setHours(0, 0, 0, 0) + const expectedResult = new Date(0) + expectedResult.setFullYear(6, 0 /* Jan */, 1) + expectedResult.setHours(0, 0, 0, 0) + const result = lastDayOfISOWeekYear(initialDate) + assert.deepEqual(result, expectedResult) + }) + + it('returns `Invalid Date` if the given date is invalid', function() { + const result = lastDayOfISOWeekYear(new Date(NaN)) + assert(result instanceof Date && isNaN(result.getTime())) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + assert.throws(lastDayOfISOWeekYear.bind(null), TypeError) + }) +}) |