diff options
Diffstat (limited to 'date-fns/src/eachHourOfInterval')
-rw-r--r-- | date-fns/src/eachHourOfInterval/benchmark.js | 20 | ||||
-rw-r--r-- | date-fns/src/eachHourOfInterval/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/eachHourOfInterval/index.js.flow | 57 | ||||
-rw-r--r-- | date-fns/src/eachHourOfInterval/index.ts | 69 | ||||
-rw-r--r-- | date-fns/src/eachHourOfInterval/test.ts | 134 |
5 files changed, 284 insertions, 0 deletions
diff --git a/date-fns/src/eachHourOfInterval/benchmark.js b/date-fns/src/eachHourOfInterval/benchmark.js new file mode 100644 index 0000000..c96c557 --- /dev/null +++ b/date-fns/src/eachHourOfInterval/benchmark.js @@ -0,0 +1,20 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import eachHourOfInterval from '.' + +suite( + 'eachHourOfInterval', + function() { + benchmark('date-fns', function() { + return eachHourOfInterval({ 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/eachHourOfInterval/index.d.ts b/date-fns/src/eachHourOfInterval/index.d.ts new file mode 100644 index 0000000..4c3addd --- /dev/null +++ b/date-fns/src/eachHourOfInterval/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { eachHourOfInterval } from 'date-fns' +export default eachHourOfInterval diff --git a/date-fns/src/eachHourOfInterval/index.js.flow b/date-fns/src/eachHourOfInterval/index.js.flow new file mode 100644 index 0000000..5a9b8e4 --- /dev/null +++ b/date-fns/src/eachHourOfInterval/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/eachHourOfInterval/index.ts b/date-fns/src/eachHourOfInterval/index.ts new file mode 100644 index 0000000..27ad6dc --- /dev/null +++ b/date-fns/src/eachHourOfInterval/index.ts @@ -0,0 +1,69 @@ +import addHours from '../addHours/index' +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' +import { Interval, StepOptions } from '../types' + +/** + * @name eachHourOfInterval + * @category Interval Helpers + * @summary Return the array of hours within the specified time interval. + * + * @description + * Return the array of hours within the specified time interval. + * + * @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 hours from the hour of the interval start to the hour 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 hour between 6 October 2014, 12:00 and 6 October 2014, 15:00 + * var result = eachHourOfInterval({ + * start: new Date(2014, 9, 6, 12), + * end: new Date(2014, 9, 6, 15) + * }) + * //=> [ + * // Mon Oct 06 2014 12:00:00, + * // Mon Oct 06 2014 13:00:00, + * // Mon Oct 06 2014 14:00:00, + * // Mon Oct 06 2014 15:00:00 + * // ] + */ +export default function eachHourOfInterval( + dirtyInterval: Interval, + options?: StepOptions +): Date[] { + requiredArgs(1, arguments) + + const interval = dirtyInterval || {} + const startDate = toDate(interval.start) + const endDate = toDate(interval.end) + + const startTime = startDate.getTime() + const endTime = endDate.getTime() + + // Throw an exception if start date is after end date or if any date is `Invalid Date` + if (!(startTime <= endTime)) { + throw new RangeError('Invalid interval') + } + + const dates = [] + + let currentDate = startDate + currentDate.setMinutes(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 = addHours(currentDate, step) + } + + return dates +} diff --git a/date-fns/src/eachHourOfInterval/test.ts b/date-fns/src/eachHourOfInterval/test.ts new file mode 100644 index 0000000..4bdaa05 --- /dev/null +++ b/date-fns/src/eachHourOfInterval/test.ts @@ -0,0 +1,134 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import eachHourOfInterval from '.' + +describe('eachHourOfInterval', () => { + it('returns an array with starts of hours from the hour of the start date to the hour of the end date', () => { + const result = eachHourOfInterval({ + start: new Date(2014, 9 /* Oct */, 6, 12), + end: new Date(2014, 9 /* Oct */, 6, 15) + }) + assert.deepEqual(result, [ + new Date(2014, 9 /* Oct */, 6, 12), + new Date(2014, 9 /* Oct */, 6, 13), + new Date(2014, 9 /* Oct */, 6, 14), + new Date(2014, 9 /* Oct */, 6, 15) + ]) + }) + + it('accepts timestamps', () => { + const result = eachHourOfInterval({ + start: new Date(2014, 9 /* Oct */, 6, 12).getTime(), + end: new Date(2014, 9 /* Oct */, 6, 15).getTime() + }) + assert.deepEqual(result, [ + new Date(2014, 9 /* Oct */, 6, 12), + new Date(2014, 9 /* Oct */, 6, 13), + new Date(2014, 9 /* Oct */, 6, 14), + new Date(2014, 9 /* Oct */, 6, 15) + ]) + }) + + it('handles the hours that are not starts of hours', () => { + const result = eachHourOfInterval({ + start: new Date(2014, 9 /* Oct */, 6, 12, 59, 59, 999), + end: new Date(2014, 9 /* Oct */, 6, 15, 59, 59, 999) + }) + assert.deepEqual(result, [ + new Date(2014, 9 /* Oct */, 6, 12), + new Date(2014, 9 /* Oct */, 6, 13), + new Date(2014, 9 /* Oct */, 6, 14), + new Date(2014, 9 /* Oct */, 6, 15) + ]) + }) + + it('returns one hour if the both arguments are on the same hour', () => { + const result = eachHourOfInterval({ + start: new Date(2014, 9 /* Oct */, 6, 12, 35), + end: new Date(2014, 9 /* Oct */, 6, 12, 47) + }) + assert.deepEqual(result, [new Date(2014, 9 /* Oct */, 6, 12)]) + }) + + it('returns one hour if the both arguments are the same', () => { + const result = eachHourOfInterval({ + start: new Date(2014, 9 /* Oct */, 6, 12, 35), + end: new Date(2014, 9 /* Oct */, 6, 12, 35) + }) + assert.deepEqual(result, [new Date(2014, 9 /* Oct */, 6, 12)]) + }) + + it('throws an exception if the start date is after the end date', () => { + const block = eachHourOfInterval.bind(null, { + start: new Date(2014, 9 /* Oct */, 12, 35, 0, 0, 1), + end: new Date(2014, 9 /* Oct */, 12, 35, 0, 0, 0) + }) + assert.throws(block, RangeError) + }) + + it('throws an exception if the start date is `Invalid Date`', () => { + const block = eachHourOfInterval.bind(null, { + start: new Date(NaN), + end: new Date(2014, 9 /* Oct */, 6, 12) + }) + assert.throws(block, RangeError) + }) + + it('throws an exception if the end date is `Invalid Date`', () => { + const block = eachHourOfInterval.bind(null, { + start: new Date(2014, 9 /* Oct */, 12, 12), + end: new Date(NaN) + }) + assert.throws(block, RangeError) + }) + + it('throws an exception if the interval is undefined', () => { + const block = eachHourOfInterval.bind( + null, + // $ExpectedMistake + undefined + ) + assert.throws(block, RangeError) + }) + + it('throws TypeError exception if passed less than 1 argument', () => { + assert.throws(eachHourOfInterval.bind(null), TypeError) + }) + + describe('options.step', () => { + const interval = { + start: new Date(2014, 9 /* Oct */, 6, 12), + end: new Date(2014, 9 /* Oct */, 6, 18) + } + + const stepError = /^RangeError: `options.step` must be a number greater than 1$/ + + it('returns an array with starts of hours from the hour of the start date to the hour of the end date with the given step', () => { + const result = eachHourOfInterval(interval, { step: 3 }) + assert.deepEqual(result, [ + new Date(2014, 9 /* Oct */, 6, 12), + new Date(2014, 9 /* Oct */, 6, 15), + new Date(2014, 9 /* Oct */, 6, 18) + ]) + }) + + it('throws TypeError error if `options.step` is less than 1', () => { + assert.throws(() => eachHourOfInterval(interval, { step: 0 }), stepError) + assert.throws(() => eachHourOfInterval(interval, { step: -3 }), stepError) + }) + + it('throws TypeError error if `options.step` is NaN', () => { + // $ExpectedMistake + assert.throws( + () => eachHourOfInterval(interval, { step: 'w' }), + stepError + ) + assert.throws( + () => eachHourOfInterval(interval, { step: NaN }), + stepError + ) + }) + }) +}) |