diff options
Diffstat (limited to 'date-fns/src/setMonth')
-rw-r--r-- | date-fns/src/setMonth/benchmark.js | 21 | ||||
-rw-r--r-- | date-fns/src/setMonth/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/setMonth/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/setMonth/index.ts | 44 | ||||
-rw-r--r-- | date-fns/src/setMonth/test.ts | 65 |
5 files changed, 186 insertions, 0 deletions
diff --git a/date-fns/src/setMonth/benchmark.js b/date-fns/src/setMonth/benchmark.js new file mode 100644 index 0000000..0113f1f --- /dev/null +++ b/date-fns/src/setMonth/benchmark.js @@ -0,0 +1,21 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import setMonth from '.' +import moment from 'moment' + +suite('setMonth', function () { + benchmark('date-fns', function () { + return setMonth(this.date, 6) + }) + + benchmark('Moment.js', function () { + return this.moment.month(6) + }) +}, { + setup: function () { + this.date = new Date() + this.moment = moment() + } +}) diff --git a/date-fns/src/setMonth/index.d.ts b/date-fns/src/setMonth/index.d.ts new file mode 100644 index 0000000..720d61c --- /dev/null +++ b/date-fns/src/setMonth/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { setMonth } from 'date-fns' +export default setMonth diff --git a/date-fns/src/setMonth/index.js.flow b/date-fns/src/setMonth/index.js.flow new file mode 100644 index 0000000..29bd4de --- /dev/null +++ b/date-fns/src/setMonth/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, month: number) => Date diff --git a/date-fns/src/setMonth/index.ts b/date-fns/src/setMonth/index.ts new file mode 100644 index 0000000..c625378 --- /dev/null +++ b/date-fns/src/setMonth/index.ts @@ -0,0 +1,44 @@ +import toInteger from '../_lib/toInteger/index' +import toDate from '../toDate/index' +import getDaysInMonth from '../getDaysInMonth/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name setMonth + * @category Month Helpers + * @summary Set the month to the given date. + * + * @description + * Set the month to the given 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} month - the month of the new date + * @returns {Date} the new date with the month set + * @throws {TypeError} 2 arguments required + * + * @example + * // Set February to 1 September 2014: + * const result = setMonth(new Date(2014, 8, 1), 1) + * //=> Sat Feb 01 2014 00:00:00 + */ +export default function setMonth(dirtyDate: Date | number, dirtyMonth: number): Date { + requiredArgs(2, arguments) + + const date = toDate(dirtyDate) + const month = toInteger(dirtyMonth) + const year = date.getFullYear() + const day = date.getDate() + + const dateWithDesiredMonth = new Date(0) + dateWithDesiredMonth.setFullYear(year, month, 15) + dateWithDesiredMonth.setHours(0, 0, 0, 0) + const daysInMonth = getDaysInMonth(dateWithDesiredMonth) + // Set the last day of the new month + // if the original date was the last day of the longer month + date.setMonth(month, Math.min(day, daysInMonth)) + return date +} diff --git a/date-fns/src/setMonth/test.ts b/date-fns/src/setMonth/test.ts new file mode 100644 index 0000000..75d61db --- /dev/null +++ b/date-fns/src/setMonth/test.ts @@ -0,0 +1,65 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import setMonth from '.' + +describe('setMonth', function() { + it('sets the month', function() { + const result = setMonth(new Date(2014, 8 /* Sep */, 1), 1) + assert.deepEqual(result, new Date(2014, 1 /* Feb */, 1)) + }) + + it('sets the last day of the month if the original date was the last day of a longer month', function() { + const result = setMonth(new Date(2014, 11 /* Dec */, 31), 1) + assert.deepEqual(result, new Date(2014, 1 /* Feb */, 28)) + }) + + it('accepts a timestamp', function() { + const result = setMonth(new Date(2014, 8 /* Sep */, 1).getTime(), 11) + assert.deepEqual(result, new Date(2014, 11 /* Dec */, 1)) + }) + + it('converts a fractional number to an integer', function() { + const result = setMonth(new Date(2014, 8 /* Sep */, 1), 1.5) + assert.deepEqual(result, new Date(2014, 1 /* Feb */, 1)) + }) + + it('implicitly converts number arguments', function() { + // @ts-expect-error + const result = setMonth(new Date(2014, 8 /* Sep */, 1), '1') + assert.deepEqual(result, new Date(2014, 1 /* Feb */, 1)) + }) + + it('does not mutate the original date', function() { + const date = new Date(2014, 8 /* Sep */, 1) + setMonth(date, 5) + assert.deepEqual(date, new Date(2014, 8 /* Sep */, 1)) + }) + + it('handles dates before 100 AD', function() { + const initialDate = new Date(0) + initialDate.setFullYear(0, 11 /* Dec */, 31) + initialDate.setHours(0, 0, 0, 0) + const expectedResult = new Date(0) + expectedResult.setFullYear(0, 1 /* Feb */, 29) + expectedResult.setHours(0, 0, 0, 0) + const result = setMonth(initialDate, 1) + assert.deepEqual(result, expectedResult) + }) + + it('returns `Invalid Date` if the given date is invalid', function() { + const result = setMonth(new Date(NaN), 1) + assert(result instanceof Date && isNaN(result.getTime())) + }) + + it('returns `Invalid Date` if the given amount is NaN', function() { + const result = setMonth(new Date(2014, 8 /* Sep */, 1), NaN) + assert(result instanceof Date && isNaN(result.getTime())) + }) + + it('throws TypeError exception if passed less than 2 arguments', function() { + assert.throws(setMonth.bind(null), TypeError) + assert.throws(setMonth.bind(null, 1), TypeError) + }) +}) |