diff options
Diffstat (limited to 'date-fns/src/differenceInCalendarWeeks')
-rw-r--r-- | date-fns/src/differenceInCalendarWeeks/benchmark.js | 16 | ||||
-rw-r--r-- | date-fns/src/differenceInCalendarWeeks/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/differenceInCalendarWeeks/index.js.flow | 59 | ||||
-rw-r--r-- | date-fns/src/differenceInCalendarWeeks/index.ts | 67 | ||||
-rw-r--r-- | date-fns/src/differenceInCalendarWeeks/test.ts | 164 |
5 files changed, 310 insertions, 0 deletions
diff --git a/date-fns/src/differenceInCalendarWeeks/benchmark.js b/date-fns/src/differenceInCalendarWeeks/benchmark.js new file mode 100644 index 0000000..d014aee --- /dev/null +++ b/date-fns/src/differenceInCalendarWeeks/benchmark.js @@ -0,0 +1,16 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import differenceInCalendarWeeks from '.' + +suite('differenceInCalendarWeeks', function () { + benchmark('date-fns', function () { + return differenceInCalendarWeeks(this.dateA, this.dateB) + }) +}, { + setup: function () { + this.dateA = new Date() + this.dateB = new Date(this.dateA.getTime() + 604800000) + } +}) diff --git a/date-fns/src/differenceInCalendarWeeks/index.d.ts b/date-fns/src/differenceInCalendarWeeks/index.d.ts new file mode 100644 index 0000000..c370dd6 --- /dev/null +++ b/date-fns/src/differenceInCalendarWeeks/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { differenceInCalendarWeeks } from 'date-fns' +export default differenceInCalendarWeeks diff --git a/date-fns/src/differenceInCalendarWeeks/index.js.flow b/date-fns/src/differenceInCalendarWeeks/index.js.flow new file mode 100644 index 0000000..e4968a9 --- /dev/null +++ b/date-fns/src/differenceInCalendarWeeks/index.js.flow @@ -0,0 +1,59 @@ +// @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: ( + dateLeft: Date | number, + dateRight: Date | number, + options?: { + locale?: Locale, + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + } +) => number diff --git a/date-fns/src/differenceInCalendarWeeks/index.ts b/date-fns/src/differenceInCalendarWeeks/index.ts new file mode 100644 index 0000000..da15cb7 --- /dev/null +++ b/date-fns/src/differenceInCalendarWeeks/index.ts @@ -0,0 +1,67 @@ +import startOfWeek from '../startOfWeek/index' +import getTimezoneOffsetInMilliseconds from '../_lib/getTimezoneOffsetInMilliseconds/index' +import requiredArgs from '../_lib/requiredArgs/index' +import { LocaleOptions, WeekStartOptions } from '../types' + +const MILLISECONDS_IN_WEEK = 604800000 + +/** + * @name differenceInCalendarWeeks + * @category Week Helpers + * @summary Get the number of calendar weeks between the given dates. + * + * @description + * Get the number of calendar weeks between the given dates. + * + * ### 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} dateLeft - the later date + * @param {Date|Number} dateRight - the earlier date + * @param {Object} [options] - an object with options. + * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale} + * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday) + * @returns {Number} the number of calendar weeks + * @throws {TypeError} 2 arguments required + * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6 + * + * @example + * // How many calendar weeks are between 5 July 2014 and 20 July 2014? + * const result = differenceInCalendarWeeks( + * new Date(2014, 6, 20), + * new Date(2014, 6, 5) + * ) + * //=> 3 + * + * @example + * // If the week starts on Monday, + * // how many calendar weeks are between 5 July 2014 and 20 July 2014? + * const result = differenceInCalendarWeeks( + * new Date(2014, 6, 20), + * new Date(2014, 6, 5), + * { weekStartsOn: 1 } + * ) + * //=> 2 + */ +export default function differenceInCalendarWeeks( + dirtyDateLeft: Date | number, + dirtyDateRight: Date | number, + dirtyOptions?: LocaleOptions & WeekStartOptions +): number { + requiredArgs(2, arguments) + + const startOfWeekLeft = startOfWeek(dirtyDateLeft, dirtyOptions) + const startOfWeekRight = startOfWeek(dirtyDateRight, dirtyOptions) + + const timestampLeft = + startOfWeekLeft.getTime() - getTimezoneOffsetInMilliseconds(startOfWeekLeft) + const timestampRight = + startOfWeekRight.getTime() - + getTimezoneOffsetInMilliseconds(startOfWeekRight) + + // Round the number of days to the nearest integer + // because the number of milliseconds in a week is not constant + // (e.g. it's different in the week of the daylight saving time clock shift) + return Math.round((timestampLeft - timestampRight) / MILLISECONDS_IN_WEEK) +} diff --git a/date-fns/src/differenceInCalendarWeeks/test.ts b/date-fns/src/differenceInCalendarWeeks/test.ts new file mode 100644 index 0000000..8a1f6e2 --- /dev/null +++ b/date-fns/src/differenceInCalendarWeeks/test.ts @@ -0,0 +1,164 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import differenceInCalendarWeeks from '.' + +describe('differenceInCalendarWeeks', function () { + it('returns the number of calendar weeks between the given dates', function () { + const result = differenceInCalendarWeeks( + new Date(2014, 6 /* Jul */, 8, 18, 0), + new Date(2014, 5 /* Jun */, 29, 6, 0) + ) + assert(result === 1) + }) + + it('allows to specify which day is the first day of the week', function () { + const result = differenceInCalendarWeeks( + new Date(2014, 6 /* Jul */, 8, 18, 0), + new Date(2014, 5 /* Jun */, 29, 6, 0), + { weekStartsOn: 1 } + ) + assert(result === 2) + }) + + it('allows to specify which day is the first day of the week in locale', function () { + const result = differenceInCalendarWeeks( + new Date(2014, 6 /* Jul */, 8, 18, 0), + new Date(2014, 5 /* Jun */, 29, 6, 0), + { + // @ts-expect-error + locale: { + options: { weekStartsOn: 1 }, + }, + } + ) + assert(result === 2) + }) + + it('`options.weekStartsOn` overwrites the first day of the week specified in locale', function () { + const result = differenceInCalendarWeeks( + new Date(2014, 6 /* Jul */, 8, 18, 0), + new Date(2014, 5 /* Jun */, 29, 6, 0), + { + weekStartsOn: 1, + // @ts-expect-error + locale: { + options: { weekStartsOn: 0 }, + }, + } + ) + assert(result === 2) + }) + + it('returns a positive number if the time value of the second date is smaller', function () { + const result = differenceInCalendarWeeks( + new Date(2014, 6 /* Jul */, 8, 18, 0), + new Date(2014, 5 /* Jun */, 29, 6, 0), + // @ts-expect-error + { weekStartsOn: '1' } + ) + assert(result === 2) + }) + + it('returns a negative number if the time value of the first date is smaller', function () { + const result = differenceInCalendarWeeks( + new Date(2014, 5 /* Jun */, 29, 6, 0), + new Date(2014, 6 /* Jul */, 8, 18, 0) + ) + assert(result === -1) + }) + + it('accepts timestamps', function () { + const result = differenceInCalendarWeeks( + new Date(2014, 6 /* Jul */, 12).getTime(), + new Date(2014, 6 /* Jul */, 2).getTime() + ) + assert(result === 1) + }) + + describe('edge cases', function () { + it('the difference is less than a week, but the given dates are in different calendar weeks', function () { + const result = differenceInCalendarWeeks( + new Date(2014, 6 /* Jul */, 6), + new Date(2014, 6 /* Jul */, 5) + ) + assert(result === 1) + }) + + it('the same for the swapped dates', function () { + const result = differenceInCalendarWeeks( + new Date(2014, 6 /* Jul */, 5), + new Date(2014, 6 /* Jul */, 6) + ) + assert(result === -1) + }) + + it('the days of weeks of the given dates are the same', function () { + const result = differenceInCalendarWeeks( + new Date(2014, 6 /* Jul */, 9), + new Date(2014, 6 /* Jul */, 2) + ) + assert(result === 1) + }) + + it('the given dates are the same', function () { + const result = differenceInCalendarWeeks( + new Date(2014, 8 /* Sep */, 5, 0, 0), + new Date(2014, 8 /* Sep */, 5, 0, 0) + ) + assert(result === 0) + }) + + it('does not return -0 when the given dates are the same', () => { + function isNegativeZero(x: number): boolean { + return x === 0 && 1 / x < 0 + } + + const result = differenceInCalendarWeeks( + new Date(2014, 8 /* Sep */, 5, 0, 0), + new Date(2014, 8 /* Sep */, 5, 0, 0) + ) + + const resultIsNegative = isNegativeZero(result) + assert(resultIsNegative === false) + }) + }) + + it('returns NaN if the first date is `Invalid Date`', function () { + const result = differenceInCalendarWeeks( + new Date(NaN), + new Date(2017, 0 /* Jan */, 1) + ) + assert(isNaN(result)) + }) + + it('returns NaN if the second date is `Invalid Date`', function () { + const result = differenceInCalendarWeeks( + new Date(2017, 0 /* Jan */, 1), + new Date(NaN) + ) + assert(isNaN(result)) + }) + + it('returns NaN if the both dates are `Invalid Date`', function () { + const result = differenceInCalendarWeeks(new Date(NaN), new Date(NaN)) + assert(isNaN(result)) + }) + + it('throws `RangeError` if `options.weekStartsOn` is not convertable to 0, 1, ..., 6 or undefined', function () { + // @ts-expect-error + const block = differenceInCalendarWeeks.bind( + null, + new Date(2014, 6 /* Jul */, 8, 18, 0), + new Date(2014, 5 /* Jun */, 29, 6, 0), + { weekStartsOn: NaN } + ) + assert.throws(block, RangeError) + }) + + it('throws TypeError exception if passed less than 2 arguments', function () { + assert.throws(differenceInCalendarWeeks.bind(null), TypeError) + assert.throws(differenceInCalendarWeeks.bind(null, 1), TypeError) + }) +}) |