diff options
Diffstat (limited to 'date-fns/src/isThisWeek')
-rw-r--r-- | date-fns/src/isThisWeek/benchmark.js | 20 | ||||
-rw-r--r-- | date-fns/src/isThisWeek/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/isThisWeek/index.js.flow | 58 | ||||
-rw-r--r-- | date-fns/src/isThisWeek/index.ts | 48 | ||||
-rw-r--r-- | date-fns/src/isThisWeek/test.ts | 42 |
5 files changed, 172 insertions, 0 deletions
diff --git a/date-fns/src/isThisWeek/benchmark.js b/date-fns/src/isThisWeek/benchmark.js new file mode 100644 index 0000000..ac9676f --- /dev/null +++ b/date-fns/src/isThisWeek/benchmark.js @@ -0,0 +1,20 @@ +// @flow +/* eslint-env mocha */ + +/* global suite, benchmark */ + +import isThisWeek from '.' + +suite( + 'isThisWeek', + () => { + benchmark('date-fns', function() { + return isThisWeek(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/date-fns/src/isThisWeek/index.d.ts b/date-fns/src/isThisWeek/index.d.ts new file mode 100644 index 0000000..b22ba53 --- /dev/null +++ b/date-fns/src/isThisWeek/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isThisWeek } from 'date-fns' +export default isThisWeek diff --git a/date-fns/src/isThisWeek/index.js.flow b/date-fns/src/isThisWeek/index.js.flow new file mode 100644 index 0000000..579c74a --- /dev/null +++ b/date-fns/src/isThisWeek/index.js.flow @@ -0,0 +1,58 @@ +// @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, + options?: { + locale?: Locale, + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + } +) => boolean diff --git a/date-fns/src/isThisWeek/index.ts b/date-fns/src/isThisWeek/index.ts new file mode 100644 index 0000000..4d08327 --- /dev/null +++ b/date-fns/src/isThisWeek/index.ts @@ -0,0 +1,48 @@ +import isSameWeek from '../isSameWeek/index' +import requiredArgs from '../_lib/requiredArgs/index' +import { LocaleOptions, WeekStartOptions } from '../types' + +/** + * @name isThisWeek + * @category Week Helpers + * @summary Is the given date in the same week as the current date? + * @pure false + * + * @description + * Is the given date in the same week as the current date? + * + * > ⚠️ Please note that this function is not present in the FP submodule as + * > it uses `Date.now()` internally hence impure and can't be safely curried. + * + * ### 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 date to check + * @param {Object} [options] - the 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 {Boolean} the date is in this week + * @throws {TypeError} 1 argument required + * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6 + * + * @example + * // If today is 25 September 2014, is 21 September 2014 in this week? + * var result = isThisWeek(new Date(2014, 8, 21)) + * //=> true + * + * @example + * // If today is 25 September 2014 and week starts with Monday + * // is 21 September 2014 in this week? + * var result = isThisWeek(new Date(2014, 8, 21), { weekStartsOn: 1 }) + * //=> false + */ + +export default function isThisWeek( + dirtyDate: Date | number, + options?: LocaleOptions & WeekStartOptions +): boolean { + requiredArgs(1, arguments) + + return isSameWeek(dirtyDate, Date.now(), options) +} diff --git a/date-fns/src/isThisWeek/test.ts b/date-fns/src/isThisWeek/test.ts new file mode 100644 index 0000000..e6e323d --- /dev/null +++ b/date-fns/src/isThisWeek/test.ts @@ -0,0 +1,42 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isThisWeek from '.' + +describe('isThisWeek', () => { + let clock: sinon.SinonFakeTimers + beforeEach(() => { + clock = sinon.useFakeTimers(new Date(2014, 8 /* Sep */, 25).getTime()) + }) + + afterEach(() => { + clock.restore() + }) + + it('returns true if the given date and the current date have the same week', () => { + const date = new Date(2014, 8 /* Sep */, 21) + assert(isThisWeek(date) === true) + }) + + it('returns false if the given date and the current date have different weeks', () => { + const date = new Date(2014, 8 /* Sep */, 29) + assert(isThisWeek(date) === false) + }) + + it('allows to specify which day is the first day of the week', () => { + const date = new Date(2014, 8 /* Sep */, 28) + assert(isThisWeek(date, { weekStartsOn: 1 }) === true) + }) + + it('accepts a timestamp', () => { + const date = new Date(2014, 8 /* Sep */, 21).getTime() + assert(isThisWeek(date) === true) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + // @ts-expect-error + assert.throws(isThisWeek.bind(null), TypeError) + }) +}) |