summaryrefslogtreecommitdiff
path: root/date-fns/src/addMonths
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2021-08-23 16:46:06 -0300
committerSebastian <sebasjm@gmail.com>2021-08-23 16:48:30 -0300
commit38acabfa6089ab8ac469c12b5f55022fb96935e5 (patch)
tree453dbf70000cc5e338b06201af1eaca8343f8f73 /date-fns/src/addMonths
parentf26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff)
downloadnode-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.gz
node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.bz2
node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.zip
added web vendorsHEADmaster
Diffstat (limited to 'date-fns/src/addMonths')
-rw-r--r--date-fns/src/addMonths/benchmark.js21
-rw-r--r--date-fns/src/addMonths/index.d.ts4
-rw-r--r--date-fns/src/addMonths/index.js.flow52
-rw-r--r--date-fns/src/addMonths/index.ts74
-rw-r--r--date-fns/src/addMonths/test.ts173
5 files changed, 324 insertions, 0 deletions
diff --git a/date-fns/src/addMonths/benchmark.js b/date-fns/src/addMonths/benchmark.js
new file mode 100644
index 0000000..13f5203
--- /dev/null
+++ b/date-fns/src/addMonths/benchmark.js
@@ -0,0 +1,21 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import addMonths from '.'
+import moment from 'moment'
+
+suite('addMonths', function () {
+ benchmark('date-fns', function () {
+ return addMonths(this.date, 4)
+ })
+
+ benchmark('Moment.js', function () {
+ return this.moment.add(4, 'months')
+ })
+}, {
+ setup: function () {
+ this.date = new Date()
+ this.moment = moment()
+ }
+})
diff --git a/date-fns/src/addMonths/index.d.ts b/date-fns/src/addMonths/index.d.ts
new file mode 100644
index 0000000..1308e13
--- /dev/null
+++ b/date-fns/src/addMonths/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { addMonths } from 'date-fns'
+export default addMonths
diff --git a/date-fns/src/addMonths/index.js.flow b/date-fns/src/addMonths/index.js.flow
new file mode 100644
index 0000000..f568f0d
--- /dev/null
+++ b/date-fns/src/addMonths/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, amount: number) => Date
diff --git a/date-fns/src/addMonths/index.ts b/date-fns/src/addMonths/index.ts
new file mode 100644
index 0000000..7b34369
--- /dev/null
+++ b/date-fns/src/addMonths/index.ts
@@ -0,0 +1,74 @@
+import toInteger from '../_lib/toInteger/index'
+import toDate from '../toDate/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name addMonths
+ * @category Month Helpers
+ * @summary Add the specified number of months to the given date.
+ *
+ * @description
+ * Add the specified number of months 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} amount - the amount of months to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`.
+ * @returns {Date} the new date with the months added
+ * @throws {TypeError} 2 arguments required
+ *
+ * @example
+ * // Add 5 months to 1 September 2014:
+ * const result = addMonths(new Date(2014, 8, 1), 5)
+ * //=> Sun Feb 01 2015 00:00:00
+ */
+export default function addMonths(
+ dirtyDate: Date | number,
+ dirtyAmount: number
+): Date {
+ requiredArgs(2, arguments)
+
+ const date = toDate(dirtyDate)
+ const amount = toInteger(dirtyAmount)
+ if (isNaN(amount)) {
+ return new Date(NaN)
+ }
+ if (!amount) {
+ // If 0 months, no-op to avoid changing times in the hour before end of DST
+ return date
+ }
+ const dayOfMonth = date.getDate()
+
+ // The JS Date object supports date math by accepting out-of-bounds values for
+ // month, day, etc. For example, new Date(2020, 0, 0) returns 31 Dec 2019 and
+ // new Date(2020, 13, 1) returns 1 Feb 2021. This is *almost* the behavior we
+ // want except that dates will wrap around the end of a month, meaning that
+ // new Date(2020, 13, 31) will return 3 Mar 2021 not 28 Feb 2021 as desired. So
+ // we'll default to the end of the desired month by adding 1 to the desired
+ // month and using a date of 0 to back up one day to the end of the desired
+ // month.
+ const endOfDesiredMonth = new Date(date.getTime())
+ endOfDesiredMonth.setMonth(date.getMonth() + amount + 1, 0)
+ const daysInMonth = endOfDesiredMonth.getDate()
+ if (dayOfMonth >= daysInMonth) {
+ // If we're already at the end of the month, then this is the correct date
+ // and we're done.
+ return endOfDesiredMonth
+ } else {
+ // Otherwise, we now know that setting the original day-of-month value won't
+ // cause an overflow, so set the desired day-of-month. Note that we can't
+ // just set the date of `endOfDesiredMonth` because that object may have had
+ // its time changed in the unusual case where where a DST transition was on
+ // the last day of the month and its local time was in the hour skipped or
+ // repeated next to a DST transition. So we use `date` instead which is
+ // guaranteed to still have the original time.
+ date.setFullYear(
+ endOfDesiredMonth.getFullYear(),
+ endOfDesiredMonth.getMonth(),
+ dayOfMonth
+ )
+ return date
+ }
+}
diff --git a/date-fns/src/addMonths/test.ts b/date-fns/src/addMonths/test.ts
new file mode 100644
index 0000000..fb1afb6
--- /dev/null
+++ b/date-fns/src/addMonths/test.ts
@@ -0,0 +1,173 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'assert'
+import addMonths from '.'
+import { getDstTransitions } from '../../test/dst/tzOffsetTransitions'
+
+describe('addMonths', function() {
+ it('adds the given number of months', function() {
+ const result = addMonths(new Date(2014, 8 /* Sep */, 1), 5)
+ assert.deepStrictEqual(result, new Date(2015, 1 /* Feb */, 1))
+ })
+
+ it('accepts a timestamp', function() {
+ const result = addMonths(new Date(2014, 8 /* Sep */, 1).getTime(), 12)
+ assert.deepStrictEqual(result, new Date(2015, 8 /* Sep */, 1))
+ })
+
+ it('converts a fractional number to an integer', function() {
+ const result = addMonths(new Date(2014, 8 /* Sep */, 1), 5.75)
+ assert.deepStrictEqual(result, new Date(2015, 1 /* Feb */, 1))
+ })
+
+ it('implicitly converts number arguments', function() {
+ // @ts-expect-error
+ const result = addMonths(new Date(2014, 8 /* Sep */, 1), '5')
+ assert.deepStrictEqual(result, new Date(2015, 1 /* Feb */, 1))
+ })
+
+ it('does not mutate the original date', function() {
+ const date = new Date(2014, 8 /* Sep */, 1)
+ addMonths(date, 12)
+ assert.deepStrictEqual(date, new Date(2014, 8 /* Sep */, 1))
+ })
+
+ it('works well if the desired month has fewer days and the provided date is in the last day of a month', function() {
+ const date = new Date(2014, 11 /* Dec */, 31)
+ const result = addMonths(date, 2)
+ assert.deepStrictEqual(result, new Date(2015, 1 /* Feb */, 28))
+ })
+
+ it('handles dates before 100 AD', function() {
+ const initialDate = new Date(0)
+ initialDate.setFullYear(0, 0 /* Jan */, 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 = addMonths(initialDate, 1)
+ assert.deepStrictEqual(result, expectedResult)
+ })
+
+ it('returns `Invalid Date` if the given date is invalid', function() {
+ const result = addMonths(new Date(NaN), 5)
+ // @ts-expect-error
+ assert(result instanceof Date && isNaN(result))
+ })
+
+ it('returns `Invalid Date` if the given amount is NaN', function() {
+ const result = addMonths(new Date(2014, 8 /* Sep */, 1), NaN)
+ // @ts-expect-error
+ assert(result instanceof Date && isNaN(result))
+ })
+
+ it('throws TypeError exception if passed less than 2 arguments', function() {
+ // @ts-expect-error
+ assert.throws(addMonths.bind(null), TypeError)
+ // @ts-expect-error
+ assert.throws(addMonths.bind(null, 1), TypeError)
+ })
+
+ const dstTransitions = getDstTransitions(2017)
+ const dstOnly = dstTransitions.start && dstTransitions.end ? it : it.skip
+ const tz = Intl.DateTimeFormat().resolvedOptions().timeZone || process.env.tz
+ const HOUR = 1000 * 60 * 60
+ const override = (
+ base: Date,
+ year = base.getFullYear(),
+ month = base.getMonth(),
+ day = base.getDate(),
+ hour = base.getHours(),
+ minute = base.getMinutes()
+ ) => new Date(year, month, day, hour, minute)
+
+ dstOnly(
+ `works at DST-start boundary in local timezone: ${tz || '(unknown)'}`,
+ function() {
+ const date = dstTransitions.start
+ const result = addMonths(date!, 2)
+ assert.deepStrictEqual(
+ result,
+ override(date!, date!.getFullYear(), date!.getMonth() + 2)
+ )
+ }
+ )
+
+ dstOnly(
+ `works at DST-start - 30 mins in local timezone: ${tz || '(unknown)'}`,
+ function() {
+ const date = new Date(dstTransitions.start!.getTime() - 0.5 * HOUR)
+ const result = addMonths(date, 2)
+ const expected = override(date, date.getFullYear(), date.getMonth() + 2)
+ assert.deepStrictEqual(result, expected)
+ }
+ )
+
+ dstOnly(
+ `works at DST-start - 60 mins in local timezone: ${tz || '(unknown)'}`,
+ function() {
+ const date = new Date(dstTransitions.start!.getTime() - 1 * HOUR)
+ const result = addMonths(date, 2)
+ const expected = override(date, date.getFullYear(), date.getMonth() + 2)
+ assert.deepStrictEqual(result, expected)
+ }
+ )
+
+ dstOnly(
+ `works at DST-end boundary in local timezone: ${tz || '(unknown)'}`,
+ function() {
+ const date = dstTransitions.end
+ const result = addMonths(date!, 2)
+ assert.deepStrictEqual(
+ result,
+ override(
+ date!,
+ date!.getFullYear() + (date!.getMonth() >= 10 ? 1 : 0),
+ (date!.getMonth() + 2) % 12 // protect against wrap for Nov.
+ )
+ )
+ }
+ )
+
+ dstOnly(
+ `works at DST-end - 30 mins in local timezone: ${tz || '(unknown)'}`,
+ function() {
+ const date = new Date(dstTransitions.end!.getTime() - 0.5 * HOUR)
+ const result = addMonths(date, 2)
+ assert.deepStrictEqual(
+ result,
+ override(
+ date,
+ date.getFullYear() + (date.getMonth() >= 10 ? 1 : 0),
+ (date.getMonth() + 2) % 12 // protect against wrap for Nov.
+ )
+ )
+ }
+ )
+
+ dstOnly(
+ `works at DST-end - 60 mins in local timezone: ${tz || '(unknown)'}`,
+ function() {
+ const date = new Date(dstTransitions.end!.getTime() - 1 * HOUR)
+ const result = addMonths(date, 2)
+ assert.deepStrictEqual(
+ result,
+ override(
+ date,
+ date.getFullYear() + (date.getMonth() >= 10 ? 1 : 0),
+ (date.getMonth() + 2) % 12 // protect against wrap for Nov.
+ )
+ )
+ }
+ )
+
+ dstOnly(
+ `doesn't mutate if zero increment is used: ${tz || '(unknown)'}`,
+ function() {
+ const date = new Date(dstTransitions.end!)
+ const result = addMonths(date, 0)
+ assert.deepStrictEqual(result, date)
+ }
+ )
+})