summaryrefslogtreecommitdiff
path: root/date-fns/src/nextSunday
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/nextSunday')
-rw-r--r--date-fns/src/nextSunday/index.d.ts4
-rw-r--r--date-fns/src/nextSunday/index.js.flow52
-rw-r--r--date-fns/src/nextSunday/index.ts25
-rw-r--r--date-fns/src/nextSunday/test.ts48
4 files changed, 129 insertions, 0 deletions
diff --git a/date-fns/src/nextSunday/index.d.ts b/date-fns/src/nextSunday/index.d.ts
new file mode 100644
index 0000000..e0a7965
--- /dev/null
+++ b/date-fns/src/nextSunday/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { nextSunday } from 'date-fns'
+export default nextSunday
diff --git a/date-fns/src/nextSunday/index.js.flow b/date-fns/src/nextSunday/index.js.flow
new file mode 100644
index 0000000..4b7c616
--- /dev/null
+++ b/date-fns/src/nextSunday/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) => Date
diff --git a/date-fns/src/nextSunday/index.ts b/date-fns/src/nextSunday/index.ts
new file mode 100644
index 0000000..5320ac3
--- /dev/null
+++ b/date-fns/src/nextSunday/index.ts
@@ -0,0 +1,25 @@
+import requiredArgs from '../_lib/requiredArgs/index'
+import nextDay from '../nextDay/index'
+import toDate from '../toDate/index'
+
+/**
+ * @name nextSunday
+ * @category Weekday Helpers
+ * @summary When is the next Sunday?
+ *
+ * @description
+ * When is the next Sunday?
+ *
+ * @param {Date | number} date - the date to start counting from
+ * @returns {Date} the next Sunday
+ * @throws {TypeError} 1 argument required
+ *
+ * @example
+ * // When is the next Sunday after Mar, 22, 2020?
+ * const result = nextSunday(new Date(2020, 2, 22))
+ * //=> Sun Mar 29 2020 00:00:00
+ */
+export default function nextSunday(date: Date | number): Date {
+ requiredArgs(1, arguments)
+ return nextDay(toDate(date), 0)
+}
diff --git a/date-fns/src/nextSunday/test.ts b/date-fns/src/nextSunday/test.ts
new file mode 100644
index 0000000..468c53b
--- /dev/null
+++ b/date-fns/src/nextSunday/test.ts
@@ -0,0 +1,48 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import nextSunday from '.'
+
+describe('nextSunday', () => {
+ it('returns the following Sunday given various dates before the same', () => {
+ assert.deepStrictEqual(
+ nextSunday(new Date(2020, 2 /* Mar */, 23)),
+ new Date(2020, 2 /* Mar */, 29)
+ )
+
+ assert.deepStrictEqual(
+ nextSunday(new Date(2020, 2 /* Mar */, 22)),
+ new Date(2020, 2 /* Mar */, 29)
+ )
+
+ assert.deepStrictEqual(
+ nextSunday(new Date(2020, 2 /* Mar */, 21)),
+ new Date(2020, 2 /* Mar */, 22)
+ )
+
+ assert.deepStrictEqual(
+ nextSunday(new Date(2020, 2 /* Mar */, 20)),
+ new Date(2020, 2 /* Mar */, 22)
+ )
+
+ assert.deepStrictEqual(
+ nextSunday(new Date(2020, 2 /* Mar */, 19)),
+ new Date(2020, 2 /* Mar */, 22)
+ )
+
+ assert.deepStrictEqual(
+ nextSunday(new Date(2020, 2 /* Mar */, 18)),
+ new Date(2020, 2 /* Mar */, 22)
+ )
+
+ assert.deepStrictEqual(
+ nextSunday(new Date(2020, 2 /* Mar */, 17)),
+ new Date(2020, 2 /* Mar */, 22)
+ )
+ })
+
+ it('returns `Invalid Date` if the given date is invalid', () => {
+ assert(nextSunday(new Date(NaN)) instanceof Date)
+ })
+})