diff options
Diffstat (limited to 'date-fns/src/startOfYear')
-rw-r--r-- | date-fns/src/startOfYear/benchmark.js | 21 | ||||
-rw-r--r-- | date-fns/src/startOfYear/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/startOfYear/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/startOfYear/index.ts | 34 | ||||
-rw-r--r-- | date-fns/src/startOfYear/test.ts | 46 |
5 files changed, 157 insertions, 0 deletions
diff --git a/date-fns/src/startOfYear/benchmark.js b/date-fns/src/startOfYear/benchmark.js new file mode 100644 index 0000000..4a2893e --- /dev/null +++ b/date-fns/src/startOfYear/benchmark.js @@ -0,0 +1,21 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import startOfYear from '.' +import moment from 'moment' + +suite('startOfYear', function () { + benchmark('date-fns', function () { + return startOfYear(this.date) + }) + + benchmark('Moment.js', function () { + return this.moment.startOf('year') + }) +}, { + setup: function () { + this.date = new Date() + this.moment = moment() + } +}) diff --git a/date-fns/src/startOfYear/index.d.ts b/date-fns/src/startOfYear/index.d.ts new file mode 100644 index 0000000..669ffc9 --- /dev/null +++ b/date-fns/src/startOfYear/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { startOfYear } from 'date-fns' +export default startOfYear diff --git a/date-fns/src/startOfYear/index.js.flow b/date-fns/src/startOfYear/index.js.flow new file mode 100644 index 0000000..4b7c616 --- /dev/null +++ b/date-fns/src/startOfYear/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/startOfYear/index.ts b/date-fns/src/startOfYear/index.ts new file mode 100644 index 0000000..b72be94 --- /dev/null +++ b/date-fns/src/startOfYear/index.ts @@ -0,0 +1,34 @@ +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name startOfYear + * @category Year Helpers + * @summary Return the start of a year for the given date. + * + * @description + * Return the start of a year 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 year + * @throws {TypeError} 1 argument required + * + * @example + * // The start of a year for 2 September 2014 11:55:00: + * const result = startOfYear(new Date(2014, 8, 2, 11, 55, 00)) + * //=> Wed Jan 01 2014 00:00:00 + */ +export default function startOfYear(dirtyDate: Date | number): Date { + requiredArgs(1, arguments) + + const cleanDate = toDate(dirtyDate) + const date = new Date(0) + date.setFullYear(cleanDate.getFullYear(), 0, 1) + date.setHours(0, 0, 0, 0) + return date +} diff --git a/date-fns/src/startOfYear/test.ts b/date-fns/src/startOfYear/test.ts new file mode 100644 index 0000000..bff3352 --- /dev/null +++ b/date-fns/src/startOfYear/test.ts @@ -0,0 +1,46 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import startOfYear from '.' + +describe('startOfYear', function() { + it('returns the date with the time set to 00:00:00 and the date set to the first day of a year', function() { + const date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0) + const result = startOfYear(date) + assert.deepEqual(result, new Date(2014, 0 /* Jan */, 1, 0, 0, 0, 0)) + }) + + it('accepts a timestamp', function() { + const date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0).getTime() + const result = startOfYear(date) + assert.deepEqual(result, new Date(2014, 0 /* Dec */, 1, 0, 0, 0, 0)) + }) + + it('does not mutate the original date', function() { + const date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0) + startOfYear(date) + assert.deepEqual(date, new Date(2014, 8 /* Sep */, 2, 11, 55, 0)) + }) + + it('handles dates before 100 AD', function() { + const initialDate = new Date(0) + initialDate.setFullYear(9, 0 /* Jan */, 5) + initialDate.setHours(0, 0, 0, 0) + const expectedResult = new Date(0) + expectedResult.setFullYear(9, 0 /* Jan */, 1) + expectedResult.setHours(0, 0, 0, 0) + const result = startOfYear(initialDate) + assert.deepEqual(result, expectedResult) + }) + + it('returns `Invalid Date` if the given date is invalid', function() { + const result = startOfYear(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(startOfYear.bind(null), TypeError) + }) +}) |