diff options
Diffstat (limited to 'date-fns/src/subISOWeekYears')
-rw-r--r-- | date-fns/src/subISOWeekYears/benchmark.js | 15 | ||||
-rw-r--r-- | date-fns/src/subISOWeekYears/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/subISOWeekYears/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/subISOWeekYears/index.ts | 39 | ||||
-rw-r--r-- | date-fns/src/subISOWeekYears/test.ts | 62 |
5 files changed, 172 insertions, 0 deletions
diff --git a/date-fns/src/subISOWeekYears/benchmark.js b/date-fns/src/subISOWeekYears/benchmark.js new file mode 100644 index 0000000..f8b615c --- /dev/null +++ b/date-fns/src/subISOWeekYears/benchmark.js @@ -0,0 +1,15 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import subISOWeekYears from '.' + +suite('subISOWeekYears', function () { + benchmark('date-fns', function () { + return subISOWeekYears(this.date, 7) + }) +}, { + setup: function () { + this.date = new Date() + } +}) diff --git a/date-fns/src/subISOWeekYears/index.d.ts b/date-fns/src/subISOWeekYears/index.d.ts new file mode 100644 index 0000000..78018ea --- /dev/null +++ b/date-fns/src/subISOWeekYears/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { subISOWeekYears } from 'date-fns' +export default subISOWeekYears diff --git a/date-fns/src/subISOWeekYears/index.js.flow b/date-fns/src/subISOWeekYears/index.js.flow new file mode 100644 index 0000000..f568f0d --- /dev/null +++ b/date-fns/src/subISOWeekYears/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, amount: number) => Date diff --git a/date-fns/src/subISOWeekYears/index.ts b/date-fns/src/subISOWeekYears/index.ts new file mode 100644 index 0000000..bbd396c --- /dev/null +++ b/date-fns/src/subISOWeekYears/index.ts @@ -0,0 +1,39 @@ +import toInteger from '../_lib/toInteger/index' +import addISOWeekYears from '../addISOWeekYears/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name subISOWeekYears + * @category ISO Week-Numbering Year Helpers + * @summary Subtract the specified number of ISO week-numbering years from the given date. + * + * @description + * Subtract the specified number of ISO week-numbering years from the given date. + * + * 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 `subISOYears` to `subISOWeekYears`. + * "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., `setWeekYear`. + * + * @param {Date|Number} date - the date to be changed + * @param {Number} amount - the amount of ISO week-numbering years to be subtracted. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`. + * @returns {Date} the new date with the ISO week-numbering years subtracted + * @throws {TypeError} 2 arguments required + * + * @example + * // Subtract 5 ISO week-numbering years from 1 September 2014: + * const result = subISOWeekYears(new Date(2014, 8, 1), 5) + * //=> Mon Aug 31 2009 00:00:00 + */ +export default function subISOWeekYears(dirtyDate: Date | number, dirtyAmount: number) { + requiredArgs(2, arguments) + + const amount = toInteger(dirtyAmount) + return addISOWeekYears(dirtyDate, -amount) +} diff --git a/date-fns/src/subISOWeekYears/test.ts b/date-fns/src/subISOWeekYears/test.ts new file mode 100644 index 0000000..dbb4213 --- /dev/null +++ b/date-fns/src/subISOWeekYears/test.ts @@ -0,0 +1,62 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import subISOWeekYears from '.' + +describe('subISOWeekYears', function() { + it('subtracts the given number of ISO week-numbering years', function() { + const result = subISOWeekYears(new Date(2014, 8 /* Sep */, 1), 5) + assert.deepEqual(result, new Date(2009, 7 /* Aug */, 31)) + }) + + it('accepts a timestamp', function() { + const result = subISOWeekYears(new Date(2014, 8 /* Sep */, 1).getTime(), 12) + assert.deepEqual(result, new Date(2002, 8 /* Sep */, 2)) + }) + + it('converts a fractional number to an integer', function() { + const result = subISOWeekYears(new Date(2014, 8 /* Sep */, 1), 5.555) + assert.deepEqual(result, new Date(2009, 7 /* Aug */, 31)) + }) + + it('implicitly converts number arguments', function() { + // $ExpectedMistake + const result = subISOWeekYears(new Date(2014, 8 /* Sep */, 1), '5') + assert.deepEqual(result, new Date(2009, 7 /* Aug */, 31)) + }) + + it('does not mutate the original date', function() { + const date = new Date(2014, 8 /* Sep */, 1) + subISOWeekYears(date, 12) + assert.deepEqual(date, new Date(2014, 8 /* Sep */, 1)) + }) + + it('handles dates before 100 AD', function() { + const initialDate = new Date(0) + initialDate.setFullYear(15, 5 /* Jun */, 26) + initialDate.setHours(0, 0, 0, 0) + const expectedResult = new Date(0) + expectedResult.setFullYear(10, 6 /* Jul */, 2) + expectedResult.setHours(0, 0, 0, 0) + const result = subISOWeekYears(initialDate, 5) + assert.deepEqual(result, expectedResult) + }) + + it('returns `Invalid Date` if the given date is invalid', function() { + const result = subISOWeekYears(new Date(NaN), 5) + // @ts-expect-error + assert(result instanceof Date && isNaN(result)) + }) + + it('returns `Invalid Date` if the given amount is NaN', function() { + const result = subISOWeekYears(new Date(2014, 8 /* Sep */, 1), NaN) + // @ts-expect-error + assert(result instanceof Date && isNaN(result)) + }) + + it('throws TypeError exception if passed less than 2 arguments', function() { + assert.throws(subISOWeekYears.bind(null), TypeError) + assert.throws(subISOWeekYears.bind(null, 1), TypeError) + }) +}) |