summaryrefslogtreecommitdiff
path: root/date-fns/src/startOfMinute
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/startOfMinute')
-rw-r--r--date-fns/src/startOfMinute/benchmark.js21
-rw-r--r--date-fns/src/startOfMinute/index.d.ts4
-rw-r--r--date-fns/src/startOfMinute/index.js.flow52
-rw-r--r--date-fns/src/startOfMinute/index.ts32
-rw-r--r--date-fns/src/startOfMinute/test.ts35
5 files changed, 144 insertions, 0 deletions
diff --git a/date-fns/src/startOfMinute/benchmark.js b/date-fns/src/startOfMinute/benchmark.js
new file mode 100644
index 0000000..af501d4
--- /dev/null
+++ b/date-fns/src/startOfMinute/benchmark.js
@@ -0,0 +1,21 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import startOfMinute from '.'
+import moment from 'moment'
+
+suite('startOfMinute', function () {
+ benchmark('date-fns', function () {
+ return startOfMinute(this.date)
+ })
+
+ benchmark('Moment.js', function () {
+ return this.moment.startOf('minute')
+ })
+}, {
+ setup: function () {
+ this.date = new Date()
+ this.moment = moment()
+ }
+})
diff --git a/date-fns/src/startOfMinute/index.d.ts b/date-fns/src/startOfMinute/index.d.ts
new file mode 100644
index 0000000..1b96683
--- /dev/null
+++ b/date-fns/src/startOfMinute/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { startOfMinute } from 'date-fns'
+export default startOfMinute
diff --git a/date-fns/src/startOfMinute/index.js.flow b/date-fns/src/startOfMinute/index.js.flow
new file mode 100644
index 0000000..4b7c616
--- /dev/null
+++ b/date-fns/src/startOfMinute/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) => Date
diff --git a/date-fns/src/startOfMinute/index.ts b/date-fns/src/startOfMinute/index.ts
new file mode 100644
index 0000000..87d6a12
--- /dev/null
+++ b/date-fns/src/startOfMinute/index.ts
@@ -0,0 +1,32 @@
+import toDate from '../toDate/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name startOfMinute
+ * @category Minute Helpers
+ * @summary Return the start of a minute for the given date.
+ *
+ * @description
+ * Return the start of a minute for the given date.
+ * The result will be in the local timezone.
+ *
+ * ### 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 original date
+ * @returns {Date} the start of a minute
+ * @throws {TypeError} 1 argument required
+ *
+ * @example
+ * // The start of a minute for 1 December 2014 22:15:45.400:
+ * const result = startOfMinute(new Date(2014, 11, 1, 22, 15, 45, 400))
+ * //=> Mon Dec 01 2014 22:15:00
+ */
+export default function startOfMinute(dirtyDate: Date | number): Date {
+ requiredArgs(1, arguments)
+
+ const date = toDate(dirtyDate)
+ date.setSeconds(0, 0)
+ return date
+}
diff --git a/date-fns/src/startOfMinute/test.ts b/date-fns/src/startOfMinute/test.ts
new file mode 100644
index 0000000..b362e36
--- /dev/null
+++ b/date-fns/src/startOfMinute/test.ts
@@ -0,0 +1,35 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import startOfMinute from '.'
+
+describe('startOfMinute', function() {
+ it('returns the date with the time set to the first millisecond of a minute', function() {
+ const date = new Date(2014, 11 /* Dec */, 1, 22, 15, 45, 400)
+ const result = startOfMinute(date)
+ assert.deepEqual(result, new Date(2014, 11 /* Dec */, 1, 22, 15))
+ })
+
+ it('accepts a timestamp', function() {
+ const date = new Date(2014, 11 /* Dec */, 1, 22, 15).getTime()
+ const result = startOfMinute(date)
+ assert.deepEqual(result, new Date(2014, 11 /* Dec */, 1, 22, 15))
+ })
+
+ it('does not mutate the original date', function() {
+ const date = new Date(2014, 11 /* Dec */, 1, 22, 15, 45, 400)
+ startOfMinute(date)
+ assert.deepEqual(date, new Date(2014, 11 /* Dec */, 1, 22, 15, 45, 400))
+ })
+
+ it('returns `Invalid Date` if the given date is invalid', function() {
+ const result = startOfMinute(new Date(NaN))
+ //@ts-expect-error
+ assert(result instanceof Date && isNaN(result))
+ })
+
+ it('throws TypeError exception if passed less than 1 argument', function() {
+ assert.throws(startOfMinute.bind(null), TypeError)
+ })
+})