diff options
author | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:46:06 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:48:30 -0300 |
commit | 38acabfa6089ab8ac469c12b5f55022fb96935e5 (patch) | |
tree | 453dbf70000cc5e338b06201af1eaca8343f8f73 /date-fns/src/getQuarter | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.gz node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.bz2 node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.zip |
Diffstat (limited to 'date-fns/src/getQuarter')
-rw-r--r-- | date-fns/src/getQuarter/benchmark.js | 21 | ||||
-rw-r--r-- | date-fns/src/getQuarter/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/getQuarter/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/getQuarter/index.ts | 31 | ||||
-rw-r--r-- | date-fns/src/getQuarter/test.ts | 26 |
5 files changed, 134 insertions, 0 deletions
diff --git a/date-fns/src/getQuarter/benchmark.js b/date-fns/src/getQuarter/benchmark.js new file mode 100644 index 0000000..d83e7c2 --- /dev/null +++ b/date-fns/src/getQuarter/benchmark.js @@ -0,0 +1,21 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import getQuarter from '.' +import moment from 'moment' + +suite('getQuarter', function () { + benchmark('date-fns', function () { + return getQuarter(this.date) + }) + + benchmark('Moment.js', function () { + return this.moment.quarter() + }) +}, { + setup: function () { + this.date = new Date() + this.moment = moment() + } +}) diff --git a/date-fns/src/getQuarter/index.d.ts b/date-fns/src/getQuarter/index.d.ts new file mode 100644 index 0000000..6434ba0 --- /dev/null +++ b/date-fns/src/getQuarter/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { getQuarter } from 'date-fns' +export default getQuarter diff --git a/date-fns/src/getQuarter/index.js.flow b/date-fns/src/getQuarter/index.js.flow new file mode 100644 index 0000000..07bb012 --- /dev/null +++ b/date-fns/src/getQuarter/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) => number diff --git a/date-fns/src/getQuarter/index.ts b/date-fns/src/getQuarter/index.ts new file mode 100644 index 0000000..2dc72f3 --- /dev/null +++ b/date-fns/src/getQuarter/index.ts @@ -0,0 +1,31 @@ +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name getQuarter + * @category Quarter Helpers + * @summary Get the year quarter of the given date. + * + * @description + * Get the year quarter of 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 given date + * @returns {Number} the quarter + * @throws {TypeError} 1 argument required + * + * @example + * // Which quarter is 2 July 2014? + * const result = getQuarter(new Date(2014, 6, 2)) + * //=> 3 + */ +export default function getQuarter(dirtyDate: Date | number): number { + requiredArgs(1, arguments) + + const date = toDate(dirtyDate) + const quarter = Math.floor(date.getMonth() / 3) + 1 + return quarter +} diff --git a/date-fns/src/getQuarter/test.ts b/date-fns/src/getQuarter/test.ts new file mode 100644 index 0000000..9876622 --- /dev/null +++ b/date-fns/src/getQuarter/test.ts @@ -0,0 +1,26 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import getQuarter from '.' + +describe('getQuarter', function() { + it('returns the quarter of the given date', function() { + const result = getQuarter(new Date(2014, 6 /* Jul */, 2)) + assert(result === 3) + }) + + it('accepts a timestamp', function() { + const result = getQuarter(new Date(2014, 3 /* Apr */, 2).getTime()) + assert(result === 2) + }) + + it('returns NaN if the given date is invalid', function() { + const result = getQuarter(new Date(NaN)) + assert(isNaN(result)) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + assert.throws(getQuarter.bind(null), TypeError) + }) +}) |