summaryrefslogtreecommitdiff
path: root/date-fns/src/startOfISOWeek
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/startOfISOWeek')
-rw-r--r--date-fns/src/startOfISOWeek/benchmark.js21
-rw-r--r--date-fns/src/startOfISOWeek/index.d.ts4
-rw-r--r--date-fns/src/startOfISOWeek/index.js.flow52
-rw-r--r--date-fns/src/startOfISOWeek/index.ts32
-rw-r--r--date-fns/src/startOfISOWeek/test.ts35
5 files changed, 144 insertions, 0 deletions
diff --git a/date-fns/src/startOfISOWeek/benchmark.js b/date-fns/src/startOfISOWeek/benchmark.js
new file mode 100644
index 0000000..2ddb09b
--- /dev/null
+++ b/date-fns/src/startOfISOWeek/benchmark.js
@@ -0,0 +1,21 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import startOfISOWeek from '.'
+import moment from 'moment'
+
+suite('startOfISOWeek', function () {
+ benchmark('date-fns', function () {
+ return startOfISOWeek(this.date)
+ })
+
+ benchmark('Moment.js', function () {
+ return this.moment.startOf('isoWeek')
+ })
+}, {
+ setup: function () {
+ this.date = new Date()
+ this.moment = moment()
+ }
+})
diff --git a/date-fns/src/startOfISOWeek/index.d.ts b/date-fns/src/startOfISOWeek/index.d.ts
new file mode 100644
index 0000000..93e573a
--- /dev/null
+++ b/date-fns/src/startOfISOWeek/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { startOfISOWeek } from 'date-fns'
+export default startOfISOWeek
diff --git a/date-fns/src/startOfISOWeek/index.js.flow b/date-fns/src/startOfISOWeek/index.js.flow
new file mode 100644
index 0000000..4b7c616
--- /dev/null
+++ b/date-fns/src/startOfISOWeek/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/startOfISOWeek/index.ts b/date-fns/src/startOfISOWeek/index.ts
new file mode 100644
index 0000000..e4d8718
--- /dev/null
+++ b/date-fns/src/startOfISOWeek/index.ts
@@ -0,0 +1,32 @@
+import startOfWeek from '../startOfWeek/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name startOfISOWeek
+ * @category ISO Week Helpers
+ * @summary Return the start of an ISO week for the given date.
+ *
+ * @description
+ * Return the start of an ISO week for the given date.
+ * The result will be in the local timezone.
+ *
+ * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
+ *
+ * ### 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
+ * @returns {Date} the start of an ISO week
+ * @throws {TypeError} 1 argument required
+ *
+ * @example
+ * // The start of an ISO week for 2 September 2014 11:55:00:
+ * var result = startOfISOWeek(new Date(2014, 8, 2, 11, 55, 0))
+ * //=> Mon Sep 01 2014 00:00:00
+ */
+export default function startOfISOWeek(dirtyDate: Date | number): Date {
+ requiredArgs(1, arguments)
+
+ return startOfWeek(dirtyDate, { weekStartsOn: 1 })
+}
diff --git a/date-fns/src/startOfISOWeek/test.ts b/date-fns/src/startOfISOWeek/test.ts
new file mode 100644
index 0000000..6e96187
--- /dev/null
+++ b/date-fns/src/startOfISOWeek/test.ts
@@ -0,0 +1,35 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import startOfISOWeek from '.'
+
+describe('startOfISOWeek', function() {
+ it('returns the date with the time set to 00:00:00 and the date set to the first day of an ISO week', function() {
+ const date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0)
+ const result = startOfISOWeek(date)
+ assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1))
+ })
+
+ it('accepts a timestamp', function() {
+ const date = new Date(2014, 1 /* Feb */, 11, 11, 55, 0).getTime()
+ const result = startOfISOWeek(date)
+ assert.deepEqual(result, new Date(2014, 1 /* Feb */, 10))
+ })
+
+ it('does not mutate the original date', function() {
+ const date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0)
+ startOfISOWeek(date)
+ assert.deepEqual(date, new Date(2014, 8 /* Sep */, 2, 11, 55, 0))
+ })
+
+ it('returns `Invalid Date` if the given date is invalid', function() {
+ const result = startOfISOWeek(new Date(NaN))
+ //@ts-expect-error
+ assert(result instanceof Date && isNaN(result))
+ })
+
+ it('throws TypeError exception if passed less than 1 argument', function() {
+ assert.throws(startOfISOWeek.bind(null), TypeError)
+ })
+})