diff options
Diffstat (limited to 'date-fns/src/addQuarters')
-rw-r--r-- | date-fns/src/addQuarters/benchmark.js | 21 | ||||
-rw-r--r-- | date-fns/src/addQuarters/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/addQuarters/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/addQuarters/index.ts | 36 | ||||
-rw-r--r-- | date-fns/src/addQuarters/test.ts | 70 |
5 files changed, 183 insertions, 0 deletions
diff --git a/date-fns/src/addQuarters/benchmark.js b/date-fns/src/addQuarters/benchmark.js new file mode 100644 index 0000000..8e90c2f --- /dev/null +++ b/date-fns/src/addQuarters/benchmark.js @@ -0,0 +1,21 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import addQuarters from '.' +import moment from 'moment' + +suite('addQuarters', function () { + benchmark('date-fns', function () { + return addQuarters(this.date, 2) + }) + + benchmark('Moment.js', function () { + return this.moment.add(2, 'quarters') + }) +}, { + setup: function () { + this.date = new Date() + this.moment = moment() + } +}) diff --git a/date-fns/src/addQuarters/index.d.ts b/date-fns/src/addQuarters/index.d.ts new file mode 100644 index 0000000..a046e67 --- /dev/null +++ b/date-fns/src/addQuarters/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { addQuarters } from 'date-fns' +export default addQuarters diff --git a/date-fns/src/addQuarters/index.js.flow b/date-fns/src/addQuarters/index.js.flow new file mode 100644 index 0000000..f568f0d --- /dev/null +++ b/date-fns/src/addQuarters/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/addQuarters/index.ts b/date-fns/src/addQuarters/index.ts new file mode 100644 index 0000000..4eaebf0 --- /dev/null +++ b/date-fns/src/addQuarters/index.ts @@ -0,0 +1,36 @@ +import toInteger from '../_lib/toInteger/index' +import addMonths from '../addMonths/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name addQuarters + * @category Quarter Helpers + * @summary Add the specified number of year quarters to the given date. + * + * @description + * Add the specified number of year quarters to 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 date to be changed + * @param {Number} amount - the amount of quarters to be added. 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 quarters added + * @throws {TypeError} 2 arguments required + * + * @example + * // Add 1 quarter to 1 September 2014: + * const result = addQuarters(new Date(2014, 8, 1), 1) + * //=> Mon Dec 01 2014 00:00:00 + */ +export default function addQuarters( + dirtyDate: Date | number, + dirtyAmount: number +): Date { + requiredArgs(2, arguments) + + const amount = toInteger(dirtyAmount) + const months = amount * 3 + return addMonths(dirtyDate, months) +} diff --git a/date-fns/src/addQuarters/test.ts b/date-fns/src/addQuarters/test.ts new file mode 100644 index 0000000..a09434e --- /dev/null +++ b/date-fns/src/addQuarters/test.ts @@ -0,0 +1,70 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import addQuarters from '.' + +describe('addQuarters', function() { + it('adds the given number of quarters', function() { + const result = addQuarters(new Date(2014, 8 /* Sep */, 1), 1) + assert.deepStrictEqual(result, new Date(2014, 11 /* Dec */, 1)) + }) + + it('accepts a timestamp', function() { + const result = addQuarters(new Date(2014, 8 /* Sep */, 1).getTime(), 4) + assert.deepStrictEqual(result, new Date(2015, 8 /* Sep */, 1)) + }) + + it('converts a fractional number to an integer', function() { + const result = addQuarters(new Date(2014, 8 /* Sep */, 1), 1.91) + assert.deepStrictEqual(result, new Date(2014, 11 /* Dec */, 1)) + }) + + it('implicitly converts number arguments', function() { + // @ts-expect-error + const result = addQuarters(new Date(2014, 8 /* Sep */, 1), '1') + assert.deepStrictEqual(result, new Date(2014, 11 /* Dec */, 1)) + }) + + it('does not mutate the original date', function() { + const date = new Date(2014, 8 /* Sep */, 1) + addQuarters(date, 4) + assert.deepStrictEqual(date, new Date(2014, 8 /* Sep */, 1)) + }) + + it('works well if the desired month has fewer days and the provided date is in the last day of a month', function() { + const date = new Date(2014, 11 /* Dec */, 31) + const result = addQuarters(date, 3) + assert.deepStrictEqual(result, new Date(2015, 8 /* Sep */, 30)) + }) + + it('handles dates before 100 AD', function() { + const initialDate = new Date(0) + initialDate.setFullYear(-1, 10 /* Nov */, 30) + initialDate.setHours(0, 0, 0, 0) + const expectedResult = new Date(0) + expectedResult.setFullYear(0, 1 /* Feb */, 29) + expectedResult.setHours(0, 0, 0, 0) + const result = addQuarters(initialDate, 1) + assert.deepStrictEqual(result, expectedResult) + }) + + it('returns `Invalid Date` if the given date is invalid', function() { + const result = addQuarters(new Date(NaN), 1) + // @ts-expect-error + assert(result instanceof Date && isNaN(result)) + }) + + it('returns `Invalid Date` if the given amount is NaN', function() { + const result = addQuarters(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() { + // @ts-expect-error + assert.throws(addQuarters.bind(null), TypeError) + // @ts-expect-error + assert.throws(addQuarters.bind(null, 1), TypeError) + }) +}) |