summaryrefslogtreecommitdiff
path: root/date-fns/src/setISOWeek
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/setISOWeek')
-rw-r--r--date-fns/src/setISOWeek/benchmark.js21
-rw-r--r--date-fns/src/setISOWeek/index.d.ts4
-rw-r--r--date-fns/src/setISOWeek/index.js.flow52
-rw-r--r--date-fns/src/setISOWeek/index.ts38
-rw-r--r--date-fns/src/setISOWeek/test.ts60
5 files changed, 175 insertions, 0 deletions
diff --git a/date-fns/src/setISOWeek/benchmark.js b/date-fns/src/setISOWeek/benchmark.js
new file mode 100644
index 0000000..27409fc
--- /dev/null
+++ b/date-fns/src/setISOWeek/benchmark.js
@@ -0,0 +1,21 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import setISOWeek from '.'
+import moment from 'moment'
+
+suite('setISOWeek', function () {
+ benchmark('date-fns', function () {
+ return setISOWeek(this.date, 10)
+ })
+
+ benchmark('Moment.js', function () {
+ return this.moment.isoWeek(10)
+ })
+}, {
+ setup: function () {
+ this.date = new Date()
+ this.moment = moment()
+ }
+})
diff --git a/date-fns/src/setISOWeek/index.d.ts b/date-fns/src/setISOWeek/index.d.ts
new file mode 100644
index 0000000..a2e7970
--- /dev/null
+++ b/date-fns/src/setISOWeek/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { setISOWeek } from 'date-fns'
+export default setISOWeek
diff --git a/date-fns/src/setISOWeek/index.js.flow b/date-fns/src/setISOWeek/index.js.flow
new file mode 100644
index 0000000..cf1ccb8
--- /dev/null
+++ b/date-fns/src/setISOWeek/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, isoWeek: number) => Date
diff --git a/date-fns/src/setISOWeek/index.ts b/date-fns/src/setISOWeek/index.ts
new file mode 100644
index 0000000..82170f9
--- /dev/null
+++ b/date-fns/src/setISOWeek/index.ts
@@ -0,0 +1,38 @@
+import toInteger from '../_lib/toInteger/index'
+import toDate from '../toDate/index'
+import getISOWeek from '../getISOWeek/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name setISOWeek
+ * @category ISO Week Helpers
+ * @summary Set the ISO week to the given date.
+ *
+ * @description
+ * Set the ISO week to the given date, saving the weekday number.
+ *
+ * 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 date to be changed
+ * @param {Number} isoWeek - the ISO week of the new date
+ * @returns {Date} the new date with the ISO week set
+ * @throws {TypeError} 2 arguments required
+ *
+ * @example
+ * // Set the 53rd ISO week to 7 August 2004:
+ * const result = setISOWeek(new Date(2004, 7, 7), 53)
+ * //=> Sat Jan 01 2005 00:00:00
+ */
+export default function setISOWeek(dirtyDate: Date | number, dirtyISOWeek: number): Date {
+ requiredArgs(2, arguments)
+
+ const date = toDate(dirtyDate)
+ const isoWeek = toInteger(dirtyISOWeek)
+ const diff = getISOWeek(date) - isoWeek
+ date.setDate(date.getDate() - diff * 7)
+ return date
+}
diff --git a/date-fns/src/setISOWeek/test.ts b/date-fns/src/setISOWeek/test.ts
new file mode 100644
index 0000000..fe481e0
--- /dev/null
+++ b/date-fns/src/setISOWeek/test.ts
@@ -0,0 +1,60 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import setISOWeek from '.'
+
+describe('setISOWeek', function() {
+ it('sets the ISO week', function() {
+ const result = setISOWeek(new Date(2004, 7 /* Aug */, 7), 53)
+ assert.deepEqual(result, new Date(2005, 0 /* Jan */, 1))
+ })
+
+ it('accepts a timestamp', function() {
+ const result = setISOWeek(new Date(2009, 11 /* Dec */, 2).getTime(), 1)
+ assert.deepEqual(result, new Date(2008, 11 /* Dec */, 31))
+ })
+
+ it('converts a fractional number to an integer', function() {
+ const result = setISOWeek(new Date(2004, 7 /* Aug */, 7), 53.53)
+ assert.deepEqual(result, new Date(2005, 0 /* Jan */, 1))
+ })
+
+ it('implicitly converts number arguments', function() {
+ // @ts-expect-error
+ const result = setISOWeek(new Date(2004, 7 /* Aug */, 7), '53')
+ assert.deepEqual(result, new Date(2005, 0 /* Jan */, 1))
+ })
+
+ it('does not mutate the original date', function() {
+ var date = new Date(2014, 6 /* Jul */, 2)
+ setISOWeek(date, 52)
+ assert.deepEqual(date, new Date(2014, 6 /* Jul */, 2))
+ })
+
+ it('handles dates before 100 AD', function() {
+ const initialDate = new Date(0)
+ initialDate.setFullYear(4, 0 /* Jan */, 4)
+ initialDate.setHours(0, 0, 0, 0)
+ const expectedResult = new Date(0)
+ expectedResult.setFullYear(4, 11 /* Dec */, 26)
+ expectedResult.setHours(0, 0, 0, 0)
+ const result = setISOWeek(initialDate, 52)
+ assert.deepEqual(result, expectedResult)
+ })
+
+ it('returns `Invalid Date` if the given date is invalid', function() {
+ const result = setISOWeek(new Date(NaN), 53)
+ assert(result instanceof Date && isNaN(result.getTime()))
+ })
+
+ it('returns `Invalid Date` if the given amount is NaN', function() {
+ const result = setISOWeek(new Date(2004, 7 /* Aug */, 7), NaN)
+ assert(result instanceof Date && isNaN(result.getTime()))
+ })
+
+ it('throws TypeError exception if passed less than 2 arguments', function() {
+ assert.throws(setISOWeek.bind(null), TypeError)
+ assert.throws(setISOWeek.bind(null, 1), TypeError)
+ })
+})