summaryrefslogtreecommitdiff
path: root/date-fns/src/sub
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/sub')
-rw-r--r--date-fns/src/sub/benchmark.js25
-rw-r--r--date-fns/src/sub/index.d.ts4
-rw-r--r--date-fns/src/sub/index.js72
-rw-r--r--date-fns/src/sub/index.js.flow52
-rw-r--r--date-fns/src/sub/test.js82
5 files changed, 235 insertions, 0 deletions
diff --git a/date-fns/src/sub/benchmark.js b/date-fns/src/sub/benchmark.js
new file mode 100644
index 0000000..8b96341
--- /dev/null
+++ b/date-fns/src/sub/benchmark.js
@@ -0,0 +1,25 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import sub from '.'
+import moment from 'moment'
+
+suite(
+ 'add',
+ function() {
+ benchmark('date-fns', function() {
+ return sub(this.date, { hours: 5, minutes: 10 })
+ })
+
+ benchmark('Moment.js', function() {
+ return this.moment.subtract({ hours: 5, minutes: 10 })
+ })
+ },
+ {
+ setup: function() {
+ this.date = new Date()
+ this.moment = moment()
+ }
+ }
+)
diff --git a/date-fns/src/sub/index.d.ts b/date-fns/src/sub/index.d.ts
new file mode 100644
index 0000000..7a4f3ec
--- /dev/null
+++ b/date-fns/src/sub/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { sub } from 'date-fns'
+export default sub
diff --git a/date-fns/src/sub/index.js b/date-fns/src/sub/index.js
new file mode 100644
index 0000000..fa5f30f
--- /dev/null
+++ b/date-fns/src/sub/index.js
@@ -0,0 +1,72 @@
+import subDays from '../subDays/index'
+import subMonths from '../subMonths/index'
+import toDate from '../toDate/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+import toInteger from '../_lib/toInteger/index'
+
+/**
+ * @name sub
+ * @category Common Helpers
+ * @summary Subtract the specified years, months, weeks, days, hours, minutes and seconds from the given date.
+ *
+ * @description
+ * Subtract the specified years, months, weeks, days, hours, minutes and seconds from the given date.
+ *
+ * @param {Date|Number} date - the date to be changed
+ * @param {Duration} duration - the object with years, months, weeks, days, hours, minutes and seconds to be subtracted
+ *
+ * | Key | Description |
+ * |---------|------------------------------------|
+ * | years | Amount of years to be subtracted |
+ * | months | Amount of months to be subtracted |
+ * | weeks | Amount of weeks to be subtracted |
+ * | days | Amount of days to be subtracted |
+ * | hours | Amount of hours to be subtracted |
+ * | minutes | Amount of minutes to be subtracted |
+ * | seconds | Amount of seconds to be subtracted |
+ *
+ * All values default to 0
+ *
+ * @returns {Date} the new date with the seconds subtracted
+ * @throws {TypeError} 2 arguments required
+ *
+ * @example
+ * // Subtract the following duration from 15 June 2017 15:29:20
+ * const result = sub(new Date(2017, 5, 15, 15, 29, 20), {
+ * years: 2,
+ * months: 9,
+ * weeks: 1,
+ * days: 7,
+ * hours: 5,
+ * minutes: 9,
+ * seconds: 30
+ * })
+ * //=> Mon Sep 1 2014 10:19:50
+ */
+export default function sub(dirtyDate, duration) {
+ requiredArgs(2, arguments)
+
+ if (!duration || typeof duration !== 'object') return new Date(NaN)
+
+ const years = 'years' in duration ? toInteger(duration.years) : 0
+ const months = 'months' in duration ? toInteger(duration.months) : 0
+ const weeks = 'weeks' in duration ? toInteger(duration.weeks) : 0
+ const days = 'days' in duration ? toInteger(duration.days) : 0
+ const hours = 'hours' in duration ? toInteger(duration.hours) : 0
+ const minutes = 'minutes' in duration ? toInteger(duration.minutes) : 0
+ const seconds = 'seconds' in duration ? toInteger(duration.seconds) : 0
+
+ // Subtract years and months
+ const dateWithoutMonths = subMonths(toDate(dirtyDate), months + years * 12)
+
+ // Subtract weeks and days
+ const dateWithoutDays = subDays(dateWithoutMonths, days + weeks * 7)
+
+ // Subtract hours, minutes and seconds
+ const minutestoSub = minutes + hours * 60
+ const secondstoSub = seconds + minutestoSub * 60
+ const mstoSub = secondstoSub * 1000
+ const finalDate = new Date(dateWithoutDays.getTime() - mstoSub)
+
+ return finalDate
+}
diff --git a/date-fns/src/sub/index.js.flow b/date-fns/src/sub/index.js.flow
new file mode 100644
index 0000000..e79df7c
--- /dev/null
+++ b/date-fns/src/sub/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, duration: Duration) => Date
diff --git a/date-fns/src/sub/test.js b/date-fns/src/sub/test.js
new file mode 100644
index 0000000..016accc
--- /dev/null
+++ b/date-fns/src/sub/test.js
@@ -0,0 +1,82 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import sub from '.'
+
+describe('sub', () => {
+ it('subtracts the duration from the given date', () => {
+ const result = sub(new Date(2017, 5 /* June */, 15, 15, 29, 20), {
+ years: 2,
+ months: 9,
+ weeks: 1,
+ days: 7,
+ hours: 5,
+ minutes: 9,
+ seconds: 30
+ })
+ assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 10, 19, 50))
+ })
+
+ it('returns same date object when passed empty duration', () => {
+ const result = sub(new Date(2014, 8 /* Sep */, 1, 10).getTime(), {})
+ assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 10))
+ })
+
+ it('accepts a timestamp', () => {
+ const result = sub(new Date(2014, 8 /* Sep */, 1, 14).getTime(), {
+ hours: 4
+ })
+ assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 10))
+ })
+
+ it('converts a fractional number to an integer', () => {
+ const result = sub(new Date(2014, 8 /* Sep */, 1, 14), { hours: 4.2 })
+ assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 10))
+ })
+
+ it('implicitly converts number arguments', () => {
+ // $ExpectedMistake
+ const result = sub(new Date(2014, 8 /* Sep */, 1, 14), { hours: '4.2' })
+ assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 10))
+ })
+
+ it('does not mutate the original date', () => {
+ const date = new Date(2014, 8 /* Sep */, 1, 10)
+ sub(date, { hours: 4 })
+ assert.deepEqual(date, new Date(2014, 8 /* Sep */, 1, 10))
+ })
+
+ it('works well if the desired month has fewer days and the provided date is in the last day of a month', () => {
+ const date = new Date(2014, 11 /* Dec */, 31)
+ const result = sub(date, { months: 3 })
+ assert.deepEqual(result, new Date(2014, 8 /* Sep */, 30))
+ })
+
+ it('handles dates before 100 AD', () => {
+ const initialDate = new Date(0)
+ initialDate.setFullYear(1, 2 /* Mar */, 31)
+ initialDate.setHours(0, 0, 0, 0)
+ const expectedResult = new Date(0)
+ expectedResult.setFullYear(1, 1 /* Feb */, 28)
+ expectedResult.setHours(0, 0, 0, 0)
+ const result = sub(initialDate, { months: 1 })
+ assert.deepEqual(result, expectedResult)
+ })
+
+ it('returns `Invalid Date` if the given date is invalid', () => {
+ const result = sub(new Date(NaN), { hours: 5 })
+ assert(result instanceof Date && isNaN(result))
+ })
+
+ it('throws RangeError exception if passed Number as duration', () => {
+ // $ExpectedMistake
+ const result = sub(new Date(2014, 8, 1), 'wut')
+ assert(result instanceof Date && isNaN(result))
+ })
+
+ it('throws TypeError exception if passed less than 2 arguments', () => {
+ assert.throws(sub.bind(null), TypeError)
+ assert.throws(sub.bind(null, 1), TypeError)
+ })
+})