summaryrefslogtreecommitdiff
path: root/date-fns/src/subMilliseconds
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/subMilliseconds')
-rw-r--r--date-fns/src/subMilliseconds/benchmark.js21
-rw-r--r--date-fns/src/subMilliseconds/index.d.ts4
-rw-r--r--date-fns/src/subMilliseconds/index.js.flow52
-rw-r--r--date-fns/src/subMilliseconds/index.ts32
-rw-r--r--date-fns/src/subMilliseconds/test.ts67
5 files changed, 176 insertions, 0 deletions
diff --git a/date-fns/src/subMilliseconds/benchmark.js b/date-fns/src/subMilliseconds/benchmark.js
new file mode 100644
index 0000000..597dac4
--- /dev/null
+++ b/date-fns/src/subMilliseconds/benchmark.js
@@ -0,0 +1,21 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import subMilliseconds from '.'
+import moment from 'moment'
+
+suite('subMilliseconds', function () {
+ benchmark('date-fns', function () {
+ return subMilliseconds(this.date, 800)
+ })
+
+ benchmark('Moment.js', function () {
+ return this.moment.subtract(800, 'milliseconds')
+ })
+}, {
+ setup: function () {
+ this.date = new Date()
+ this.moment = moment()
+ }
+})
diff --git a/date-fns/src/subMilliseconds/index.d.ts b/date-fns/src/subMilliseconds/index.d.ts
new file mode 100644
index 0000000..ffc51f1
--- /dev/null
+++ b/date-fns/src/subMilliseconds/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { subMilliseconds } from 'date-fns'
+export default subMilliseconds
diff --git a/date-fns/src/subMilliseconds/index.js.flow b/date-fns/src/subMilliseconds/index.js.flow
new file mode 100644
index 0000000..f568f0d
--- /dev/null
+++ b/date-fns/src/subMilliseconds/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/subMilliseconds/index.ts b/date-fns/src/subMilliseconds/index.ts
new file mode 100644
index 0000000..094209e
--- /dev/null
+++ b/date-fns/src/subMilliseconds/index.ts
@@ -0,0 +1,32 @@
+import toInteger from '../_lib/toInteger/index'
+import addMilliseconds from '../addMilliseconds/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name subMilliseconds
+ * @category Millisecond Helpers
+ * @summary Subtract the specified number of milliseconds from the given date.
+ *
+ * @description
+ * Subtract the specified number of milliseconds 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 milliseconds 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 milliseconds subtracted
+ * @throws {TypeError} 2 arguments required
+ *
+ * @example
+ * // Subtract 750 milliseconds from 10 July 2014 12:45:30.000:
+ * const result = subMilliseconds(new Date(2014, 6, 10, 12, 45, 30, 0), 750)
+ * //=> Thu Jul 10 2014 12:45:29.250
+ */
+export default function subMilliseconds(dirtyDate: Date | number, dirtyAmount: number) {
+ requiredArgs(2, arguments)
+
+ const amount = toInteger(dirtyAmount)
+ return addMilliseconds(dirtyDate, -amount)
+}
diff --git a/date-fns/src/subMilliseconds/test.ts b/date-fns/src/subMilliseconds/test.ts
new file mode 100644
index 0000000..bd93b39
--- /dev/null
+++ b/date-fns/src/subMilliseconds/test.ts
@@ -0,0 +1,67 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import subMilliseconds from '.'
+
+describe('subMilliseconds', function() {
+ it('subtracts the given number of milliseconds', function() {
+ const result = subMilliseconds(
+ new Date(2014, 6 /* Jul */, 10, 12, 45, 30, 0),
+ 750
+ )
+ assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 45, 29, 250))
+ })
+
+ it('accepts a timestamp', function() {
+ const result = subMilliseconds(
+ new Date(2014, 6 /* Jul */, 10, 12, 45, 30, 0).getTime(),
+ 500
+ )
+ assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 45, 29, 500))
+ })
+
+ it('converts a fractional number to an integer', function() {
+ const result = subMilliseconds(
+ new Date(2014, 6 /* Jul */, 10, 12, 45, 30, 0),
+ 750.75
+ )
+ assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 45, 29, 250))
+ })
+
+ it('implicitly converts number arguments', function() {
+ const result = subMilliseconds(
+ new Date(2014, 6 /* Jul */, 10, 12, 45, 30, 0),
+ // $ExpectedMistake
+ // @ts-expect-error
+ '750'
+ )
+ assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 45, 29, 250))
+ })
+
+ it('does not mutate the original date', function() {
+ const date = new Date(2014, 6 /* Jul */, 10, 12, 45, 30, 0)
+ subMilliseconds(date, 250)
+ assert.deepEqual(date, new Date(2014, 6 /* Jul */, 10, 12, 45, 30, 0))
+ })
+
+ it('returns `Invalid Date` if the given date is invalid', function() {
+ const result = subMilliseconds(new Date(NaN), 750)
+ // @ts-expect-error
+ assert(result instanceof Date && isNaN(result))
+ })
+
+ it('returns `Invalid Date` if the given amount is NaN', function() {
+ const result = subMilliseconds(
+ new Date(2014, 6 /* Jul */, 10, 12, 45, 30, 0),
+ NaN
+ )
+ // @ts-expect-error
+ assert(result instanceof Date && isNaN(result))
+ })
+
+ it('throws TypeError exception if passed less than 2 arguments', function() {
+ assert.throws(subMilliseconds.bind(null), TypeError)
+ assert.throws(subMilliseconds.bind(null, 1), TypeError)
+ })
+})