summaryrefslogtreecommitdiff
path: root/date-fns/src/endOfWeek
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/endOfWeek')
-rw-r--r--date-fns/src/endOfWeek/benchmark.js21
-rw-r--r--date-fns/src/endOfWeek/index.d.ts4
-rw-r--r--date-fns/src/endOfWeek/index.js.flow58
-rw-r--r--date-fns/src/endOfWeek/index.ts67
-rw-r--r--date-fns/src/endOfWeek/test.ts144
5 files changed, 294 insertions, 0 deletions
diff --git a/date-fns/src/endOfWeek/benchmark.js b/date-fns/src/endOfWeek/benchmark.js
new file mode 100644
index 0000000..4731dc7
--- /dev/null
+++ b/date-fns/src/endOfWeek/benchmark.js
@@ -0,0 +1,21 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import endOfWeek from '.'
+import moment from 'moment'
+
+suite('endOfWeek', function () {
+ benchmark('date-fns', function () {
+ return endOfWeek(this.date)
+ })
+
+ benchmark('Moment.js', function () {
+ return this.moment.endOf('week')
+ })
+}, {
+ setup: function () {
+ this.date = new Date()
+ this.moment = moment()
+ }
+})
diff --git a/date-fns/src/endOfWeek/index.d.ts b/date-fns/src/endOfWeek/index.d.ts
new file mode 100644
index 0000000..50a77a3
--- /dev/null
+++ b/date-fns/src/endOfWeek/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { endOfWeek } from 'date-fns'
+export default endOfWeek
diff --git a/date-fns/src/endOfWeek/index.js.flow b/date-fns/src/endOfWeek/index.js.flow
new file mode 100644
index 0000000..9285b67
--- /dev/null
+++ b/date-fns/src/endOfWeek/index.js.flow
@@ -0,0 +1,58 @@
+// @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,
+ options?: {
+ locale?: Locale,
+ weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,
+ }
+) => Date
diff --git a/date-fns/src/endOfWeek/index.ts b/date-fns/src/endOfWeek/index.ts
new file mode 100644
index 0000000..f04a6f0
--- /dev/null
+++ b/date-fns/src/endOfWeek/index.ts
@@ -0,0 +1,67 @@
+import toDate from '../toDate/index'
+import toInteger from '../_lib/toInteger/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+import { LocaleOptions, WeekStartOptions } from '../types'
+
+/**
+ * @name endOfWeek
+ * @category Week Helpers
+ * @summary Return the end of a week for the given date.
+ *
+ * @description
+ * Return the end of a week for the given date.
+ * The result will be in the local timezone.
+ *
+ * ### 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 original date
+ * @param {Object} [options] - an object with options.
+ * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
+ * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
+ * @returns {Date} the end of a week
+ * @throws {TypeError} 1 argument required
+ * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
+ *
+ * @example
+ * // The end of a week for 2 September 2014 11:55:00:
+ * const result = endOfWeek(new Date(2014, 8, 2, 11, 55, 0))
+ * //=> Sat Sep 06 2014 23:59:59.999
+ *
+ * @example
+ * // If the week starts on Monday, the end of the week for 2 September 2014 11:55:00:
+ * const result = endOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 })
+ * //=> Sun Sep 07 2014 23:59:59.999
+ */
+export default function endOfWeek(
+ dirtyDate: Date | number,
+ dirtyOptions?: LocaleOptions & WeekStartOptions
+): Date {
+ requiredArgs(1, arguments)
+
+ const options = dirtyOptions || {}
+
+ const locale = options.locale
+ const localeWeekStartsOn =
+ locale && locale.options && locale.options.weekStartsOn
+ const defaultWeekStartsOn =
+ localeWeekStartsOn == null ? 0 : toInteger(localeWeekStartsOn)
+ const weekStartsOn =
+ options.weekStartsOn == null
+ ? defaultWeekStartsOn
+ : toInteger(options.weekStartsOn)
+
+ // Test if weekStartsOn is between 0 and 6 _and_ is not NaN
+ if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
+ throw new RangeError('weekStartsOn must be between 0 and 6 inclusively')
+ }
+
+ const date = toDate(dirtyDate)
+ const day = date.getDay()
+ const diff = (day < weekStartsOn ? -7 : 0) + 6 - (day - weekStartsOn)
+
+ date.setDate(date.getDate() + diff)
+ date.setHours(23, 59, 59, 999)
+ return date
+}
diff --git a/date-fns/src/endOfWeek/test.ts b/date-fns/src/endOfWeek/test.ts
new file mode 100644
index 0000000..83e1e2c
--- /dev/null
+++ b/date-fns/src/endOfWeek/test.ts
@@ -0,0 +1,144 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'assert'
+import endOfWeek from '.'
+
+describe('endOfWeek', function () {
+ it('returns the date with the time set to 23:59:59:999 and the date set to the last day of a week', function () {
+ var date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0)
+ var result = endOfWeek(date)
+ assert.deepStrictEqual(
+ result,
+ new Date(2014, 8 /* Sep */, 6, 23, 59, 59, 999)
+ )
+ })
+
+ it('allows to specify which day is the first day of the week', function () {
+ var date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0)
+ var result = endOfWeek(date, { weekStartsOn: 1 })
+ assert.deepStrictEqual(
+ result,
+ new Date(2014, 8 /* Sep */, 7, 23, 59, 59, 999)
+ )
+ })
+
+ it('allows to specify which day is the first day of the week in locale', function () {
+ var date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0)
+ var result = endOfWeek(date, {
+ // @ts-expect-error
+ locale: {
+ options: { weekStartsOn: 1 },
+ },
+ })
+ assert.deepStrictEqual(
+ result,
+ new Date(2014, 8 /* Sep */, 7, 23, 59, 59, 999)
+ )
+ })
+
+ it('`options.weekStartsOn` overwrites the first day of the week specified in locale', function () {
+ var date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0)
+ var result = endOfWeek(date, {
+ weekStartsOn: 1,
+ // @ts-expect-error
+ locale: {
+ options: { weekStartsOn: 0 },
+ },
+ })
+ assert.deepStrictEqual(
+ result,
+ new Date(2014, 8 /* Sep */, 7, 23, 59, 59, 999)
+ )
+ })
+
+ it('implicitly converts options', function () {
+ var date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0)
+ // @ts-expect-error
+ var result = endOfWeek(date, { weekStartsOn: '1' })
+ assert.deepStrictEqual(
+ result,
+ new Date(2014, 8 /* Sep */, 7, 23, 59, 59, 999)
+ )
+ })
+
+ it('accepts a timestamp', function () {
+ var date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0).getTime()
+ var result = endOfWeek(date)
+ assert.deepStrictEqual(
+ result,
+ new Date(2014, 8 /* Sep */, 6, 23, 59, 59, 999)
+ )
+ })
+
+ it('does not mutate the original date', function () {
+ var date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0)
+ endOfWeek(date)
+ assert.deepStrictEqual(date, new Date(2014, 8 /* Sep */, 2, 11, 55, 0))
+ })
+
+ describe('edge cases', function () {
+ describe('when the given day is before the start of a week', function () {
+ it('it returns the end of a week', function () {
+ var date = new Date(2014, 9 /* Oct */, 6)
+ var result = endOfWeek(date, { weekStartsOn: 3 })
+ assert.deepStrictEqual(
+ result,
+ new Date(2014, 9 /* Oct */, 7, 23, 59, 59, 999)
+ )
+ })
+ })
+
+ describe('when the given day is the start of a week', function () {
+ it('it returns the end of a week', function () {
+ var date = new Date(2014, 9 /* Oct */, 8)
+ var result = endOfWeek(date, { weekStartsOn: 3 })
+ assert.deepStrictEqual(
+ result,
+ new Date(2014, 9 /* Oct */, 14, 23, 59, 59, 999)
+ )
+ })
+ })
+
+ describe('when the given day is after the start of a week', function () {
+ it('it returns the end of a week', function () {
+ var date = new Date(2014, 9 /* Oct */, 10)
+ var result = endOfWeek(date, { weekStartsOn: 3 })
+ assert.deepStrictEqual(
+ result,
+ new Date(2014, 9 /* Oct */, 14, 23, 59, 59, 999)
+ )
+ })
+ })
+
+ it('handles the week at the end of a year', function () {
+ var date = new Date(2014, 11 /* Dec */, 29)
+ var result = endOfWeek(date, { weekStartsOn: 5 })
+ assert.deepStrictEqual(
+ result,
+ new Date(2015, 0 /* Jan */, 1, 23, 59, 59, 999)
+ )
+ })
+ })
+
+ it('returns `Invalid Date` if the given date is invalid', function () {
+ var result = endOfWeek(new Date(NaN))
+ assert(result instanceof Date && isNaN(result.getTime()))
+ })
+
+ it('throws `RangeError` if `options.weekStartsOn` is not convertable to 0, 1, ..., 6 or undefined', function () {
+ // @ts-expect-error
+ var block = endOfWeek.bind(
+ null,
+ new Date(2014, 8 /* Sep */, 2, 11, 55, 0),
+ // $ExpectedMistake
+ { weekStartsOn: NaN }
+ )
+ assert.throws(block, RangeError)
+ })
+
+ it('throws TypeError exception if passed less than 1 argument', function () {
+ // @ts-expect-error
+ assert.throws(endOfWeek.bind(null), TypeError)
+ })
+})