summaryrefslogtreecommitdiff
path: root/date-fns/src/subDays
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/subDays
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/subDays')
-rw-r--r--date-fns/src/subDays/benchmark.js21
-rw-r--r--date-fns/src/subDays/index.d.ts4
-rw-r--r--date-fns/src/subDays/index.js.flow52
-rw-r--r--date-fns/src/subDays/index.ts32
-rw-r--r--date-fns/src/subDays/test.ts52
5 files changed, 161 insertions, 0 deletions
diff --git a/date-fns/src/subDays/benchmark.js b/date-fns/src/subDays/benchmark.js
new file mode 100644
index 0000000..032cd9a
--- /dev/null
+++ b/date-fns/src/subDays/benchmark.js
@@ -0,0 +1,21 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import subDays from '.'
+import moment from 'moment'
+
+suite('subDays', function () {
+ benchmark('date-fns', function () {
+ return subDays(this.date, 14)
+ })
+
+ benchmark('Moment.js', function () {
+ return this.moment.subtract(14, 'days')
+ })
+}, {
+ setup: function () {
+ this.date = new Date()
+ this.moment = moment()
+ }
+})
diff --git a/date-fns/src/subDays/index.d.ts b/date-fns/src/subDays/index.d.ts
new file mode 100644
index 0000000..0b2fe65
--- /dev/null
+++ b/date-fns/src/subDays/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { subDays } from 'date-fns'
+export default subDays
diff --git a/date-fns/src/subDays/index.js.flow b/date-fns/src/subDays/index.js.flow
new file mode 100644
index 0000000..f568f0d
--- /dev/null
+++ b/date-fns/src/subDays/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/subDays/index.ts b/date-fns/src/subDays/index.ts
new file mode 100644
index 0000000..7c706c3
--- /dev/null
+++ b/date-fns/src/subDays/index.ts
@@ -0,0 +1,32 @@
+import toInteger from '../_lib/toInteger/index'
+import addDays from '../addDays/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name subDays
+ * @category Day Helpers
+ * @summary Subtract the specified number of days from the given date.
+ *
+ * @description
+ * Subtract the specified number of days from 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 days to be subtracted. 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 days subtracted
+ * @throws {TypeError} 2 arguments required
+ *
+ * @example
+ * // Subtract 10 days from 1 September 2014:
+ * const result = subDays(new Date(2014, 8, 1), 10)
+ * //=> Fri Aug 22 2014 00:00:00
+ */
+export default function subDays(dirtyDate: Date | number, dirtyAmount: number) {
+ requiredArgs(2, arguments)
+
+ const amount = toInteger(dirtyAmount)
+ return addDays(dirtyDate, -amount)
+}
diff --git a/date-fns/src/subDays/test.ts b/date-fns/src/subDays/test.ts
new file mode 100644
index 0000000..fadc101
--- /dev/null
+++ b/date-fns/src/subDays/test.ts
@@ -0,0 +1,52 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import subDays from '.'
+
+describe('subDays', function() {
+ it('subtracts the given number of days', function() {
+ const result = subDays(new Date(2014, 8 /* Sep */, 1), 10)
+ assert.deepEqual(result, new Date(2014, 7 /* Aug */, 22))
+ })
+
+ it('accepts a timestamp', function() {
+ const result = subDays(new Date(2014, 8 /* Sep */, 1).getTime(), 10)
+ assert.deepEqual(result, new Date(2014, 7 /* Aug */, 22))
+ })
+
+ it('converts a fractional number to an integer', function() {
+ const result = subDays(new Date(2014, 8 /* Sep */, 1), 10.85)
+ assert.deepEqual(result, new Date(2014, 7 /* Aug */, 22))
+ })
+
+ it('implicitly converts number arguments', function() {
+ // $ExpectedMistake
+ // @ts-expect-error
+ const result = subDays(new Date(2014, 8 /* Sep */, 1), '10')
+ assert.deepEqual(result, new Date(2014, 7 /* Aug */, 22))
+ })
+
+ it('does not mutate the original date', function() {
+ const date = new Date(2014, 8 /* Sep */, 1)
+ subDays(date, 11)
+ assert.deepEqual(date, new Date(2014, 8 /* Sep */, 1))
+ })
+
+ it('returns `Invalid Date` if the given date is invalid', function() {
+ const result = subDays(new Date(NaN), 10)
+ // @ts-expect-error
+ assert(result instanceof Date && isNaN(result))
+ })
+
+ it('returns `Invalid Date` if the given amount is NaN', function() {
+ const result = subDays(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() {
+ assert.throws(subDays.bind(null), TypeError)
+ assert.throws(subDays.bind(null, 1), TypeError)
+ })
+})