summaryrefslogtreecommitdiff
path: root/date-fns/src/eachWeekendOfInterval
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/eachWeekendOfInterval')
-rwxr-xr-xdate-fns/src/eachWeekendOfInterval/benchmark.js16
-rw-r--r--date-fns/src/eachWeekendOfInterval/index.d.ts4
-rwxr-xr-xdate-fns/src/eachWeekendOfInterval/index.js.flow52
-rw-r--r--date-fns/src/eachWeekendOfInterval/index.ts47
-rw-r--r--date-fns/src/eachWeekendOfInterval/test.ts67
5 files changed, 186 insertions, 0 deletions
diff --git a/date-fns/src/eachWeekendOfInterval/benchmark.js b/date-fns/src/eachWeekendOfInterval/benchmark.js
new file mode 100755
index 0000000..df23caf
--- /dev/null
+++ b/date-fns/src/eachWeekendOfInterval/benchmark.js
@@ -0,0 +1,16 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import eachWeekendOfInterval from '.'
+
+suite('eachWeekendOfInterval', function () {
+ benchmark('date-fns', function () {
+ return eachWeekendOfInterval({start: this.dateStart, end: this.dateEnd})
+ })
+}, {
+ setup: function () {
+ this.dateStart = new Date(2022, 0, 1)
+ this.dateEnd = new Date(2022, 11, 31)
+ }
+})
diff --git a/date-fns/src/eachWeekendOfInterval/index.d.ts b/date-fns/src/eachWeekendOfInterval/index.d.ts
new file mode 100644
index 0000000..c0b3fea
--- /dev/null
+++ b/date-fns/src/eachWeekendOfInterval/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { eachWeekendOfInterval } from 'date-fns'
+export default eachWeekendOfInterval
diff --git a/date-fns/src/eachWeekendOfInterval/index.js.flow b/date-fns/src/eachWeekendOfInterval/index.js.flow
new file mode 100755
index 0000000..30230a5
--- /dev/null
+++ b/date-fns/src/eachWeekendOfInterval/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: (interval: Interval) => Date[]
diff --git a/date-fns/src/eachWeekendOfInterval/index.ts b/date-fns/src/eachWeekendOfInterval/index.ts
new file mode 100644
index 0000000..ffd2873
--- /dev/null
+++ b/date-fns/src/eachWeekendOfInterval/index.ts
@@ -0,0 +1,47 @@
+import eachDayOfInterval from '../eachDayOfInterval/index'
+import isSunday from '../isSunday/index'
+import isWeekend from '../isWeekend/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name eachWeekendOfInterval
+ * @category Interval Helpers
+ * @summary List all the Saturdays and Sundays in the given date interval.
+ *
+ * @description
+ * Get all the Saturdays and Sundays in the given date interval.
+ *
+ * @param {Interval} interval - the given interval. See [Interval]{@link https://date-fns.org/docs/Interval}
+ * @returns {Date[]} an array containing all the Saturdays and Sundays
+ * @throws {TypeError} 1 argument required
+ * @throws {RangeError} The start of an interval cannot be after its end
+ * @throws {RangeError} Date in interval cannot be `Invalid Date`
+ *
+ * @example
+ * // Lists all Saturdays and Sundays in the given date interval
+ * const result = eachWeekendOfInterval({
+ * start: new Date(2018, 8, 17),
+ * end: new Date(2018, 8, 30)
+ * })
+ * //=> [
+ * // Sat Sep 22 2018 00:00:00,
+ * // Sun Sep 23 2018 00:00:00,
+ * // Sat Sep 29 2018 00:00:00,
+ * // Sun Sep 30 2018 00:00:00
+ * // ]
+ */
+export default function eachWeekendOfInterval(interval: Interval): Date[] {
+ requiredArgs(1, arguments)
+
+ const dateInterval = eachDayOfInterval(interval)
+ const weekends = []
+ let index = 0
+ while (index < dateInterval.length) {
+ const date = dateInterval[index++]
+ if (isWeekend(date)) {
+ weekends.push(date)
+ if (isSunday(date)) index = index + 5
+ }
+ }
+ return weekends
+}
diff --git a/date-fns/src/eachWeekendOfInterval/test.ts b/date-fns/src/eachWeekendOfInterval/test.ts
new file mode 100644
index 0000000..75df655
--- /dev/null
+++ b/date-fns/src/eachWeekendOfInterval/test.ts
@@ -0,0 +1,67 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'assert'
+import eachWeekendOfInterval from '.'
+
+describe('eachWeekendOfInterval', function () {
+ it('returns all weekends within the interval', function () {
+ const result = eachWeekendOfInterval({
+ start: new Date(2018, 8 /* Sept */, 17),
+ end: new Date(2018, 8 /* Sept */, 30)
+ })
+ assert.deepEqual(result, [
+ new Date(2018, 8 /* Sept */, 22),
+ new Date(2018, 8 /* Sept */, 23),
+ new Date(2018, 8 /* Sept */, 29),
+ new Date(2018, 8 /* Sept */, 30)
+ ])
+ })
+
+ it('returns all weekends within the interval when starting on a weekend', function () {
+ const result = eachWeekendOfInterval({
+ start: new Date(2018, 8 /* Sept */, 22),
+ end: new Date(2018, 8 /* Sept */, 30)
+ })
+ assert.deepEqual(result, [
+ new Date(2018, 8 /* Sept */, 22),
+ new Date(2018, 8 /* Sept */, 23),
+ new Date(2018, 8 /* Sept */, 29),
+ new Date(2018, 8 /* Sept */, 30)
+ ])
+ })
+
+ it('throws `RangeError` invalid interval start date is used', function () {
+ // $ExpectedMistake
+ const block = eachWeekendOfInterval.bind(null, {
+ start: new Date(NaN),
+ end: new Date(2019, 11 /* Dec */, 31)
+ })
+ assert.throws(block, RangeError)
+ })
+
+ it('throws `RangeError` invalid interval end date is used', function () {
+ // $ExpectedMistake
+ const block = eachWeekendOfInterval.bind(null, {
+ start: new Date(2019, 0 /* Jan */, 1),
+ end: new Date(NaN)
+ })
+ assert.throws(block, RangeError)
+ })
+
+ it('throws TypeError exception if passed less than 1 argument', function () {
+ assert.throws(eachWeekendOfInterval, TypeError)
+ })
+
+ it('throws `RangeError` if start of an interval is after its end', function () {
+ const block = eachWeekendOfInterval.bind(
+ null,
+ // $ExpectedMistake
+ {
+ start: new Date(2018, 8 /* Sept */, 25),
+ end: new Date(2018, 8 /* Sept */, 6)
+ }
+ )
+ assert.throws(block, RangeError)
+ })
+})