diff options
Diffstat (limited to 'date-fns/src/eachDayOfInterval')
-rw-r--r-- | date-fns/src/eachDayOfInterval/benchmark.js | 16 | ||||
-rw-r--r-- | date-fns/src/eachDayOfInterval/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/eachDayOfInterval/index.js.flow | 57 | ||||
-rw-r--r-- | date-fns/src/eachDayOfInterval/index.ts | 98 | ||||
-rw-r--r-- | date-fns/src/eachDayOfInterval/test.ts | 137 |
5 files changed, 312 insertions, 0 deletions
diff --git a/date-fns/src/eachDayOfInterval/benchmark.js b/date-fns/src/eachDayOfInterval/benchmark.js new file mode 100644 index 0000000..45d8e44 --- /dev/null +++ b/date-fns/src/eachDayOfInterval/benchmark.js @@ -0,0 +1,16 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import eachDayOfInterval from '.' + +suite('eachDayOfInterval', function () { + benchmark('date-fns', function () { + return eachDayOfInterval({start: this.dateA, end: this.dateB}) + }) +}, { + setup: function () { + this.dateA = new Date() + this.dateB = new Date(this.dateA.getTime() + 604800000) + } +}) diff --git a/date-fns/src/eachDayOfInterval/index.d.ts b/date-fns/src/eachDayOfInterval/index.d.ts new file mode 100644 index 0000000..80fc0dc --- /dev/null +++ b/date-fns/src/eachDayOfInterval/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { eachDayOfInterval } from 'date-fns' +export default eachDayOfInterval diff --git a/date-fns/src/eachDayOfInterval/index.js.flow b/date-fns/src/eachDayOfInterval/index.js.flow new file mode 100644 index 0000000..5a9b8e4 --- /dev/null +++ b/date-fns/src/eachDayOfInterval/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: ( + interval: Interval, + options?: { + step?: number, + } +) => Date[] diff --git a/date-fns/src/eachDayOfInterval/index.ts b/date-fns/src/eachDayOfInterval/index.ts new file mode 100644 index 0000000..fded968 --- /dev/null +++ b/date-fns/src/eachDayOfInterval/index.ts @@ -0,0 +1,98 @@ +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name eachDayOfInterval + * @category Interval Helpers + * @summary Return the array of dates within the specified time interval. + * + * @description + * Return the array of dates within the specified time interval. + * + * ### 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 `eachDay` to `eachDayOfInterval`. + * This change was made to mirror the use of the word "interval" in standard ISO 8601:2004 terminology: + * + * ``` + * 2.1.3 + * time interval + * part of the time axis limited by two instants + * ``` + * + * Also, this function now accepts an object with `start` and `end` properties + * instead of two arguments as an interval. + * This function now throws `RangeError` if the start of the interval is after its end + * or if any date in the interval is `Invalid Date`. + * + * ```javascript + * // Before v2.0.0 + * + * eachDay(new Date(2014, 0, 10), new Date(2014, 0, 20)) + * + * // v2.0.0 onward + * + * eachDayOfInterval( + * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) } + * ) + * ``` + * + * @param {Interval} interval - the interval. See [Interval]{@link https://date-fns.org/docs/Interval} + * @param {Object} [options] - an object with options. + * @param {Number} [options.step=1] - the step to increment by. The value should be more than 1. + * @returns {Date[]} the array with starts of days from the day of the interval start to the day of the interval end + * @throws {TypeError} 1 argument required + * @throws {RangeError} `options.step` must be a number greater than 1 + * @throws {RangeError} The start of an interval cannot be after its end + * @throws {RangeError} Date in interval cannot be `Invalid Date` + * + * @example + * // Each day between 6 October 2014 and 10 October 2014: + * const result = eachDayOfInterval({ + * start: new Date(2014, 9, 6), + * end: new Date(2014, 9, 10) + * }) + * //=> [ + * // Mon Oct 06 2014 00:00:00, + * // Tue Oct 07 2014 00:00:00, + * // Wed Oct 08 2014 00:00:00, + * // Thu Oct 09 2014 00:00:00, + * // Fri Oct 10 2014 00:00:00 + * // ] + */ +export default function eachDayOfInterval( + dirtyInterval: Interval, + options?: { step?: number } +): Date[] { + requiredArgs(1, arguments) + + const interval = dirtyInterval || {} + const startDate = toDate(interval.start) + const endDate = toDate(interval.end) + + const endTime = endDate.getTime() + + // Throw an exception if start date is after end date or if any date is `Invalid Date` + if (!(startDate.getTime() <= endTime)) { + throw new RangeError('Invalid interval') + } + + const dates = [] + + const currentDate = startDate + currentDate.setHours(0, 0, 0, 0) + + const step = options && 'step' in options ? Number(options.step) : 1 + if (step < 1 || isNaN(step)) + throw new RangeError('`options.step` must be a number greater than 1') + + while (currentDate.getTime() <= endTime) { + dates.push(toDate(currentDate)) + currentDate.setDate(currentDate.getDate() + step) + currentDate.setHours(0, 0, 0, 0) + } + + return dates +} diff --git a/date-fns/src/eachDayOfInterval/test.ts b/date-fns/src/eachDayOfInterval/test.ts new file mode 100644 index 0000000..2ace8e1 --- /dev/null +++ b/date-fns/src/eachDayOfInterval/test.ts @@ -0,0 +1,137 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import eachDayOfInterval from '.' + +describe('eachDayOfInterval', () => { + it('returns an array with starts of days from the day of the start date to the day of the end date', () => { + const result = eachDayOfInterval({ + start: new Date(2014, 9 /* Oct */, 6), + end: new Date(2014, 9 /* Oct */, 12) + }) + assert.deepStrictEqual(result, [ + new Date(2014, 9 /* Oct */, 6), + new Date(2014, 9 /* Oct */, 7), + new Date(2014, 9 /* Oct */, 8), + new Date(2014, 9 /* Oct */, 9), + new Date(2014, 9 /* Oct */, 10), + new Date(2014, 9 /* Oct */, 11), + new Date(2014, 9 /* Oct */, 12) + ]) + }) + + it('accepts timestamps', () => { + const result = eachDayOfInterval({ + start: new Date(2014, 9 /* Oct */, 6).getTime(), + end: new Date(2014, 9 /* Oct */, 12).getTime() + }) + assert.deepStrictEqual(result, [ + new Date(2014, 9 /* Oct */, 6), + new Date(2014, 9 /* Oct */, 7), + new Date(2014, 9 /* Oct */, 8), + new Date(2014, 9 /* Oct */, 9), + new Date(2014, 9 /* Oct */, 10), + new Date(2014, 9 /* Oct */, 11), + new Date(2014, 9 /* Oct */, 12) + ]) + }) + + it('handles the dates that are not starts of days', () => { + const result = eachDayOfInterval({ + start: new Date(2014, 9 /* Oct */, 6, 6, 35), + end: new Date(2014, 9 /* Oct */, 12, 22, 15) + }) + assert.deepStrictEqual(result, [ + new Date(2014, 9 /* Oct */, 6), + new Date(2014, 9 /* Oct */, 7), + new Date(2014, 9 /* Oct */, 8), + new Date(2014, 9 /* Oct */, 9), + new Date(2014, 9 /* Oct */, 10), + new Date(2014, 9 /* Oct */, 11), + new Date(2014, 9 /* Oct */, 12) + ]) + }) + + it('returns one day if the both arguments are on the same day', () => { + const result = eachDayOfInterval({ + start: new Date(2014, 9 /* Oct */, 6, 14), + end: new Date(2014, 9 /* Oct */, 6, 15) + }) + assert.deepStrictEqual(result, [new Date(2014, 9 /* Oct */, 6)]) + }) + + it('returns one day if the both arguments are the same', () => { + const result = eachDayOfInterval({ + start: new Date(2014, 9 /* Oct */, 6, 14), + end: new Date(2014, 9 /* Oct */, 6, 14) + }) + assert.deepStrictEqual(result, [new Date(2014, 9 /* Oct */, 6)]) + }) + + it('throws an exception if the start date is after the end date', () => { + const block = eachDayOfInterval.bind(null, { + start: new Date(2014, 9 /* Oct */, 12), + end: new Date(2014, 9 /* Oct */, 6) + }) + assert.throws(block, RangeError) + }) + + it('throws an exception if the start date is `Invalid Date`', () => { + var block = eachDayOfInterval.bind(null, { + start: new Date(NaN), + end: new Date(2014, 9 /* Oct */, 6) + }) + assert.throws(block, RangeError) + }) + + it('throws an exception if the end date is `Invalid Date`', () => { + var block = eachDayOfInterval.bind(null, { + start: new Date(2014, 9 /* Oct */, 12), + end: new Date(NaN) + }) + assert.throws(block, RangeError) + }) + + it('throws an exception if the interval is undefined', () => { + var block = eachDayOfInterval.bind( + null, + //@ts-expect-error + undefined + ) + assert.throws(block, RangeError) + }) + + it('throws TypeError exception if passed less than 1 argument', () => { + assert.throws(eachDayOfInterval.bind(null), TypeError) + }) + + describe('options.step', () => { + const interval = { + start: new Date(2014, 9 /* Oct */, 6), + end: new Date(2014, 9 /* Oct */, 13) + } + + const stepError = /^RangeError: `options.step` must be a number greater than 1$/ + + it('returns an array with starts of days from the day of the start date to the day of the end date with the given step', () => { + const result = eachDayOfInterval(interval, { step: 3 }) + assert.deepStrictEqual(result, [ + new Date(2014, 9 /* Oct */, 6), + new Date(2014, 9 /* Oct */, 9), + new Date(2014, 9 /* Oct */, 12) + ]) + }) + + it('throws TypeError error if `options.step` is less than 1', () => { + assert.throws(() => eachDayOfInterval(interval, { step: 0 }), stepError) + assert.throws(() => eachDayOfInterval(interval, { step: -3 }), stepError) + }) + + it('throws TypeError error if `options.step` is NaN', () => { + //@ts-expect-error + assert.throws(() => eachDayOfInterval(interval, { step: 'w' }), stepError) + assert.throws(() => eachDayOfInterval(interval, { step: NaN }), stepError) + }) + }) +}) |