summaryrefslogtreecommitdiff
path: root/date-fns/src/startOfQuarter
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/startOfQuarter
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/startOfQuarter')
-rw-r--r--date-fns/src/startOfQuarter/benchmark.js21
-rw-r--r--date-fns/src/startOfQuarter/index.d.ts4
-rw-r--r--date-fns/src/startOfQuarter/index.js.flow52
-rw-r--r--date-fns/src/startOfQuarter/index.ts35
-rw-r--r--date-fns/src/startOfQuarter/test.ts35
5 files changed, 147 insertions, 0 deletions
diff --git a/date-fns/src/startOfQuarter/benchmark.js b/date-fns/src/startOfQuarter/benchmark.js
new file mode 100644
index 0000000..fc99fa0
--- /dev/null
+++ b/date-fns/src/startOfQuarter/benchmark.js
@@ -0,0 +1,21 @@
+// @flow
+/* eslint-env mocha */
+/* global suite, benchmark */
+
+import startOfQuarter from '.'
+import moment from 'moment'
+
+suite('startOfQuarter', function () {
+ benchmark('date-fns', function () {
+ return startOfQuarter(this.date)
+ })
+
+ benchmark('Moment.js', function () {
+ return this.moment.startOf('quarter')
+ })
+}, {
+ setup: function () {
+ this.date = new Date()
+ this.moment = moment()
+ }
+})
diff --git a/date-fns/src/startOfQuarter/index.d.ts b/date-fns/src/startOfQuarter/index.d.ts
new file mode 100644
index 0000000..7afa727
--- /dev/null
+++ b/date-fns/src/startOfQuarter/index.d.ts
@@ -0,0 +1,4 @@
+// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
+
+import { startOfQuarter } from 'date-fns'
+export default startOfQuarter
diff --git a/date-fns/src/startOfQuarter/index.js.flow b/date-fns/src/startOfQuarter/index.js.flow
new file mode 100644
index 0000000..4b7c616
--- /dev/null
+++ b/date-fns/src/startOfQuarter/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/startOfQuarter/index.ts b/date-fns/src/startOfQuarter/index.ts
new file mode 100644
index 0000000..c61c2a0
--- /dev/null
+++ b/date-fns/src/startOfQuarter/index.ts
@@ -0,0 +1,35 @@
+import toDate from '../toDate/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @name startOfQuarter
+ * @category Quarter Helpers
+ * @summary Return the start of a year quarter for the given date.
+ *
+ * @description
+ * Return the start of a year quarter 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 quarter
+ * @throws {TypeError} 1 argument required
+ *
+ * @example
+ * // The start of a quarter for 2 September 2014 11:55:00:
+ * const result = startOfQuarter(new Date(2014, 8, 2, 11, 55, 0))
+ * //=> Tue Jul 01 2014 00:00:00
+ */
+export default function startOfQuarter(dirtyDate: Date | number): Date {
+ requiredArgs(1, arguments)
+
+ const date = toDate(dirtyDate)
+ const currentMonth = date.getMonth()
+ const month = currentMonth - (currentMonth % 3)
+ date.setMonth(month, 1)
+ date.setHours(0, 0, 0, 0)
+ return date
+}
diff --git a/date-fns/src/startOfQuarter/test.ts b/date-fns/src/startOfQuarter/test.ts
new file mode 100644
index 0000000..311e60d
--- /dev/null
+++ b/date-fns/src/startOfQuarter/test.ts
@@ -0,0 +1,35 @@
+// @flow
+/* eslint-env mocha */
+
+import assert from 'power-assert'
+import startOfQuarter from '.'
+
+describe('startOfQuarter', function() {
+ it('returns the date with the time set to 00:00:00 and the date set to the first day of a quarter', function() {
+ const date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0)
+ const result = startOfQuarter(date)
+ assert.deepEqual(result, new Date(2014, 6 /* Jul */, 1))
+ })
+
+ it('accepts a timestamp', function() {
+ const date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0).getTime()
+ const result = startOfQuarter(date)
+ assert.deepEqual(result, new Date(2014, 6 /* Jul */, 1))
+ })
+
+ it('does not mutate the original date', function() {
+ const date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0)
+ startOfQuarter(date)
+ assert.deepEqual(date, new Date(2014, 8 /* Sep */, 2, 11, 55, 0))
+ })
+
+ it('returns `Invalid Date` if the given date is invalid', function() {
+ const result = startOfQuarter(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(startOfQuarter.bind(null), TypeError)
+ })
+})