From e1a44d95fbe4407d7b1d4560bc3192e610c4f594 Mon Sep 17 00:00:00 2001 From: Sasha Koss Date: Wed, 12 Jun 2019 10:20:41 +0400 Subject: [PATCH] Restore now-dependent functions (closes #1184) (#1197) --- CHANGELOG.md | 62 +- scripts/build/_lib/prettier.js | 2 +- scripts/build/_lib/typings/common.js | 31 +- scripts/build/docs.js | 39 +- scripts/test/node.sh | 4 +- src/endOfToday/benchmark.js | 9 + src/endOfToday/index.d.ts | 4 + src/endOfToday/index.js | 28 + src/endOfToday/index.js.flow | 38 ++ src/endOfToday/test.js | 24 + src/endOfTomorrow/benchmark.js | 9 + src/endOfTomorrow/index.d.ts | 4 + src/endOfTomorrow/index.js | 34 ++ src/endOfTomorrow/index.js.flow | 38 ++ src/endOfTomorrow/test.js | 34 ++ src/endOfYesterday/benchmark.js | 9 + src/endOfYesterday/index.d.ts | 4 + src/endOfYesterday/index.js | 34 ++ src/endOfYesterday/index.js.flow | 38 ++ src/endOfYesterday/test.js | 34 ++ src/esm/index.js | 20 + src/formatDistance/index.js | 10 +- src/formatDistanceToNow/benchmark.js | 25 + src/formatDistanceToNow/index.d.ts | 4 + src/formatDistanceToNow/index.js | 115 ++++ src/formatDistanceToNow/index.js.flow | 45 ++ src/formatDistanceToNow/test.js | 232 ++++++++ src/index.js | 20 + src/index.js.flow | 53 ++ src/isFuture/benchmark.js | 19 + src/isFuture/index.d.ts | 4 + src/isFuture/index.js | 36 ++ src/isFuture/index.js.flow | 38 ++ src/isFuture/test.js | 41 ++ src/isPast/benchmark.js | 19 + src/isPast/index.d.ts | 4 + src/isPast/index.js | 36 ++ src/isPast/index.js.flow | 38 ++ src/isPast/test.js | 41 ++ src/isThisHour/benchmark.js | 20 + src/isThisHour/index.d.ts | 4 + src/isThisHour/index.js | 37 ++ src/isThisHour/index.js.flow | 38 ++ src/isThisHour/test.js | 38 ++ src/isThisISOWeek/benchmark.js | 20 + src/isThisISOWeek/index.d.ts | 4 + src/isThisISOWeek/index.js | 39 ++ src/isThisISOWeek/index.js.flow | 38 ++ src/isThisISOWeek/test.js | 36 ++ src/isThisMinute/benchmark.js | 20 + src/isThisMinute/index.d.ts | 4 + src/isThisMinute/index.js | 38 ++ src/isThisMinute/index.js.flow | 38 ++ src/isThisMinute/test.js | 38 ++ src/isThisMonth/benchmark.js | 20 + src/isThisMonth/index.d.ts | 4 + src/isThisMonth/index.js | 37 ++ src/isThisMonth/index.js.flow | 38 ++ src/isThisMonth/test.js | 36 ++ src/isThisQuarter/benchmark.js | 20 + src/isThisQuarter/index.d.ts | 4 + src/isThisQuarter/index.js | 36 ++ src/isThisQuarter/index.js.flow | 38 ++ src/isThisQuarter/test.js | 36 ++ src/isThisSecond/benchmark.js | 20 + src/isThisSecond/index.d.ts | 4 + src/isThisSecond/index.js | 37 ++ src/isThisSecond/index.js.flow | 38 ++ src/isThisSecond/test.js | 38 ++ src/isThisWeek/benchmark.js | 20 + src/isThisWeek/index.d.ts | 4 + src/isThisWeek/index.js | 47 ++ src/isThisWeek/index.js.flow | 44 ++ src/isThisWeek/test.js | 41 ++ src/isThisYear/benchmark.js | 19 + src/isThisYear/index.d.ts | 4 + src/isThisYear/index.js | 36 ++ src/isThisYear/index.js.flow | 38 ++ src/isThisYear/test.js | 36 ++ src/isToday/benchmark.js | 19 + src/isToday/index.d.ts | 4 + src/isToday/index.js | 36 ++ src/isToday/index.js.flow | 38 ++ src/isToday/test.js | 36 ++ src/isTomorrow/benchmark.js | 19 + src/isTomorrow/index.d.ts | 4 + src/isTomorrow/index.js | 37 ++ src/isTomorrow/index.js.flow | 38 ++ src/isTomorrow/test.js | 36 ++ src/isYesterday/benchmark.js | 19 + src/isYesterday/index.d.ts | 4 + src/isYesterday/index.js | 37 ++ src/isYesterday/index.js.flow | 38 ++ src/isYesterday/test.js | 36 ++ src/startOfToday/benchmark.js | 9 + src/startOfToday/index.d.ts | 4 + src/startOfToday/index.js | 28 + src/startOfToday/index.js.flow | 38 ++ src/startOfToday/test.js | 24 + src/startOfTomorrow/benchmark.js | 9 + src/startOfTomorrow/index.d.ts | 4 + src/startOfTomorrow/index.js | 34 ++ src/startOfTomorrow/index.js.flow | 38 ++ src/startOfTomorrow/test.js | 34 ++ src/startOfYesterday/benchmark.js | 9 + src/startOfYesterday/index.d.ts | 4 + src/startOfYesterday/index.js | 34 ++ src/startOfYesterday/index.js.flow | 38 ++ src/startOfYesterday/test.js | 34 ++ typings.d.ts | 799 ++++++++++++++++++++++++++ 110 files changed, 3830 insertions(+), 97 deletions(-) create mode 100644 src/endOfToday/benchmark.js create mode 100644 src/endOfToday/index.d.ts create mode 100644 src/endOfToday/index.js create mode 100644 src/endOfToday/index.js.flow create mode 100644 src/endOfToday/test.js create mode 100644 src/endOfTomorrow/benchmark.js create mode 100644 src/endOfTomorrow/index.d.ts create mode 100644 src/endOfTomorrow/index.js create mode 100644 src/endOfTomorrow/index.js.flow create mode 100644 src/endOfTomorrow/test.js create mode 100644 src/endOfYesterday/benchmark.js create mode 100644 src/endOfYesterday/index.d.ts create mode 100644 src/endOfYesterday/index.js create mode 100644 src/endOfYesterday/index.js.flow create mode 100644 src/endOfYesterday/test.js create mode 100644 src/formatDistanceToNow/benchmark.js create mode 100644 src/formatDistanceToNow/index.d.ts create mode 100644 src/formatDistanceToNow/index.js create mode 100644 src/formatDistanceToNow/index.js.flow create mode 100644 src/formatDistanceToNow/test.js create mode 100644 src/isFuture/benchmark.js create mode 100644 src/isFuture/index.d.ts create mode 100644 src/isFuture/index.js create mode 100644 src/isFuture/index.js.flow create mode 100644 src/isFuture/test.js create mode 100644 src/isPast/benchmark.js create mode 100644 src/isPast/index.d.ts create mode 100644 src/isPast/index.js create mode 100644 src/isPast/index.js.flow create mode 100644 src/isPast/test.js create mode 100644 src/isThisHour/benchmark.js create mode 100644 src/isThisHour/index.d.ts create mode 100644 src/isThisHour/index.js create mode 100644 src/isThisHour/index.js.flow create mode 100644 src/isThisHour/test.js create mode 100644 src/isThisISOWeek/benchmark.js create mode 100644 src/isThisISOWeek/index.d.ts create mode 100644 src/isThisISOWeek/index.js create mode 100644 src/isThisISOWeek/index.js.flow create mode 100644 src/isThisISOWeek/test.js create mode 100644 src/isThisMinute/benchmark.js create mode 100644 src/isThisMinute/index.d.ts create mode 100644 src/isThisMinute/index.js create mode 100644 src/isThisMinute/index.js.flow create mode 100644 src/isThisMinute/test.js create mode 100644 src/isThisMonth/benchmark.js create mode 100644 src/isThisMonth/index.d.ts create mode 100644 src/isThisMonth/index.js create mode 100644 src/isThisMonth/index.js.flow create mode 100644 src/isThisMonth/test.js create mode 100644 src/isThisQuarter/benchmark.js create mode 100644 src/isThisQuarter/index.d.ts create mode 100644 src/isThisQuarter/index.js create mode 100644 src/isThisQuarter/index.js.flow create mode 100644 src/isThisQuarter/test.js create mode 100644 src/isThisSecond/benchmark.js create mode 100644 src/isThisSecond/index.d.ts create mode 100644 src/isThisSecond/index.js create mode 100644 src/isThisSecond/index.js.flow create mode 100644 src/isThisSecond/test.js create mode 100644 src/isThisWeek/benchmark.js create mode 100644 src/isThisWeek/index.d.ts create mode 100644 src/isThisWeek/index.js create mode 100644 src/isThisWeek/index.js.flow create mode 100644 src/isThisWeek/test.js create mode 100644 src/isThisYear/benchmark.js create mode 100644 src/isThisYear/index.d.ts create mode 100644 src/isThisYear/index.js create mode 100644 src/isThisYear/index.js.flow create mode 100644 src/isThisYear/test.js create mode 100644 src/isToday/benchmark.js create mode 100644 src/isToday/index.d.ts create mode 100644 src/isToday/index.js create mode 100644 src/isToday/index.js.flow create mode 100644 src/isToday/test.js create mode 100644 src/isTomorrow/benchmark.js create mode 100644 src/isTomorrow/index.d.ts create mode 100644 src/isTomorrow/index.js create mode 100644 src/isTomorrow/index.js.flow create mode 100644 src/isTomorrow/test.js create mode 100644 src/isYesterday/benchmark.js create mode 100644 src/isYesterday/index.d.ts create mode 100644 src/isYesterday/index.js create mode 100644 src/isYesterday/index.js.flow create mode 100644 src/isYesterday/test.js create mode 100644 src/startOfToday/benchmark.js create mode 100644 src/startOfToday/index.d.ts create mode 100644 src/startOfToday/index.js create mode 100644 src/startOfToday/index.js.flow create mode 100644 src/startOfToday/test.js create mode 100644 src/startOfTomorrow/benchmark.js create mode 100644 src/startOfTomorrow/index.d.ts create mode 100644 src/startOfTomorrow/index.js create mode 100644 src/startOfTomorrow/index.js.flow create mode 100644 src/startOfTomorrow/test.js create mode 100644 src/startOfYesterday/benchmark.js create mode 100644 src/startOfYesterday/index.d.ts create mode 100644 src/startOfYesterday/index.js create mode 100644 src/startOfYesterday/index.js.flow create mode 100644 src/startOfYesterday/test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 78662c17a1..6f3bff62a5 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -263,67 +263,6 @@ for the list of changes made since `v2.0.0-alpha.1`. var maxDate = max(dates) ``` -- **BREAKING**: remove all functions that create the current date internally: - - - `distanceInWordsToNow` - - `isFuture` - - `isPast` - - `endOfToday` - - `endOfTomorrow` - - `endOfYesterday` - - `startOfToday` - - `startOfTomorrow` - - `startOfYesterday` - - `isToday` - - `isTomorrow` - - `isYesterday` - - `isThisSecond` - - `isThisMinute` - - `isThisHour` - - `isThisWeek` - - `isThisISOWeek` - - `isThisMonth` - - `isThisQuarter` - - `isThisYear` - - `isThisISOYear` - - These functions are not pure, cannot have FP-versions [#253](https://github.com/date-fns/date-fns/issues/253) - and would add extra code for UTC-versions [#376](https://github.com/date-fns/date-fns/issues/376). - - See issue: [#377](https://github.com/date-fns/date-fns/issues/377) - - ```javascript - // Before v2.0.0 - var result = endOfToday() - - // v2.0.0 onward - var result = endOfDay(new Date()) - ``` - - Upgrade guide: - - - `distanceInWordsToNow(date)` → `formatDistance(date, new Date())` - - `isFuture(date)` → `isAfter(date, new Date())` - - `isPast(date)` → `isBefore(date, new Date())` - - `endOfToday()` → `endOfDay(new Date())` - - `endOfTomorrow()` → `endOfDay(addDays(new Date(), 1))` - - `endOfYesterday()` → `endOfDay(subDays(new Date(), 1))` - - `startOfToday()` → `startOfDay(new Date())` - - `startOfTomorrow()` → `startOfDay(addDays(new Date(), 1))` - - `startOfYesterday()` → `startOfDay(subDays(new Date(), 1))` - - `isToday(date)` → `isSameDay(new Date(), date)` - - `isTomorrow(date)` → `isSameDay(date, addDays(new Date(), 1))` - - `isYesterday(date)` → `isSameDay(date, subDays(new Date(), 1))` - - `isThisSecond(date)` → `isSameSecond(date, new Date())` - - `isThisMinute(date)` → `isSameMinute(date, new Date())` - - `isThisHour(date)` → `isSameHour(date, new Date())` - - `isThisWeek(date)` → `isSameWeek(date, new Date())` - - `isThisISOWeek(date)` → `isSameISOWeek(date, new Date())` - - `isThisMonth(date)` → `isSameMonth(date, new Date())` - - `isThisQuarter(date)` → `isSameQuarter(date, new Date())` - - `isThisYear(date)` → `isSameYear(date, new Date())` - - `isThisISOYear(date)` → `isSameISOYear(date, new Date())` - - **BREAKING**: make the second argument of `format` required for the sake of explicitness. ```javascript @@ -423,6 +362,7 @@ for the list of changes made since `v2.0.0-alpha.1`. - `distanceInWords` → `formatDistance` - `distanceInWordsStrict` → `formatDistanceStrict` + - `distanceInWordsToNow` → `formatDistanceToNow` to make them consistent with `format` and `formatRelative`. diff --git a/scripts/build/_lib/prettier.js b/scripts/build/_lib/prettier.js index 2e551c6483..5ae6f5bed6 100644 --- a/scripts/build/_lib/prettier.js +++ b/scripts/build/_lib/prettier.js @@ -2,6 +2,6 @@ const prettier = require('prettier') const packageJSON = require('../../../package.json') const config = packageJSON.prettier -module.exports = (code, parser = 'babylon') => { +module.exports = (code, parser = 'babel') => { return prettier.format(code, Object.assign(config, { parser })) } diff --git a/scripts/build/_lib/typings/common.js b/scripts/build/_lib/typings/common.js index ba23f6ddc0..46b845fb43 100644 --- a/scripts/build/_lib/typings/common.js +++ b/scripts/build/_lib/typings/common.js @@ -1,26 +1,29 @@ -const { - addSeparator, - formatBlock -} = require('./formatBlock') +const { addSeparator, formatBlock } = require('./formatBlock') const lowerCaseTypes = ['String', 'Number', 'Boolean'] -function correctTypeCase (type) { +function correctTypeCase(type) { if (lowerCaseTypes.includes(type)) { return type.toLowerCase() } return type } -function getParams (params, {leftBorder = '{', rightBorder = '}'} = {}) { - if (params.length === 0) { +function getParams(params, { leftBorder = '{', rightBorder = '}' } = {}) { + if (!params || params.length === 0) { return leftBorder + rightBorder } const formattedParams = addSeparator( params.map(param => { - const {name, props, optional, variable, type: {names: typeNames}} = param - const type = getType(typeNames, {props, forceArray: variable}) + const { + name, + props, + optional, + variable, + type: { names: typeNames } + } = param + const type = getType(typeNames, { props, forceArray: variable }) return `${variable ? '...' : ''}${name}${optional ? '?' : ''}: ${type}` }), ',' @@ -33,7 +36,7 @@ function getParams (params, {leftBorder = '{', rightBorder = '}'} = {}) { ` } -function getType (types, {props = [], forceArray = false} = {}) { +function getType(types, { props = [], forceArray = false } = {}) { const typeStrings = types.map(type => { if (type === '*') { return 'any' @@ -60,7 +63,8 @@ function getType (types, {props = [], forceArray = false} = {}) { return caseCorrectedType }) - const allArrayTypes = typeStrings.length > 1 && typeStrings.every(type => type.endsWith('[]')) + const allArrayTypes = + typeStrings.length > 1 && typeStrings.every(type => type.endsWith('[]')) if (allArrayTypes) { return `(${typeStrings.map(type => type.replace('[]', '')).join(' | ')})[]` } @@ -68,9 +72,8 @@ function getType (types, {props = [], forceArray = false} = {}) { return typeStrings.join(' | ') } -function getFPFnType (params, returns) { - const fpParams = params - .map(param => param.type.names) +function getFPFnType(params, returns) { + const fpParams = params.map(param => param.type.names) const arity = fpParams.length diff --git a/scripts/build/docs.js b/scripts/build/docs.js index 56807069e3..e487b03684 100755 --- a/scripts/build/docs.js +++ b/scripts/build/docs.js @@ -36,21 +36,32 @@ function generateDocsFromSource() { 'no-cache': true })[0] ) - .map(doc => ({ - type: 'jsdoc', - kind: 'function', - urlId: doc.name, - category: doc.category, - title: doc.name, - description: doc.summary, - content: doc - })) + .map(doc => { + const pureTag = + doc.customTags && doc.customTags.find(t => t.tag === 'pure') + const pure = (pureTag && pureTag.value) !== 'false' + return { + type: 'jsdoc', + kind: 'function', + urlId: doc.name, + category: doc.category, + title: doc.name, + description: doc.summary, + content: doc, + pure + } + }) .reduce( (array, doc) => array .concat(generateFnDoc(doc)) - .concat(generateFPFnDoc(doc)) - .concat(generateFPFnWithOptionsDoc(doc) || []), + .concat( + doc.pure + ? [generateFPFnDoc(doc)].concat( + generateFPFnWithOptionsDoc(doc) || [] + ) + : [] + ), [] ) @@ -270,7 +281,7 @@ function generateFPFnWithOptionsDoc(dirtyDoc) { } function withOptions(args) { - return args[0].name === 'options' + return args && args[0].name === 'options' } function generateUsageTabs(isFPFn) { @@ -342,7 +353,9 @@ function paramsToTree(dirtyParams) { } function generateSyntaxString(name, args, isFPFn) { - if (isFPFn) { + if (!args) { + return undefined + } else if (isFPFn) { return args.reduce((acc, arg) => acc.concat(`(${arg.name})`), name) } else { const argsString = args diff --git a/scripts/test/node.sh b/scripts/test/node.sh index a68ea92ecb..182b72e566 100755 --- a/scripts/test/node.sh +++ b/scripts/test/node.sh @@ -4,8 +4,6 @@ # # It's a part of the test process. -set -ex - export PATH="$(yarn bin):$PATH" # Update and source nvm @@ -17,5 +15,5 @@ do echo "Running tests using Node.js $version" nvm install $version npm rebuild - jest + jest || exit 1 done \ No newline at end of file diff --git a/src/endOfToday/benchmark.js b/src/endOfToday/benchmark.js new file mode 100644 index 0000000000..515c52860d --- /dev/null +++ b/src/endOfToday/benchmark.js @@ -0,0 +1,9 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import endOfToday from '.' + +suite('endOfToday', () => { + benchmark('date-fns', () => endOfToday()) +}) diff --git a/src/endOfToday/index.d.ts b/src/endOfToday/index.d.ts new file mode 100644 index 0000000000..30de098148 --- /dev/null +++ b/src/endOfToday/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { endOfToday } from 'date-fns' +export default endOfToday diff --git a/src/endOfToday/index.js b/src/endOfToday/index.js new file mode 100644 index 0000000000..cb3d837fe5 --- /dev/null +++ b/src/endOfToday/index.js @@ -0,0 +1,28 @@ +import endOfDay from '../endOfDay/index.js' + +/** + * @name endOfToday + * @category Day Helpers + * @summary Return the end of today. + * @pure false + * + * @description + * Return the end of today. + * + * > ⚠️ 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). + * + * @returns {Date} the end of today + * + * @example + * // If today is 6 October 2014: + * var result = endOfToday() + * //=> Mon Oct 6 2014 23:59:59.999 + */ +export default function endOfToday() { + return endOfDay(Date.now()) +} diff --git a/src/endOfToday/index.js.flow b/src/endOfToday/index.js.flow new file mode 100644 index 0000000000..504b9f5cc7 --- /dev/null +++ b/src/endOfToday/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: () => Date diff --git a/src/endOfToday/test.js b/src/endOfToday/test.js new file mode 100644 index 0000000000..769c4cd46b --- /dev/null +++ b/src/endOfToday/test.js @@ -0,0 +1,24 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import endOfToday from '.' + +describe('endOfToday', () => { + let clock + beforeEach(() => { + clock = sinon.useFakeTimers( + new Date(2014, 8 /* Sep */, 25, 14, 30, 45, 500).getTime() + ) + }) + + afterEach(() => { + clock.restore() + }) + + it('returns the current date with the time setted to 23:59:59.999', () => { + const result = endOfToday() + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 25, 23, 59, 59, 999)) + }) +}) diff --git a/src/endOfTomorrow/benchmark.js b/src/endOfTomorrow/benchmark.js new file mode 100644 index 0000000000..c2b7e6b544 --- /dev/null +++ b/src/endOfTomorrow/benchmark.js @@ -0,0 +1,9 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import endOfTomorrow from '.' + +suite('endOfTomorrow', () => { + benchmark('date-fns', () => endOfTomorrow()) +}) diff --git a/src/endOfTomorrow/index.d.ts b/src/endOfTomorrow/index.d.ts new file mode 100644 index 0000000000..df5619e353 --- /dev/null +++ b/src/endOfTomorrow/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { endOfTomorrow } from 'date-fns' +export default endOfTomorrow diff --git a/src/endOfTomorrow/index.js b/src/endOfTomorrow/index.js new file mode 100644 index 0000000000..3d8adebc3d --- /dev/null +++ b/src/endOfTomorrow/index.js @@ -0,0 +1,34 @@ +/** + * @name endOfTomorrow + * @category Day Helpers + * @summary Return the end of tomorrow. + * @pure false + * + * @description + * Return the end of tomorrow. + * + * > ⚠️ 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). + * + * @returns {Date} the end of tomorrow + * + * @example + * // If today is 6 October 2014: + * var result = endOfTomorrow() + * //=> Tue Oct 7 2014 23:59:59.999 + */ +export default function endOfTomorrow() { + var now = new Date() + var year = now.getFullYear() + var month = now.getMonth() + var day = now.getDate() + + var date = new Date(0) + date.setFullYear(year, month, day + 1) + date.setHours(23, 59, 59, 999) + return date +} diff --git a/src/endOfTomorrow/index.js.flow b/src/endOfTomorrow/index.js.flow new file mode 100644 index 0000000000..504b9f5cc7 --- /dev/null +++ b/src/endOfTomorrow/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: () => Date diff --git a/src/endOfTomorrow/test.js b/src/endOfTomorrow/test.js new file mode 100644 index 0000000000..04972eb713 --- /dev/null +++ b/src/endOfTomorrow/test.js @@ -0,0 +1,34 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import endOfTomorrow from '.' + +describe('endOfTomorrow', function() { + it('returns tomorrow with the time setted to 23:59:59.999', function() { + const clock = sinon.useFakeTimers( + new Date(2014, 8 /* Sep */, 25, 14, 30, 45, 500).getTime() + ) + + const result = endOfTomorrow() + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 26, 23, 59, 59, 999)) + + clock.restore() + }) + + it('handles dates before 100 AD', function() { + const now = new Date(0) + now.setFullYear(14, 8 /* Sep */, 25) + now.setHours(14, 30, 45, 500) + const clock = sinon.useFakeTimers(now.getTime()) + + const expectedResult = new Date(0) + expectedResult.setFullYear(14, 8 /* Sep */, 26) + expectedResult.setHours(23, 59, 59, 999) + const result = endOfTomorrow() + assert.deepEqual(result, expectedResult) + + clock.restore() + }) +}) diff --git a/src/endOfYesterday/benchmark.js b/src/endOfYesterday/benchmark.js new file mode 100644 index 0000000000..e3681e56ad --- /dev/null +++ b/src/endOfYesterday/benchmark.js @@ -0,0 +1,9 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import endOfYesterday from '.' + +suite('endOfYesterday', () => { + benchmark('date-fns', () => endOfYesterday()) +}) diff --git a/src/endOfYesterday/index.d.ts b/src/endOfYesterday/index.d.ts new file mode 100644 index 0000000000..b06935b6fb --- /dev/null +++ b/src/endOfYesterday/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { endOfYesterday } from 'date-fns' +export default endOfYesterday diff --git a/src/endOfYesterday/index.js b/src/endOfYesterday/index.js new file mode 100644 index 0000000000..37aeb8cb16 --- /dev/null +++ b/src/endOfYesterday/index.js @@ -0,0 +1,34 @@ +/** + * @name endOfYesterday + * @category Day Helpers + * @summary Return the end of yesterday. + * @pure false + * + * @description + * Return the end of yesterday. + * + * > ⚠️ 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). + * + * @returns {Date} the end of yesterday + * + * @example + * // If today is 6 October 2014: + * var result = endOfYesterday() + * //=> Sun Oct 5 2014 23:59:59.999 + */ +export default function endOfYesterday() { + var now = new Date() + var year = now.getFullYear() + var month = now.getMonth() + var day = now.getDate() + + var date = new Date(0) + date.setFullYear(year, month, day - 1) + date.setHours(23, 59, 59, 999) + return date +} diff --git a/src/endOfYesterday/index.js.flow b/src/endOfYesterday/index.js.flow new file mode 100644 index 0000000000..504b9f5cc7 --- /dev/null +++ b/src/endOfYesterday/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: () => Date diff --git a/src/endOfYesterday/test.js b/src/endOfYesterday/test.js new file mode 100644 index 0000000000..2a25c89561 --- /dev/null +++ b/src/endOfYesterday/test.js @@ -0,0 +1,34 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import endOfYesterday from '.' + +describe('endOfYesterday', () => { + it('returns yesterday with the time setted to 23:59:59.999', () => { + const clock = sinon.useFakeTimers( + new Date(2014, 8 /* Sep */, 25, 14, 30, 45, 500).getTime() + ) + + const result = endOfYesterday() + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 24, 23, 59, 59, 999)) + + clock.restore() + }) + + it('handles dates before 100 AD', () => { + const now = new Date(0) + now.setFullYear(14, 8 /* Sep */, 25) + now.setHours(14, 30, 45, 500) + const clock = sinon.useFakeTimers(now.getTime()) + + const expectedResult = new Date(0) + expectedResult.setFullYear(14, 8 /* Sep */, 24) + expectedResult.setHours(23, 59, 59, 999) + const result = endOfYesterday() + assert.deepEqual(result, expectedResult) + + clock.restore() + }) +}) diff --git a/src/esm/index.js b/src/esm/index.js index 837ff003ca..13cd41bc58 100644 --- a/src/esm/index.js +++ b/src/esm/index.js @@ -74,13 +74,17 @@ export { default as endOfMinute } from './endOfMinute/index.js' export { default as endOfMonth } from './endOfMonth/index.js' export { default as endOfQuarter } from './endOfQuarter/index.js' export { default as endOfSecond } from './endOfSecond/index.js' +export { default as endOfToday } from './endOfToday/index.js' +export { default as endOfTomorrow } from './endOfTomorrow/index.js' export { default as endOfWeek } from './endOfWeek/index.js' export { default as endOfYear } from './endOfYear/index.js' +export { default as endOfYesterday } from './endOfYesterday/index.js' export { default as format } from './format/index.js' export { default as formatDistance } from './formatDistance/index.js' export { default as formatDistanceStrict } from './formatDistanceStrict/index.js' +export { default as formatDistanceToNow } from './formatDistanceToNow/index.js' export { default as formatRelative } from './formatRelative/index.js' export { default as fromUnixTime } from './fromUnixTime/index.js' export { default as getDate } from './getDate/index.js' @@ -115,9 +119,11 @@ export { default as isDate } from './isDate/index.js' export { default as isEqual } from './isEqual/index.js' export { default as isFirstDayOfMonth } from './isFirstDayOfMonth/index.js' export { default as isFriday } from './isFriday/index.js' +export { default as isFuture } from './isFuture/index.js' export { default as isLastDayOfMonth } from './isLastDayOfMonth/index.js' export { default as isLeapYear } from './isLeapYear/index.js' export { default as isMonday } from './isMonday/index.js' +export { default as isPast } from './isPast/index.js' export { default as isSameDay } from './isSameDay/index.js' export { default as isSameHour } from './isSameHour/index.js' export { default as isSameISOWeek } from './isSameISOWeek/index.js' @@ -130,12 +136,23 @@ export { default as isSameWeek } from './isSameWeek/index.js' export { default as isSameYear } from './isSameYear/index.js' export { default as isSaturday } from './isSaturday/index.js' export { default as isSunday } from './isSunday/index.js' +export { default as isThisHour } from './isThisHour/index.js' +export { default as isThisISOWeek } from './isThisISOWeek/index.js' +export { default as isThisMinute } from './isThisMinute/index.js' +export { default as isThisMonth } from './isThisMonth/index.js' +export { default as isThisQuarter } from './isThisQuarter/index.js' +export { default as isThisSecond } from './isThisSecond/index.js' +export { default as isThisWeek } from './isThisWeek/index.js' +export { default as isThisYear } from './isThisYear/index.js' export { default as isThursday } from './isThursday/index.js' +export { default as isToday } from './isToday/index.js' +export { default as isTomorrow } from './isTomorrow/index.js' export { default as isTuesday } from './isTuesday/index.js' export { default as isValid } from './isValid/index.js' export { default as isWednesday } from './isWednesday/index.js' export { default as isWeekend } from './isWeekend/index.js' export { default as isWithinInterval } from './isWithinInterval/index.js' +export { default as isYesterday } from './isYesterday/index.js' export { default as lastDayOfDecade } from './lastDayOfDecade/index.js' export { default as lastDayOfISOWeek } from './lastDayOfISOWeek/index.js' export { @@ -177,9 +194,12 @@ export { default as startOfMinute } from './startOfMinute/index.js' export { default as startOfMonth } from './startOfMonth/index.js' export { default as startOfQuarter } from './startOfQuarter/index.js' export { default as startOfSecond } from './startOfSecond/index.js' +export { default as startOfToday } from './startOfToday/index.js' +export { default as startOfTomorrow } from './startOfTomorrow/index.js' export { default as startOfWeek } from './startOfWeek/index.js' export { default as startOfWeekYear } from './startOfWeekYear/index.js' export { default as startOfYear } from './startOfYear/index.js' +export { default as startOfYesterday } from './startOfYesterday/index.js' export { default as subDays } from './subDays/index.js' export { default as subHours } from './subHours/index.js' export { default as subISOWeekYears } from './subISOWeekYears/index.js' diff --git a/src/formatDistance/index.js b/src/formatDistance/index.js index ab6418ac69..4d106cfc22 100644 --- a/src/formatDistance/index.js +++ b/src/formatDistance/index.js @@ -1,10 +1,10 @@ -import getTimezoneOffsetInMilliseconds from '../_lib/getTimezoneOffsetInMilliseconds/index.js' import compareAsc from '../compareAsc/index.js' -import toDate from '../toDate/index.js' -import differenceInSeconds from '../differenceInSeconds/index.js' import differenceInMonths from '../differenceInMonths/index.js' -import cloneObject from '../_lib/cloneObject/index.js' +import differenceInSeconds from '../differenceInSeconds/index.js' import defaultLocale from '../locale/en-US/index.js' +import toDate from '../toDate/index.js' +import cloneObject from '../_lib/cloneObject/index.js' +import getTimezoneOffsetInMilliseconds from '../_lib/getTimezoneOffsetInMilliseconds/index.js' var MINUTES_IN_DAY = 1440 var MINUTES_IN_ALMOST_TWO_DAYS = 2520 @@ -52,7 +52,7 @@ var MINUTES_IN_TWO_MONTHS = 86400 * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * - * - The function was renamed from `distanceInWords ` to `formatDistance ` + * - The function was renamed from `distanceInWords ` to `formatDistance` * to make its name consistent with `format` and `formatRelative`. * * - The order of arguments is swapped to make the function diff --git a/src/formatDistanceToNow/benchmark.js b/src/formatDistanceToNow/benchmark.js new file mode 100644 index 0000000000..8a6f444d28 --- /dev/null +++ b/src/formatDistanceToNow/benchmark.js @@ -0,0 +1,25 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import moment from 'moment' +import formatDistanceToNow from '.' + +suite( + 'formatDistanceToNow', + () => { + benchmark('date-fns', function() { + return formatDistanceToNow(this.date) + }) + + benchmark('Moment.js', function() { + return this.moment.toNow() + }) + }, + { + setup: function() { + this.date = new Date() + this.moment = moment() + } + } +) diff --git a/src/formatDistanceToNow/index.d.ts b/src/formatDistanceToNow/index.d.ts new file mode 100644 index 0000000000..1d8b75017d --- /dev/null +++ b/src/formatDistanceToNow/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { formatDistanceToNow } from 'date-fns' +export default formatDistanceToNow diff --git a/src/formatDistanceToNow/index.js b/src/formatDistanceToNow/index.js new file mode 100644 index 0000000000..b2ecf4da12 --- /dev/null +++ b/src/formatDistanceToNow/index.js @@ -0,0 +1,115 @@ +import distanceInWords from '../formatDistance/index.js' + +/** + * @name formatDistanceToNow + * @category Common Helpers + * @summary Return the distance between the given date and now in words. + * @pure false + * + * @description + * Return the distance between the given date and now in words. + * + * | Distance to now | Result | + * |-------------------------------------------------------------------|---------------------| + * | 0 ... 30 secs | less than a minute | + * | 30 secs ... 1 min 30 secs | 1 minute | + * | 1 min 30 secs ... 44 mins 30 secs | [2..44] minutes | + * | 44 mins ... 30 secs ... 89 mins 30 secs | about 1 hour | + * | 89 mins 30 secs ... 23 hrs 59 mins 30 secs | about [2..24] hours | + * | 23 hrs 59 mins 30 secs ... 41 hrs 59 mins 30 secs | 1 day | + * | 41 hrs 59 mins 30 secs ... 29 days 23 hrs 59 mins 30 secs | [2..30] days | + * | 29 days 23 hrs 59 mins 30 secs ... 44 days 23 hrs 59 mins 30 secs | about 1 month | + * | 44 days 23 hrs 59 mins 30 secs ... 59 days 23 hrs 59 mins 30 secs | about 2 months | + * | 59 days 23 hrs 59 mins 30 secs ... 1 yr | [2..12] months | + * | 1 yr ... 1 yr 3 months | about 1 year | + * | 1 yr 3 months ... 1 yr 9 month s | over 1 year | + * | 1 yr 9 months ... 2 yrs | almost 2 years | + * | N yrs ... N yrs 3 months | about N years | + * | N yrs 3 months ... N yrs 9 months | over N years | + * | N yrs 9 months ... N+1 yrs | almost N+1 years | + * + * With `options.includeSeconds == true`: + * | Distance to now | Result | + * |---------------------|----------------------| + * | 0 secs ... 5 secs | less than 5 seconds | + * | 5 secs ... 10 secs | less than 10 seconds | + * | 10 secs ... 20 secs | less than 20 seconds | + * | 20 secs ... 40 secs | half a minute | + * | 40 secs ... 60 secs | less than a minute | + * | 60 secs ... 90 secs | 1 minute | + * + * > ⚠️ 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). + * + * - The function was renamed from `distanceInWordsToNow ` to `formatDistanceToNow` + * to make its name consistent with `format` and `formatRelative`. + * + * ```javascript + * // Before v2.0.0 + * + * distanceInWordsToNow(new Date(2014, 6, 2), { addSuffix: true }) + * //=> 'in 6 months' + * + * // v2.0.0 onward + * + * formatDistanceToNow(new Date(2014, 6, 2), { addSuffix: true }) + * //=> 'in 6 months' + * ``` + * + * @param {Date|Number} date - the given date + * @param {Object} [options] - the object with options + * @param {Boolean} [options.includeSeconds=false] - distances less than a minute are more detailed + * @param {Boolean} [options.addSuffix=false] - result specifies if now is earlier or later than the passed date + * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale} + * @returns {String} the distance in words + * @throws {TypeError} 1 argument required + * @throws {RangeError} `options.locale` must contain `formatDistance` property + * + * @example + * // If today is 1 January 2015, what is the distance to 2 July 2014? + * var result = formatDistanceToNow( + * new Date(2014, 6, 2) + * ) + * //=> '6 months' + * + * @example + * // If now is 1 January 2015 00:00:00, + * // what is the distance to 1 January 2015 00:00:15, including seconds? + * var result = formatDistanceToNow( + * new Date(2015, 0, 1, 0, 0, 15), + * {includeSeconds: true} + * ) + * //=> 'less than 20 seconds' + * + * @example + * // If today is 1 January 2015, + * // what is the distance to 1 January 2016, with a suffix? + * var result = formatDistanceToNow( + * new Date(2016, 0, 1), + * {addSuffix: true} + * ) + * //=> 'in about 1 year' + * + * @example + * // If today is 1 January 2015, + * // what is the distance to 1 August 2016 in Esperanto? + * var eoLocale = require('date-fns/locale/eo') + * var result = formatDistanceToNow( + * new Date(2016, 7, 1), + * {locale: eoLocale} + * ) + * //=> 'pli ol 1 jaro' + */ +export default function formatDistanceToNow(dirtyDate, dirtyOptions) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return distanceInWords(dirtyDate, Date.now(), dirtyOptions) +} diff --git a/src/formatDistanceToNow/index.js.flow b/src/formatDistanceToNow/index.js.flow new file mode 100644 index 0000000000..cea3084fe4 --- /dev/null +++ b/src/formatDistanceToNow/index.js.flow @@ -0,0 +1,45 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: ( + date: Date | number, + options?: { + includeSeconds?: boolean, + addSuffix?: boolean, + locale?: Locale + } +) => string diff --git a/src/formatDistanceToNow/test.js b/src/formatDistanceToNow/test.js new file mode 100644 index 0000000000..f98f9bf21a --- /dev/null +++ b/src/formatDistanceToNow/test.js @@ -0,0 +1,232 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import formatDistanceToNow from '.' + +describe('formatDistanceToNow', () => { + let clock + beforeEach(() => { + clock = sinon.useFakeTimers(new Date(1986, 3, 4, 10, 32, 0).getTime()) + }) + + afterEach(() => { + clock.restore() + }) + + describe('seconds', () => { + context('when the includeSeconds option is true', () => { + it('less than 5 seconds', () => { + const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 58), { + includeSeconds: true + }) + assert(result === 'less than 5 seconds') + }) + + it('less than 10 seconds', () => { + const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 52), { + includeSeconds: true + }) + assert(result === 'less than 10 seconds') + }) + + it('less than 20 seconds', () => { + const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 45), { + includeSeconds: true + }) + assert(result === 'less than 20 seconds') + }) + + it('half a minute', () => { + const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 35), { + includeSeconds: true + }) + assert(result === 'half a minute') + }) + + it('less than a minute', () => { + const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 15), { + includeSeconds: true + }) + assert(result === 'less than a minute') + }) + + it('1 minute', () => { + const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 0), { + includeSeconds: true + }) + assert(result === '1 minute') + }) + }) + }) + + describe('minutes', () => { + it('less than a minute', () => { + const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 40)) + assert(result === 'less than a minute') + }) + + it('1 minute', () => { + const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 10)) + assert(result === '1 minute') + }) + + it('n minutes', () => { + const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 29, 10)) + assert(result === '3 minutes') + }) + }) + + describe('hours', () => { + it('about 1 hour', () => { + const result = formatDistanceToNow(new Date(1986, 3, 4, 9, 32, 0)) + assert(result === 'about 1 hour') + }) + + it('about n hours', () => { + const result = formatDistanceToNow(new Date(1986, 3, 4, 7, 32, 0)) + assert(result === 'about 3 hours') + }) + }) + + describe('days', () => { + it('1 day', () => { + const result = formatDistanceToNow(new Date(1986, 3, 3, 10, 32, 0)) + assert(result === '1 day') + }) + + it('n days', () => { + const result = formatDistanceToNow(new Date(1986, 3, 1, 10, 32, 0)) + assert(result === '3 days') + }) + }) + + describe('months', () => { + it('about 1 month', () => { + const result = formatDistanceToNow(new Date(1986, 2, 4, 10, 32, 0)) + assert(result === 'about 1 month') + }) + + it('n months', () => { + const result = formatDistanceToNow(new Date(1986, 0, 4, 10, 32, 0)) + assert(result === '3 months') + }) + }) + + describe('years', () => { + it('about 1 year', () => { + const result = formatDistanceToNow(new Date(1985, 3, 4, 10, 32, 0)) + assert(result === 'about 1 year') + }) + + it('over 1 year', () => { + const result = formatDistanceToNow(new Date(1984, 10, 4, 10, 32, 0)) + assert(result === 'over 1 year') + }) + + it('almost n years', () => { + const result = formatDistanceToNow(new Date(1983, 4, 4, 10, 32, 0)) + assert(result === 'almost 3 years') + }) + + it('about n years', () => { + const result = formatDistanceToNow(new Date(1983, 3, 4, 10, 32, 0)) + assert(result === 'about 3 years') + }) + + it('over n years', () => { + const result = formatDistanceToNow(new Date(1982, 10, 4, 10, 32, 0)) + assert(result === 'over 3 years') + }) + }) + + it('accepts a timestamp', () => { + const result = formatDistanceToNow( + new Date(1986, 3, 4, 10, 31, 40).getTime() + ) + assert(result === 'less than a minute') + }) + + describe('when the addSuffix option is true', () => { + it('adds a past suffix', () => { + const result = formatDistanceToNow(new Date(1986, 3, 4, 10, 31, 35), { + includeSeconds: true, + addSuffix: true + }) + assert(result === 'half a minute ago') + }) + + it('adds a future suffix', () => { + const result = formatDistanceToNow(new Date(1986, 3, 4, 11, 32, 0), { + addSuffix: true + }) + assert(result === 'in about 1 hour') + }) + }) + + describe('implicit conversion of options', () => { + it('`options.includeSeconds`', () => { + const result = formatDistanceToNow( + new Date(1986, 3, 4, 10, 31, 52), + // $ExpectedMistake + { includeSeconds: 1 } + ) + assert(result === 'less than 10 seconds') + }) + + it('`options.addSuffix`', () => { + const result = formatDistanceToNow( + new Date(1986, 3, 4, 11, 32, 0), + // $ExpectedMistake + { addSuffix: 1 } + ) + assert(result === 'in about 1 hour') + }) + }) + + describe('custom locale', () => { + it('can be passed to the function', () => { + function localizeDistance(token, count, options) { + assert(token === 'aboutXHours') + assert(count === 1) + assert(options.addSuffix === true) + assert(options.comparison > 0) + return 'It works!' + } + + const customLocale = { + formatDistance: localizeDistance + } + + const result = formatDistanceToNow(new Date(1986, 3, 4, 11, 32, 0), { + addSuffix: true, + // $ExpectedMistake + locale: customLocale + }) + + assert(result === 'It works!') + }) + + context('does not contain `distanceInWords` property', () => { + it('throws `RangeError`', function() { + const customLocale = {} + const block = formatDistanceToNow.bind( + null, + // $ExpectedMistake + new Date(1986, 3, 4, 10, 32, 0), + { includeSeconds: true, locale: customLocale } + ) + assert.throws(block, RangeError) + }) + }) + }) + + it('throws RangeError if the passed date is `Invalid Date`', function() { + assert.throws(formatDistanceToNow.bind(null, new Date(NaN)), RangeError) + }) + + it('throws TypeError exception if passed less than 1 argument', () => { + assert.throws(formatDistanceToNow.bind(null), TypeError) + }) +}) diff --git a/src/index.js b/src/index.js index 14756b4579..49caa264ab 100644 --- a/src/index.js +++ b/src/index.js @@ -51,11 +51,15 @@ module.exports = { endOfMonth: require('./endOfMonth/index.js'), endOfQuarter: require('./endOfQuarter/index.js'), endOfSecond: require('./endOfSecond/index.js'), + endOfToday: require('./endOfToday/index.js'), + endOfTomorrow: require('./endOfTomorrow/index.js'), endOfWeek: require('./endOfWeek/index.js'), endOfYear: require('./endOfYear/index.js'), + endOfYesterday: require('./endOfYesterday/index.js'), format: require('./format/index.js'), formatDistance: require('./formatDistance/index.js'), formatDistanceStrict: require('./formatDistanceStrict/index.js'), + formatDistanceToNow: require('./formatDistanceToNow/index.js'), formatRelative: require('./formatRelative/index.js'), fromUnixTime: require('./fromUnixTime/index.js'), getDate: require('./getDate/index.js'), @@ -88,9 +92,11 @@ module.exports = { isEqual: require('./isEqual/index.js'), isFirstDayOfMonth: require('./isFirstDayOfMonth/index.js'), isFriday: require('./isFriday/index.js'), + isFuture: require('./isFuture/index.js'), isLastDayOfMonth: require('./isLastDayOfMonth/index.js'), isLeapYear: require('./isLeapYear/index.js'), isMonday: require('./isMonday/index.js'), + isPast: require('./isPast/index.js'), isSameDay: require('./isSameDay/index.js'), isSameHour: require('./isSameHour/index.js'), isSameISOWeek: require('./isSameISOWeek/index.js'), @@ -103,12 +109,23 @@ module.exports = { isSameYear: require('./isSameYear/index.js'), isSaturday: require('./isSaturday/index.js'), isSunday: require('./isSunday/index.js'), + isThisHour: require('./isThisHour/index.js'), + isThisISOWeek: require('./isThisISOWeek/index.js'), + isThisMinute: require('./isThisMinute/index.js'), + isThisMonth: require('./isThisMonth/index.js'), + isThisQuarter: require('./isThisQuarter/index.js'), + isThisSecond: require('./isThisSecond/index.js'), + isThisWeek: require('./isThisWeek/index.js'), + isThisYear: require('./isThisYear/index.js'), isThursday: require('./isThursday/index.js'), + isToday: require('./isToday/index.js'), + isTomorrow: require('./isTomorrow/index.js'), isTuesday: require('./isTuesday/index.js'), isValid: require('./isValid/index.js'), isWednesday: require('./isWednesday/index.js'), isWeekend: require('./isWeekend/index.js'), isWithinInterval: require('./isWithinInterval/index.js'), + isYesterday: require('./isYesterday/index.js'), lastDayOfDecade: require('./lastDayOfDecade/index.js'), lastDayOfISOWeek: require('./lastDayOfISOWeek/index.js'), lastDayOfISOWeekYear: require('./lastDayOfISOWeekYear/index.js'), @@ -146,9 +163,12 @@ module.exports = { startOfMonth: require('./startOfMonth/index.js'), startOfQuarter: require('./startOfQuarter/index.js'), startOfSecond: require('./startOfSecond/index.js'), + startOfToday: require('./startOfToday/index.js'), + startOfTomorrow: require('./startOfTomorrow/index.js'), startOfWeek: require('./startOfWeek/index.js'), startOfWeekYear: require('./startOfWeekYear/index.js'), startOfYear: require('./startOfYear/index.js'), + startOfYesterday: require('./startOfYesterday/index.js'), subDays: require('./subDays/index.js'), subHours: require('./subHours/index.js'), subISOWeekYears: require('./subISOWeekYears/index.js'), diff --git a/src/index.js.flow b/src/index.js.flow index 12ef492dd0..a8f7387b90 100644 --- a/src/index.js.flow +++ b/src/index.js.flow @@ -215,6 +215,10 @@ declare module.exports: { endOfSecond: (date: Date | number) => Date, + endOfToday: () => Date, + + endOfTomorrow: () => Date, + endOfWeek: ( date: Date | number, options?: { @@ -225,6 +229,8 @@ declare module.exports: { endOfYear: (date: Date | number) => Date, + endOfYesterday: () => Date, + format: ( date: Date | number, format: string, @@ -258,6 +264,15 @@ declare module.exports: { } ) => string, + formatDistanceToNow: ( + date: Date | number, + options?: { + includeSeconds?: boolean, + addSuffix?: boolean, + locale?: Locale + } + ) => string, + formatRelative: ( date: Date | number, baseDate: Date | number, @@ -358,12 +373,16 @@ declare module.exports: { isFriday: (date: Date | number) => boolean, + isFuture: (date: Date | number) => boolean, + isLastDayOfMonth: (date: Date | number) => boolean, isLeapYear: (date: Date | number) => boolean, isMonday: (date: Date | number) => boolean, + isPast: (date: Date | number) => boolean, + isSameDay: (dateLeft: Date | number, dateRight: Date | number) => boolean, isSameHour: (dateLeft: Date | number, dateRight: Date | number) => boolean, @@ -398,8 +417,34 @@ declare module.exports: { isSunday: (date: Date | number) => boolean, + isThisHour: (date: Date | number) => boolean, + + isThisISOWeek: (date: Date | number) => boolean, + + isThisMinute: (date: Date | number) => boolean, + + isThisMonth: (date: Date | number) => boolean, + + isThisQuarter: (date: Date | number) => boolean, + + isThisSecond: (date: Date | number) => boolean, + + isThisWeek: ( + date: Date | number, + options?: { + locale?: Locale, + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 + } + ) => boolean, + + isThisYear: (date: Date | number) => boolean, + isThursday: (date: Date | number) => boolean, + isToday: (date: Date | number) => boolean, + + isTomorrow: (date: Date | number) => boolean, + isTuesday: (date: Date | number) => boolean, isValid: (date: any) => boolean, @@ -410,6 +455,8 @@ declare module.exports: { isWithinInterval: (date: Date | number, interval: Interval) => boolean, + isYesterday: (date: Date | number) => boolean, + lastDayOfDecade: (date: Date | number) => Date, lastDayOfISOWeek: (date: Date | number) => Date, @@ -539,6 +586,10 @@ declare module.exports: { startOfSecond: (date: Date | number) => Date, + startOfToday: () => Date, + + startOfTomorrow: () => Date, + startOfWeek: ( date: Date | number, options?: { @@ -558,6 +609,8 @@ declare module.exports: { startOfYear: (date: Date | number) => Date, + startOfYesterday: () => Date, + subDays: (date: Date | number, amount: number) => Date, subHours: (date: Date | number, amount: number) => Date, diff --git a/src/isFuture/benchmark.js b/src/isFuture/benchmark.js new file mode 100644 index 0000000000..d04fd1a40a --- /dev/null +++ b/src/isFuture/benchmark.js @@ -0,0 +1,19 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import isFuture from '.' + +suite( + 'isFuture', + () => { + benchmark('date-fns', function() { + return isFuture(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/src/isFuture/index.d.ts b/src/isFuture/index.d.ts new file mode 100644 index 0000000000..4cb82e8add --- /dev/null +++ b/src/isFuture/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isFuture } from 'date-fns' +export default isFuture diff --git a/src/isFuture/index.js b/src/isFuture/index.js new file mode 100644 index 0000000000..cbbde9ee0b --- /dev/null +++ b/src/isFuture/index.js @@ -0,0 +1,36 @@ +import toDate from '../toDate/index.js' + +/** + * @name isFuture + * @category Common Helpers + * @summary Is the given date in the future? + * @pure false + * + * @description + * Is the given date in the future? + * + * > ⚠️ 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 + * @returns {Boolean} the date is in the future + * @throws {TypeError} 1 argument required + * + * @example + * // If today is 6 October 2014, is 31 December 2014 in the future? + * var result = isFuture(new Date(2014, 11, 31)) + * //=> true + */ +export default function isFuture(dirtyDate) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return toDate(dirtyDate).getTime() > Date.now() +} diff --git a/src/isFuture/index.js.flow b/src/isFuture/index.js.flow new file mode 100644 index 0000000000..dab9a538cf --- /dev/null +++ b/src/isFuture/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: (date: Date | number) => boolean diff --git a/src/isFuture/test.js b/src/isFuture/test.js new file mode 100644 index 0000000000..ee6fa9101e --- /dev/null +++ b/src/isFuture/test.js @@ -0,0 +1,41 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import sinon from 'sinon' +import isFuture from '.' + +describe('isFuture', () => { + let clock + beforeEach(() => { + clock = sinon.useFakeTimers(new Date(2014, 8 /* Sep */, 25).getTime()) + }) + + afterEach(() => { + clock.restore() + }) + + it('returns true if the given date is in the future', () => { + const result = isFuture(new Date(2014, 9 /* Oct */, 31)) + assert(result === true) + }) + + it('returns false if the given date is in the past', () => { + const result = isFuture(new Date(2014, 8 /* Sep */, 1)) + assert(result === false) + }) + + it('returns false if the given date is now', () => { + const result = isFuture(new Date(2014, 8 /* Sep */, 25)) + assert(result === false) + }) + + it('accepts a timestamp', () => { + const result = isFuture(new Date(2014, 9 /* Oct */, 31).getTime()) + assert(result === true) + }) + + it('throws TypeError exception if passed less than 1 argument', () => { + assert.throws(isFuture.bind(null), TypeError) + }) +}) diff --git a/src/isPast/benchmark.js b/src/isPast/benchmark.js new file mode 100644 index 0000000000..d5403bdaf2 --- /dev/null +++ b/src/isPast/benchmark.js @@ -0,0 +1,19 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import isPast from '.' + +suite( + 'isPast', + () => { + benchmark('date-fns', function() { + return isPast(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/src/isPast/index.d.ts b/src/isPast/index.d.ts new file mode 100644 index 0000000000..8ae92ca705 --- /dev/null +++ b/src/isPast/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isPast } from 'date-fns' +export default isPast diff --git a/src/isPast/index.js b/src/isPast/index.js new file mode 100644 index 0000000000..7b1083c181 --- /dev/null +++ b/src/isPast/index.js @@ -0,0 +1,36 @@ +import toDate from '../toDate/index.js' + +/** + * @name isPast + * @category Common Helpers + * @summary Is the given date in the past? + * @pure false + * + * @description + * Is the given date in the past? + * + * > ⚠️ 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 + * @returns {Boolean} the date is in the past + * @throws {TypeError} 1 argument required + * + * @example + * // If today is 6 October 2014, is 2 July 2014 in the past? + * var result = isPast(new Date(2014, 6, 2)) + * //=> true + */ +export default function isPast(dirtyDate) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return toDate(dirtyDate).getTime() < Date.now() +} diff --git a/src/isPast/index.js.flow b/src/isPast/index.js.flow new file mode 100644 index 0000000000..dab9a538cf --- /dev/null +++ b/src/isPast/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: (date: Date | number) => boolean diff --git a/src/isPast/test.js b/src/isPast/test.js new file mode 100644 index 0000000000..7d1db8c963 --- /dev/null +++ b/src/isPast/test.js @@ -0,0 +1,41 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isPast from '.' + +describe('isPast', () => { + let clock + beforeEach(() => { + clock = sinon.useFakeTimers(new Date(2014, 8 /* Sep */, 25).getTime()) + }) + + afterEach(() => { + clock.restore() + }) + + it('returns true if the given date is in the past', () => { + const result = isPast(new Date(2014, 6 /* Jul */, 2)) + assert(result === true) + }) + + it('returns false if the given date is in the future', () => { + const result = isPast(new Date(2014, 11 /* Dec */, 31)) + assert(result === false) + }) + + it('returns false if the given date is now', () => { + const result = isPast(new Date(2014, 8 /* Sep */, 25)) + assert(result === false) + }) + + it('accepts a timestamp', () => { + const result = isPast(new Date(2014, 6 /* Jul */, 2).getTime()) + assert(result === true) + }) + + it('throws TypeError exception if passed less than 1 argument', () => { + assert.throws(isPast.bind(null), TypeError) + }) +}) diff --git a/src/isThisHour/benchmark.js b/src/isThisHour/benchmark.js new file mode 100644 index 0000000000..6f52e5290f --- /dev/null +++ b/src/isThisHour/benchmark.js @@ -0,0 +1,20 @@ +// @flow +/* eslint-env mocha */ + +/* global suite, benchmark */ + +import isThisHour from '.' + +suite( + 'isThisHour', + () => { + benchmark('date-fns', function() { + return isThisHour(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/src/isThisHour/index.d.ts b/src/isThisHour/index.d.ts new file mode 100644 index 0000000000..1009e428d8 --- /dev/null +++ b/src/isThisHour/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isThisHour } from 'date-fns' +export default isThisHour diff --git a/src/isThisHour/index.js b/src/isThisHour/index.js new file mode 100644 index 0000000000..db59c7d718 --- /dev/null +++ b/src/isThisHour/index.js @@ -0,0 +1,37 @@ +import isSameHour from '../isSameHour/index.js' + +/** + * @name isThisHour + * @category Hour Helpers + * @summary Is the given date in the same hour as the current date? + * @pure false + * + * @description + * Is the given date in the same hour 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 + * @returns {Boolean} the date is in this hour + * @throws {TypeError} 1 argument required + * + * @example + * // If now is 25 September 2014 18:30:15.500, + * // is 25 September 2014 18:00:00 in this hour? + * var result = isThisHour(new Date(2014, 8, 25, 18)) + * //=> true + */ +export default function isThisHour(dirtyDate) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return isSameHour(Date.now(), dirtyDate) +} diff --git a/src/isThisHour/index.js.flow b/src/isThisHour/index.js.flow new file mode 100644 index 0000000000..dab9a538cf --- /dev/null +++ b/src/isThisHour/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: (date: Date | number) => boolean diff --git a/src/isThisHour/test.js b/src/isThisHour/test.js new file mode 100644 index 0000000000..0a034e6b83 --- /dev/null +++ b/src/isThisHour/test.js @@ -0,0 +1,38 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isThisHour from '.' + +describe('isThisHour', () => { + let clock + beforeEach(() => { + clock = sinon.useFakeTimers( + new Date(2014, 8 /* Sep */, 25, 18, 30, 15, 500).getTime() + ) + }) + + afterEach(() => { + clock.restore() + }) + + it('returns true if the given date and the current date have the same hour', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18) + assert(isThisHour(date) === true) + }) + + it('returns false if the given date and the current date have different hours', () => { + const date = new Date(2014, 8 /* Sep */, 25, 19) + assert(isThisHour(date) === false) + }) + + it('accepts a timestamp', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18, 45).getTime() + assert(isThisHour(date) === true) + }) + + it('throws TypeError exception if passed less than 1 argument', () => { + assert.throws(isThisHour.bind(null), TypeError) + }) +}) diff --git a/src/isThisISOWeek/benchmark.js b/src/isThisISOWeek/benchmark.js new file mode 100644 index 0000000000..d036d611c1 --- /dev/null +++ b/src/isThisISOWeek/benchmark.js @@ -0,0 +1,20 @@ +// @flow +/* eslint-env mocha */ + +/* global suite, benchmark */ + +import isThisISOWeek from '.' + +suite( + 'isThisISOWeek', + () => { + benchmark('date-fns', function() { + return isThisISOWeek(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/src/isThisISOWeek/index.d.ts b/src/isThisISOWeek/index.d.ts new file mode 100644 index 0000000000..4fb42e8ded --- /dev/null +++ b/src/isThisISOWeek/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isThisISOWeek } from 'date-fns' +export default isThisISOWeek diff --git a/src/isThisISOWeek/index.js b/src/isThisISOWeek/index.js new file mode 100644 index 0000000000..bfdb12a031 --- /dev/null +++ b/src/isThisISOWeek/index.js @@ -0,0 +1,39 @@ +import isSameISOWeek from '../isSameISOWeek/index.js' + +/** + * @name isThisISOWeek + * @category ISO Week Helpers + * @summary Is the given date in the same ISO week as the current date? + * @pure false + * + * @description + * Is the given date in the same ISO week as the current date? + * + * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_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 + * @returns {Boolean} the date is in this ISO week + * @throws {TypeError} 1 argument required + * + * @example + * // If today is 25 September 2014, is 22 September 2014 in this ISO week? + * var result = isThisISOWeek(new Date(2014, 8, 22)) + * //=> true + */ + +export default function isThisISOWeek(dirtyDate) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return isSameISOWeek(dirtyDate, Date.now()) +} diff --git a/src/isThisISOWeek/index.js.flow b/src/isThisISOWeek/index.js.flow new file mode 100644 index 0000000000..dab9a538cf --- /dev/null +++ b/src/isThisISOWeek/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: (date: Date | number) => boolean diff --git a/src/isThisISOWeek/test.js b/src/isThisISOWeek/test.js new file mode 100644 index 0000000000..0fe319e6b0 --- /dev/null +++ b/src/isThisISOWeek/test.js @@ -0,0 +1,36 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isThisISOWeek from '.' + +describe('isSameISOWeek', () => { + let clock + 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 ISO week', () => { + const date = new Date(2014, 8 /* Sep */, 22) + assert(isThisISOWeek(date) === true) + }) + + it('returns false if the given date and the current date have different ISO weeks', () => { + const date = new Date(2014, 8 /* Sep */, 21) + assert(isThisISOWeek(date) === false) + }) + + it('accepts a timestamp', () => { + const date = new Date(2014, 8 /* Sep */, 29).getTime() + assert(isThisISOWeek(date) === false) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + assert.throws(isThisISOWeek.bind(null), TypeError) + }) +}) diff --git a/src/isThisMinute/benchmark.js b/src/isThisMinute/benchmark.js new file mode 100644 index 0000000000..9801f3a3f5 --- /dev/null +++ b/src/isThisMinute/benchmark.js @@ -0,0 +1,20 @@ +// @flow +/* eslint-env mocha */ + +/* global suite, benchmark */ + +import isThisMinute from '.' + +suite( + 'isThisMinute', + () => { + benchmark('date-fns', function() { + return isThisMinute(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/src/isThisMinute/index.d.ts b/src/isThisMinute/index.d.ts new file mode 100644 index 0000000000..ca1430d584 --- /dev/null +++ b/src/isThisMinute/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isThisMinute } from 'date-fns' +export default isThisMinute diff --git a/src/isThisMinute/index.js b/src/isThisMinute/index.js new file mode 100644 index 0000000000..2bd418f013 --- /dev/null +++ b/src/isThisMinute/index.js @@ -0,0 +1,38 @@ +import isSameMinute from '../isSameMinute/index.js' + +/** + * @name isThisMinute + * @category Minute Helpers + * @summary Is the given date in the same minute as the current date? + * @pure false + * + * @description + * Is the given date in the same minute 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 + * @returns {Boolean} the date is in this minute + * @throws {TypeError} 1 argument required + * + * @example + * // If now is 25 September 2014 18:30:15.500, + * // is 25 September 2014 18:30:00 in this minute? + * var result = isThisMinute(new Date(2014, 8, 25, 18, 30)) + * //=> true + */ + +export default function isThisMinute(dirtyDate) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return isSameMinute(Date.now(), dirtyDate) +} diff --git a/src/isThisMinute/index.js.flow b/src/isThisMinute/index.js.flow new file mode 100644 index 0000000000..dab9a538cf --- /dev/null +++ b/src/isThisMinute/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: (date: Date | number) => boolean diff --git a/src/isThisMinute/test.js b/src/isThisMinute/test.js new file mode 100644 index 0000000000..f9c0454dc9 --- /dev/null +++ b/src/isThisMinute/test.js @@ -0,0 +1,38 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isThisMinute from '.' + +describe('isThisMinute', () => { + let clock + beforeEach(() => { + clock = sinon.useFakeTimers( + new Date(2014, 8 /* Sep */, 25, 18, 30, 15, 500).getTime() + ) + }) + + afterEach(() => { + clock.restore() + }) + + it('returns true if the given date and the current date have the same minute', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18, 30) + assert(isThisMinute(date) === true) + }) + + it('returns false if the given date and the current date have different minutes', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18, 31) + assert(isThisMinute(date) === false) + }) + + it('accepts a timestamp', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18, 30, 30).getTime() + assert(isThisMinute(date) === true) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + assert.throws(isThisMinute.bind(null), TypeError) + }) +}) diff --git a/src/isThisMonth/benchmark.js b/src/isThisMonth/benchmark.js new file mode 100644 index 0000000000..d3deed014f --- /dev/null +++ b/src/isThisMonth/benchmark.js @@ -0,0 +1,20 @@ +// @flow +/* eslint-env mocha */ + +/* global suite, benchmark */ + +import isThisMonth from '.' + +suite( + 'isThisMonth', + () => { + benchmark('date-fns', function() { + return isThisMonth(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/src/isThisMonth/index.d.ts b/src/isThisMonth/index.d.ts new file mode 100644 index 0000000000..eaf72fdd21 --- /dev/null +++ b/src/isThisMonth/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isThisMonth } from 'date-fns' +export default isThisMonth diff --git a/src/isThisMonth/index.js b/src/isThisMonth/index.js new file mode 100644 index 0000000000..f7d86d28f8 --- /dev/null +++ b/src/isThisMonth/index.js @@ -0,0 +1,37 @@ +import isSameMonth from '../isSameMonth/index.js' + +/** + * @name isThisMonth + * @category Month Helpers + * @summary Is the given date in the same month as the current date? + * @pure false + * + * @description + * Is the given date in the same month 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 + * @returns {Boolean} the date is in this month + * @throws {TypeError} 1 argument required + * + * @example + * // If today is 25 September 2014, is 15 September 2014 in this month? + * var result = isThisMonth(new Date(2014, 8, 15)) + * //=> true + */ + +export default function isThisMonth(dirtyDate) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return isSameMonth(Date.now(), dirtyDate) +} diff --git a/src/isThisMonth/index.js.flow b/src/isThisMonth/index.js.flow new file mode 100644 index 0000000000..dab9a538cf --- /dev/null +++ b/src/isThisMonth/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: (date: Date | number) => boolean diff --git a/src/isThisMonth/test.js b/src/isThisMonth/test.js new file mode 100644 index 0000000000..cf3f427c02 --- /dev/null +++ b/src/isThisMonth/test.js @@ -0,0 +1,36 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isThisMonth from '.' + +describe('isThisMonth', () => { + let clock + 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 month (and year)', () => { + const date = new Date(2014, 8 /* Sep */, 15) + assert(isThisMonth(date) === true) + }) + + it('returns false if the given date and the current date have different months', () => { + const date = new Date(2013, 7 /* Aug */, 31) + assert(isThisMonth(date) === false) + }) + + it('accepts a timestamp', () => { + const date = new Date(2014, 8 /* Sep */, 30).getTime() + assert(isThisMonth(date) === true) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + assert.throws(isThisMonth.bind(null), TypeError) + }) +}) diff --git a/src/isThisQuarter/benchmark.js b/src/isThisQuarter/benchmark.js new file mode 100644 index 0000000000..2c0db5c5f4 --- /dev/null +++ b/src/isThisQuarter/benchmark.js @@ -0,0 +1,20 @@ +// @flow +/* eslint-env mocha */ + +/* global suite, benchmark */ + +import isThisQuarter from '.' + +suite( + 'isThisQuarter', + () => { + benchmark('date-fns', function() { + return isThisQuarter(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/src/isThisQuarter/index.d.ts b/src/isThisQuarter/index.d.ts new file mode 100644 index 0000000000..8c25708a33 --- /dev/null +++ b/src/isThisQuarter/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isThisQuarter } from 'date-fns' +export default isThisQuarter diff --git a/src/isThisQuarter/index.js b/src/isThisQuarter/index.js new file mode 100644 index 0000000000..3c2fe5d685 --- /dev/null +++ b/src/isThisQuarter/index.js @@ -0,0 +1,36 @@ +import isSameQuarter from '../isSameQuarter/index.js' + +/** + * @name isThisQuarter + * @category Quarter Helpers + * @summary Is the given date in the same quarter as the current date? + * @pure false + * + * @description + * Is the given date in the same quarter 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 + * @returns {Boolean} the date is in this quarter + * @throws {TypeError} 1 argument required + * + * @example + * // If today is 25 September 2014, is 2 July 2014 in this quarter? + * var result = isThisQuarter(new Date(2014, 6, 2)) + * //=> true + */ +export default function isThisQuarter(dirtyDate) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return isSameQuarter(Date.now(), dirtyDate) +} diff --git a/src/isThisQuarter/index.js.flow b/src/isThisQuarter/index.js.flow new file mode 100644 index 0000000000..dab9a538cf --- /dev/null +++ b/src/isThisQuarter/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: (date: Date | number) => boolean diff --git a/src/isThisQuarter/test.js b/src/isThisQuarter/test.js new file mode 100644 index 0000000000..fe59445074 --- /dev/null +++ b/src/isThisQuarter/test.js @@ -0,0 +1,36 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isThisQuarter from '.' + +describe('isThisQuarter', function() { + let clock + beforeEach(function() { + clock = sinon.useFakeTimers(new Date(2014, 8 /* Sep */, 25).getTime()) + }) + + afterEach(function() { + clock.restore() + }) + + it('returns true if the given date and the current date have the same quarter (and year)', function() { + const date = new Date(2014, 6 /* Jul */, 2) + assert(isThisQuarter(date) === true) + }) + + it('returns false if the given date and the current date have different quarters', function() { + const date = new Date(2014, 1 /* Feb */, 11) + assert(isThisQuarter(date) === false) + }) + + it('accepts a timestamp', function() { + const date = new Date(2014, 6 /* Jul */, 2).getTime() + assert(isThisQuarter(date) === true) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + assert.throws(isThisQuarter.bind(null), TypeError) + }) +}) diff --git a/src/isThisSecond/benchmark.js b/src/isThisSecond/benchmark.js new file mode 100644 index 0000000000..4fd274b1d3 --- /dev/null +++ b/src/isThisSecond/benchmark.js @@ -0,0 +1,20 @@ +// @flow +/* eslint-env mocha */ + +/* global suite, benchmark */ + +import isThisSecond from '.' + +suite( + 'isThisSecond', + () => { + benchmark('date-fns', function() { + return isThisSecond(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/src/isThisSecond/index.d.ts b/src/isThisSecond/index.d.ts new file mode 100644 index 0000000000..9d45da10be --- /dev/null +++ b/src/isThisSecond/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isThisSecond } from 'date-fns' +export default isThisSecond diff --git a/src/isThisSecond/index.js b/src/isThisSecond/index.js new file mode 100644 index 0000000000..6b72479192 --- /dev/null +++ b/src/isThisSecond/index.js @@ -0,0 +1,37 @@ +import isSameSecond from '../isSameSecond/index.js' + +/** + * @name isThisSecond + * @category Second Helpers + * @summary Is the given date in the same second as the current date? + * @pure false + * + * @description + * Is the given date in the same second 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 + * @returns {Boolean} the date is in this second + * @throws {TypeError} 1 argument required + * + * @example + * // If now is 25 September 2014 18:30:15.500, + * // is 25 September 2014 18:30:15.000 in this second? + * var result = isThisSecond(new Date(2014, 8, 25, 18, 30, 15)) + * //=> true + */ +export default function isThisSecond(dirtyDate) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return isSameSecond(Date.now(), dirtyDate) +} diff --git a/src/isThisSecond/index.js.flow b/src/isThisSecond/index.js.flow new file mode 100644 index 0000000000..dab9a538cf --- /dev/null +++ b/src/isThisSecond/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: (date: Date | number) => boolean diff --git a/src/isThisSecond/test.js b/src/isThisSecond/test.js new file mode 100644 index 0000000000..4929f3d17e --- /dev/null +++ b/src/isThisSecond/test.js @@ -0,0 +1,38 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isThisSecond from '.' + +describe('isThisSecond', () => { + let clock + beforeEach(() => { + clock = sinon.useFakeTimers( + new Date(2014, 8 /* Sep */, 25, 18, 30, 15, 500).getTime() + ) + }) + + afterEach(() => { + clock.restore() + }) + + it('returns true if the given date and the current date have the same second', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18, 30, 15) + assert(isThisSecond(date) === true) + }) + + it('returns false if the given date and the current date have different seconds', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18, 30, 16) + assert(isThisSecond(date) === false) + }) + + it('accepts a timestamp', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18, 30, 15, 250).getTime() + assert(isThisSecond(date) === true) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + assert.throws(isThisSecond.bind(null), TypeError) + }) +}) diff --git a/src/isThisWeek/benchmark.js b/src/isThisWeek/benchmark.js new file mode 100644 index 0000000000..ac9676ff0d --- /dev/null +++ b/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/src/isThisWeek/index.d.ts b/src/isThisWeek/index.d.ts new file mode 100644 index 0000000000..b22ba530e7 --- /dev/null +++ b/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/src/isThisWeek/index.js b/src/isThisWeek/index.js new file mode 100644 index 0000000000..9a13b0a3b1 --- /dev/null +++ b/src/isThisWeek/index.js @@ -0,0 +1,47 @@ +import isSameWeek from '../isSameWeek/index.js' + +/** + * @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, options) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return isSameWeek(dirtyDate, Date.now(), options) +} diff --git a/src/isThisWeek/index.js.flow b/src/isThisWeek/index.js.flow new file mode 100644 index 0000000000..d7c541e550 --- /dev/null +++ b/src/isThisWeek/index.js.flow @@ -0,0 +1,44 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: ( + date: Date | number, + options?: { + locale?: Locale, + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 + } +) => boolean diff --git a/src/isThisWeek/test.js b/src/isThisWeek/test.js new file mode 100644 index 0000000000..fdeb1e6d3d --- /dev/null +++ b/src/isThisWeek/test.js @@ -0,0 +1,41 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isThisWeek from '.' + +describe('isThisWeek', () => { + let clock + 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() { + assert.throws(isThisWeek.bind(null), TypeError) + }) +}) diff --git a/src/isThisYear/benchmark.js b/src/isThisYear/benchmark.js new file mode 100644 index 0000000000..7dab3f6785 --- /dev/null +++ b/src/isThisYear/benchmark.js @@ -0,0 +1,19 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import isThisYear from '.' + +suite( + 'isThisYear', + () => { + benchmark('date-fns', function() { + return isThisYear(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/src/isThisYear/index.d.ts b/src/isThisYear/index.d.ts new file mode 100644 index 0000000000..b95f1be3b0 --- /dev/null +++ b/src/isThisYear/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isThisYear } from 'date-fns' +export default isThisYear diff --git a/src/isThisYear/index.js b/src/isThisYear/index.js new file mode 100644 index 0000000000..ddb65c3ddc --- /dev/null +++ b/src/isThisYear/index.js @@ -0,0 +1,36 @@ +import isSameYear from '../isSameYear/index.js' + +/** + * @name isThisYear + * @category Year Helpers + * @summary Is the given date in the same year as the current date? + * @pure false + * + * @description + * Is the given date in the same year 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 + * @returns {Boolean} the date is in this year + * @throws {TypeError} 1 argument required + * + * @example + * // If today is 25 September 2014, is 2 July 2014 in this year? + * var result = isThisYear(new Date(2014, 6, 2)) + * //=> true + */ +export default function isThisYear(dirtyDate) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return isSameYear(dirtyDate, Date.now()) +} diff --git a/src/isThisYear/index.js.flow b/src/isThisYear/index.js.flow new file mode 100644 index 0000000000..dab9a538cf --- /dev/null +++ b/src/isThisYear/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: (date: Date | number) => boolean diff --git a/src/isThisYear/test.js b/src/isThisYear/test.js new file mode 100644 index 0000000000..f8b05957b2 --- /dev/null +++ b/src/isThisYear/test.js @@ -0,0 +1,36 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isThisYear from '.' + +describe('isThisYear', () => { + let clock + 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 year', () => { + var date = new Date(2014, 6 /* Jul */, 2) + assert(isThisYear(date) === true) + }) + + it('returns false if the given date and the current date have different years', () => { + var date = new Date(2015, 6 /* Jul */, 2) + assert(isThisYear(date) === false) + }) + + it('accepts a timestamp', () => { + var date = new Date(2014, 6 /* Jul */, 2).getTime() + assert(isThisYear(date) === true) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + assert.throws(isThisYear.bind(null), TypeError) + }) +}) diff --git a/src/isToday/benchmark.js b/src/isToday/benchmark.js new file mode 100644 index 0000000000..e31d41abb5 --- /dev/null +++ b/src/isToday/benchmark.js @@ -0,0 +1,19 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import isToday from '.' + +suite( + 'isToday', + () => { + benchmark('date-fns', function() { + return isToday(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/src/isToday/index.d.ts b/src/isToday/index.d.ts new file mode 100644 index 0000000000..ed0cd157ca --- /dev/null +++ b/src/isToday/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isToday } from 'date-fns' +export default isToday diff --git a/src/isToday/index.js b/src/isToday/index.js new file mode 100644 index 0000000000..72f35911e5 --- /dev/null +++ b/src/isToday/index.js @@ -0,0 +1,36 @@ +import isSameDay from '../isSameDay/index.js' + +/** + * @name isToday + * @category Day Helpers + * @summary Is the given date today? + * @pure false + * + * @description + * Is the given date today? + * + * > ⚠️ 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 + * @returns {Boolean} the date is today + * @throws {TypeError} 1 argument required + * + * @example + * // If today is 6 October 2014, is 6 October 14:00:00 today? + * var result = isToday(new Date(2014, 9, 6, 14, 0)) + * //=> true + */ +export default function isToday(dirtyDate) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return isSameDay(dirtyDate, Date.now()) +} diff --git a/src/isToday/index.js.flow b/src/isToday/index.js.flow new file mode 100644 index 0000000000..dab9a538cf --- /dev/null +++ b/src/isToday/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: (date: Date | number) => boolean diff --git a/src/isToday/test.js b/src/isToday/test.js new file mode 100644 index 0000000000..1f4b682690 --- /dev/null +++ b/src/isToday/test.js @@ -0,0 +1,36 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isToday from '.' + +describe('isToday', function() { + let clock + beforeEach(function() { + clock = sinon.useFakeTimers(new Date(2014, 8 /* Sep */, 25).getTime()) + }) + + afterEach(function() { + clock.restore() + }) + + it('returns true if the given date is today', function() { + const result = isToday(new Date(2014, 8 /* Sep */, 25)) + assert(result === true) + }) + + it('returns false if the given date is not today', function() { + const result = isToday(new Date(2014, 8 /* Sep */, 26)) + assert(result === false) + }) + + it('accepts a timestamp', function() { + const result = isToday(new Date(2014, 8 /* Sep */, 25).getTime()) + assert(result === true) + }) + + it('throws TypeError exception if passed less than 1 argument', () => { + assert.throws(isToday.bind(null), TypeError) + }) +}) diff --git a/src/isTomorrow/benchmark.js b/src/isTomorrow/benchmark.js new file mode 100644 index 0000000000..b15319bca5 --- /dev/null +++ b/src/isTomorrow/benchmark.js @@ -0,0 +1,19 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import isTomorrow from '.' + +suite( + 'isTomorrow', + () => { + benchmark('date-fns', function() { + return isTomorrow(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/src/isTomorrow/index.d.ts b/src/isTomorrow/index.d.ts new file mode 100644 index 0000000000..b9c9115a52 --- /dev/null +++ b/src/isTomorrow/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isTomorrow } from 'date-fns' +export default isTomorrow diff --git a/src/isTomorrow/index.js b/src/isTomorrow/index.js new file mode 100644 index 0000000000..2c1c9f7d83 --- /dev/null +++ b/src/isTomorrow/index.js @@ -0,0 +1,37 @@ +import addDays from '../addDays/index.js' +import isSameDay from '../isSameDay/index.js' + +/** + * @name isTomorrow + * @category Day Helpers + * @summary Is the given date tomorrow? + * @pure false + * + * @description + * Is the given date tomorrow? + * + * > ⚠️ 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 + * @returns {Boolean} the date is tomorrow + * @throws {TypeError} 1 argument required + * + * @example + * // If today is 6 October 2014, is 7 October 14:00:00 tomorrow? + * var result = isTomorrow(new Date(2014, 9, 7, 14, 0)) + * //=> true + */ +export default function isTomorrow(dirtyDate) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return isSameDay(dirtyDate, addDays(Date.now(), 1)) +} diff --git a/src/isTomorrow/index.js.flow b/src/isTomorrow/index.js.flow new file mode 100644 index 0000000000..dab9a538cf --- /dev/null +++ b/src/isTomorrow/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: (date: Date | number) => boolean diff --git a/src/isTomorrow/test.js b/src/isTomorrow/test.js new file mode 100644 index 0000000000..5148e99b4b --- /dev/null +++ b/src/isTomorrow/test.js @@ -0,0 +1,36 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isTomorrow from '.' + +describe('isTomorrow', () => { + let clock + beforeEach(() => { + clock = sinon.useFakeTimers(new Date(2014, 8 /* Aug */, 25).getTime()) + }) + + afterEach(() => { + clock.restore() + }) + + it('returns true if the given date is tomorrow', () => { + const result = isTomorrow(new Date(2014, 8 /* Sep */, 26)) + assert(result === true) + }) + + it('returns false if the given date is not tomorrow', () => { + const result = isTomorrow(new Date(2014, 8 /* Sep */, 25)) + assert(result === false) + }) + + it('accepts a timestamp', () => { + const result = isTomorrow(new Date(2014, 8 /* Sep */, 26).getTime()) + assert(result === true) + }) + + it('throws TypeError exception if passed less than 1 argument', () => { + assert.throws(isTomorrow.bind(null), TypeError) + }) +}) diff --git a/src/isYesterday/benchmark.js b/src/isYesterday/benchmark.js new file mode 100644 index 0000000000..2e333b4951 --- /dev/null +++ b/src/isYesterday/benchmark.js @@ -0,0 +1,19 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import isYesterday from '.' + +suite( + 'isYesterday', + () => { + benchmark('date-fns', function() { + return isYesterday(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/src/isYesterday/index.d.ts b/src/isYesterday/index.d.ts new file mode 100644 index 0000000000..b10c63578c --- /dev/null +++ b/src/isYesterday/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isYesterday } from 'date-fns' +export default isYesterday diff --git a/src/isYesterday/index.js b/src/isYesterday/index.js new file mode 100644 index 0000000000..5b7fce805b --- /dev/null +++ b/src/isYesterday/index.js @@ -0,0 +1,37 @@ +import isSameDay from '../isSameDay/index.js' +import subDays from '../subDays/index.js' + +/** + * @name isYesterday + * @category Day Helpers + * @summary Is the given date yesterday? + * @pure false + * + * @description + * Is the given date yesterday? + * + * > ⚠️ 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 + * @returns {Boolean} the date is yesterday + * @throws {TypeError} 1 argument required + * + * @example + * // If today is 6 October 2014, is 5 October 14:00:00 yesterday? + * var result = isYesterday(new Date(2014, 9, 5, 14, 0)) + * //=> true + */ +export default function isYesterday(dirtyDate) { + if (arguments.length < 1) { + throw new TypeError( + '1 argument required, but only ' + arguments.length + ' present' + ) + } + + return isSameDay(dirtyDate, subDays(Date.now(), 1)) +} diff --git a/src/isYesterday/index.js.flow b/src/isYesterday/index.js.flow new file mode 100644 index 0000000000..dab9a538cf --- /dev/null +++ b/src/isYesterday/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: (date: Date | number) => boolean diff --git a/src/isYesterday/test.js b/src/isYesterday/test.js new file mode 100644 index 0000000000..3fd63774a8 --- /dev/null +++ b/src/isYesterday/test.js @@ -0,0 +1,36 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isYesterday from '.' + +describe('isYesterday', () => { + let clock + beforeEach(() => { + clock = sinon.useFakeTimers(new Date(2014, 8 /* Aug */, 25).getTime()) + }) + + afterEach(() => { + clock.restore() + }) + + it('returns true if the given date is yesterday', () => { + const result = isYesterday(new Date(2014, 8 /* Sep */, 24)) + assert(result === true) + }) + + it('returns false if the given date is not yesterday', () => { + const result = isYesterday(new Date(2014, 8 /* Sep */, 25)) + assert(result === false) + }) + + it('accepts a timestamp', () => { + const result = isYesterday(new Date(2014, 8 /* Sep */, 24).getTime()) + assert(result === true) + }) + + it('throws TypeError exception if passed less than 1 argument', () => { + assert.throws(isYesterday.bind(null), TypeError) + }) +}) diff --git a/src/startOfToday/benchmark.js b/src/startOfToday/benchmark.js new file mode 100644 index 0000000000..c479df5822 --- /dev/null +++ b/src/startOfToday/benchmark.js @@ -0,0 +1,9 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import startOfToday from '.' + +suite('startOfToday', () => { + benchmark('date-fns', () => startOfToday()) +}) diff --git a/src/startOfToday/index.d.ts b/src/startOfToday/index.d.ts new file mode 100644 index 0000000000..8a0615e30b --- /dev/null +++ b/src/startOfToday/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { startOfToday } from 'date-fns' +export default startOfToday diff --git a/src/startOfToday/index.js b/src/startOfToday/index.js new file mode 100644 index 0000000000..f763cc9ba1 --- /dev/null +++ b/src/startOfToday/index.js @@ -0,0 +1,28 @@ +import startOfDay from '../startOfDay/index.js' + +/** + * @name startOfToday + * @category Day Helpers + * @summary Return the start of today. + * @pure false + * + * @description + * Return the start of today. + * + * > ⚠️ 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). + * + * @returns {Date} the start of today + * + * @example + * // If today is 6 October 2014: + * var result = startOfToday() + * //=> Mon Oct 6 2014 00:00:00 + */ +export default function startOfToday() { + return startOfDay(Date.now()) +} diff --git a/src/startOfToday/index.js.flow b/src/startOfToday/index.js.flow new file mode 100644 index 0000000000..504b9f5cc7 --- /dev/null +++ b/src/startOfToday/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: () => Date diff --git a/src/startOfToday/test.js b/src/startOfToday/test.js new file mode 100644 index 0000000000..1b2f69185b --- /dev/null +++ b/src/startOfToday/test.js @@ -0,0 +1,24 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import sinon from 'sinon' +import startOfToday from '.' + +describe('startOfToday', function() { + let clock + beforeEach(function() { + clock = sinon.useFakeTimers( + new Date(2014, 8 /* Sep */, 25, 14, 30, 45, 500).getTime() + ) + }) + + afterEach(function() { + clock.restore() + }) + + it('returns the current date with the time setted to 00:00:00', function() { + var result = startOfToday() + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 25)) + }) +}) diff --git a/src/startOfTomorrow/benchmark.js b/src/startOfTomorrow/benchmark.js new file mode 100644 index 0000000000..a550b46048 --- /dev/null +++ b/src/startOfTomorrow/benchmark.js @@ -0,0 +1,9 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import startOfTomorrow from '.' + +suite('startOfTomorrow', () => { + benchmark('date-fns', () => startOfTomorrow()) +}) diff --git a/src/startOfTomorrow/index.d.ts b/src/startOfTomorrow/index.d.ts new file mode 100644 index 0000000000..464ad5b0d7 --- /dev/null +++ b/src/startOfTomorrow/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { startOfTomorrow } from 'date-fns' +export default startOfTomorrow diff --git a/src/startOfTomorrow/index.js b/src/startOfTomorrow/index.js new file mode 100644 index 0000000000..5c32d05c0f --- /dev/null +++ b/src/startOfTomorrow/index.js @@ -0,0 +1,34 @@ +/** + * @name startOfTomorrow + * @category Day Helpers + * @summary Return the start of tomorrow. + * @pure false + * + * @description + * Return the start of tomorrow. + * + * > ⚠️ 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). + * + * @returns {Date} the start of tomorrow + * + * @example + * // If today is 6 October 2014: + * var result = startOfTomorrow() + * //=> Tue Oct 7 2014 00:00:00 + */ +export default function startOfTomorrow() { + var now = new Date() + var year = now.getFullYear() + var month = now.getMonth() + var day = now.getDate() + + var date = new Date(0) + date.setFullYear(year, month, day + 1) + date.setHours(0, 0, 0, 0) + return date +} diff --git a/src/startOfTomorrow/index.js.flow b/src/startOfTomorrow/index.js.flow new file mode 100644 index 0000000000..504b9f5cc7 --- /dev/null +++ b/src/startOfTomorrow/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: () => Date diff --git a/src/startOfTomorrow/test.js b/src/startOfTomorrow/test.js new file mode 100644 index 0000000000..aab3aba9f7 --- /dev/null +++ b/src/startOfTomorrow/test.js @@ -0,0 +1,34 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import startOfTomorrow from '.' + +describe('startOfTomorrow', function() { + it('returns the start of tomorrow', function() { + const clock = sinon.useFakeTimers( + new Date(2014, 8 /* Sep */, 25, 14, 30, 45, 500).getTime() + ) + + const result = startOfTomorrow() + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 26)) + + clock.restore() + }) + + it('handles dates before 100 AD', function() { + const now = new Date(0) + now.setFullYear(14, 8 /* Sep */, 25) + now.setHours(0, 0, 0, 0) + const clock = sinon.useFakeTimers(now.getTime()) + + const expectedResult = new Date(0) + expectedResult.setFullYear(14, 8 /* Sep */, 26) + expectedResult.setHours(0, 0, 0, 0) + const result = startOfTomorrow() + assert.deepEqual(result, expectedResult) + + clock.restore() + }) +}) diff --git a/src/startOfYesterday/benchmark.js b/src/startOfYesterday/benchmark.js new file mode 100644 index 0000000000..4c378778a6 --- /dev/null +++ b/src/startOfYesterday/benchmark.js @@ -0,0 +1,9 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import startOfToday from '.' + +suite('startOfYesterday', () => { + benchmark('date-fns', () => startOfToday()) +}) diff --git a/src/startOfYesterday/index.d.ts b/src/startOfYesterday/index.d.ts new file mode 100644 index 0000000000..0342a28e9d --- /dev/null +++ b/src/startOfYesterday/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { startOfYesterday } from 'date-fns' +export default startOfYesterday diff --git a/src/startOfYesterday/index.js b/src/startOfYesterday/index.js new file mode 100644 index 0000000000..a7ae58e22f --- /dev/null +++ b/src/startOfYesterday/index.js @@ -0,0 +1,34 @@ +/** + * @name startOfYesterday + * @category Day Helpers + * @summary Return the start of yesterday. + * @pure false + * + * @description + * Return the start of yesterday. + * + * > ⚠️ 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). + * + * @returns {Date} the start of yesterday + * + * @example + * // If today is 6 October 2014: + * var result = startOfYesterday() + * //=> Sun Oct 5 2014 00:00:00 + */ +export default function startOfYesterday() { + var now = new Date() + var year = now.getFullYear() + var month = now.getMonth() + var day = now.getDate() + + var date = new Date(0) + date.setFullYear(year, month, day - 1) + date.setHours(0, 0, 0, 0) + return date +} diff --git a/src/startOfYesterday/index.js.flow b/src/startOfYesterday/index.js.flow new file mode 100644 index 0000000000..504b9f5cc7 --- /dev/null +++ b/src/startOfYesterday/index.js.flow @@ -0,0 +1,38 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | number, + end: Date | number +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: () => Date diff --git a/src/startOfYesterday/test.js b/src/startOfYesterday/test.js new file mode 100644 index 0000000000..584739345d --- /dev/null +++ b/src/startOfYesterday/test.js @@ -0,0 +1,34 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import startOfYesterday from '.' + +describe('startOfYesterday', function() { + it('returns the start of yesterday', function() { + const clock = sinon.useFakeTimers( + new Date(2014, 8 /* Sep */, 25, 14, 30, 45, 500).getTime() + ) + + const result = startOfYesterday() + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 24)) + + clock.restore() + }) + + it('handles dates before 100 AD', function() { + const now = new Date(0) + now.setFullYear(14, 8 /* Sep */, 25) + now.setHours(0, 0, 0, 0) + const clock = sinon.useFakeTimers(now.getTime()) + + const expectedResult = new Date(0) + expectedResult.setFullYear(14, 8 /* Sep */, 24) + expectedResult.setHours(0, 0, 0, 0) + const result = startOfYesterday() + assert.deepEqual(result, expectedResult) + + clock.restore() + }) +}) diff --git a/typings.d.ts b/typings.d.ts index 611eea81ec..fb3c360746 100644 --- a/typings.d.ts +++ b/typings.d.ts @@ -303,6 +303,12 @@ declare module 'date-fns' { function endOfSecond(date: Date | number): Date namespace endOfSecond {} + function endOfToday(): Date + namespace endOfToday {} + + function endOfTomorrow(): Date + namespace endOfTomorrow {} + function endOfWeek( date: Date | number, options?: { @@ -315,6 +321,9 @@ declare module 'date-fns' { function endOfYear(date: Date | number): Date namespace endOfYear {} + function endOfYesterday(): Date + namespace endOfYesterday {} + function format( date: Date | number, format: string, @@ -351,6 +360,16 @@ declare module 'date-fns' { ): string namespace formatDistanceStrict {} + function formatDistanceToNow( + date: Date | number, + options?: { + includeSeconds?: boolean + addSuffix?: boolean + locale?: Locale + } + ): string + namespace formatDistanceToNow {} + function formatRelative( date: Date | number, baseDate: Date | number, @@ -483,6 +502,9 @@ declare module 'date-fns' { function isFriday(date: Date | number): boolean namespace isFriday {} + function isFuture(date: Date | number): boolean + namespace isFuture {} + function isLastDayOfMonth(date: Date | number): boolean namespace isLastDayOfMonth {} @@ -492,6 +514,9 @@ declare module 'date-fns' { function isMonday(date: Date | number): boolean namespace isMonday {} + function isPast(date: Date | number): boolean + namespace isPast {} + function isSameDay(dateLeft: Date | number, dateRight: Date | number): boolean namespace isSameDay {} @@ -559,9 +584,45 @@ declare module 'date-fns' { function isSunday(date: Date | number): boolean namespace isSunday {} + function isThisHour(date: Date | number): boolean + namespace isThisHour {} + + function isThisISOWeek(date: Date | number): boolean + namespace isThisISOWeek {} + + function isThisMinute(date: Date | number): boolean + namespace isThisMinute {} + + function isThisMonth(date: Date | number): boolean + namespace isThisMonth {} + + function isThisQuarter(date: Date | number): boolean + namespace isThisQuarter {} + + function isThisSecond(date: Date | number): boolean + namespace isThisSecond {} + + function isThisWeek( + date: Date | number, + options?: { + locale?: Locale + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 + } + ): boolean + namespace isThisWeek {} + + function isThisYear(date: Date | number): boolean + namespace isThisYear {} + function isThursday(date: Date | number): boolean namespace isThursday {} + function isToday(date: Date | number): boolean + namespace isToday {} + + function isTomorrow(date: Date | number): boolean + namespace isTomorrow {} + function isTuesday(date: Date | number): boolean namespace isTuesday {} @@ -577,6 +638,9 @@ declare module 'date-fns' { function isWithinInterval(date: Date | number, interval: Interval): boolean namespace isWithinInterval {} + function isYesterday(date: Date | number): boolean + namespace isYesterday {} + function lastDayOfDecade(date: Date | number): Date namespace lastDayOfDecade {} @@ -743,6 +807,12 @@ declare module 'date-fns' { function startOfSecond(date: Date | number): Date namespace startOfSecond {} + function startOfToday(): Date + namespace startOfToday {} + + function startOfTomorrow(): Date + namespace startOfTomorrow {} + function startOfWeek( date: Date | number, options?: { @@ -765,6 +835,9 @@ declare module 'date-fns' { function startOfYear(date: Date | number): Date namespace startOfYear {} + function startOfYesterday(): Date + namespace startOfYesterday {} + function subDays(date: Date | number, amount: number): Date namespace subDays {} @@ -1043,6 +1116,16 @@ declare module 'date-fns/endOfSecond' { export default endOfSecond } +declare module 'date-fns/endOfToday' { + import { endOfToday } from 'date-fns' + export default endOfToday +} + +declare module 'date-fns/endOfTomorrow' { + import { endOfTomorrow } from 'date-fns' + export default endOfTomorrow +} + declare module 'date-fns/endOfWeek' { import { endOfWeek } from 'date-fns' export default endOfWeek @@ -1053,6 +1136,11 @@ declare module 'date-fns/endOfYear' { export default endOfYear } +declare module 'date-fns/endOfYesterday' { + import { endOfYesterday } from 'date-fns' + export default endOfYesterday +} + declare module 'date-fns/format' { import { format } from 'date-fns' export default format @@ -1068,6 +1156,11 @@ declare module 'date-fns/formatDistanceStrict' { export default formatDistanceStrict } +declare module 'date-fns/formatDistanceToNow' { + import { formatDistanceToNow } from 'date-fns' + export default formatDistanceToNow +} + declare module 'date-fns/formatRelative' { import { formatRelative } from 'date-fns' export default formatRelative @@ -1228,6 +1321,11 @@ declare module 'date-fns/isFriday' { export default isFriday } +declare module 'date-fns/isFuture' { + import { isFuture } from 'date-fns' + export default isFuture +} + declare module 'date-fns/isLastDayOfMonth' { import { isLastDayOfMonth } from 'date-fns' export default isLastDayOfMonth @@ -1243,6 +1341,11 @@ declare module 'date-fns/isMonday' { export default isMonday } +declare module 'date-fns/isPast' { + import { isPast } from 'date-fns' + export default isPast +} + declare module 'date-fns/isSameDay' { import { isSameDay } from 'date-fns' export default isSameDay @@ -1303,11 +1406,61 @@ declare module 'date-fns/isSunday' { export default isSunday } +declare module 'date-fns/isThisHour' { + import { isThisHour } from 'date-fns' + export default isThisHour +} + +declare module 'date-fns/isThisISOWeek' { + import { isThisISOWeek } from 'date-fns' + export default isThisISOWeek +} + +declare module 'date-fns/isThisMinute' { + import { isThisMinute } from 'date-fns' + export default isThisMinute +} + +declare module 'date-fns/isThisMonth' { + import { isThisMonth } from 'date-fns' + export default isThisMonth +} + +declare module 'date-fns/isThisQuarter' { + import { isThisQuarter } from 'date-fns' + export default isThisQuarter +} + +declare module 'date-fns/isThisSecond' { + import { isThisSecond } from 'date-fns' + export default isThisSecond +} + +declare module 'date-fns/isThisWeek' { + import { isThisWeek } from 'date-fns' + export default isThisWeek +} + +declare module 'date-fns/isThisYear' { + import { isThisYear } from 'date-fns' + export default isThisYear +} + declare module 'date-fns/isThursday' { import { isThursday } from 'date-fns' export default isThursday } +declare module 'date-fns/isToday' { + import { isToday } from 'date-fns' + export default isToday +} + +declare module 'date-fns/isTomorrow' { + import { isTomorrow } from 'date-fns' + export default isTomorrow +} + declare module 'date-fns/isTuesday' { import { isTuesday } from 'date-fns' export default isTuesday @@ -1333,6 +1486,11 @@ declare module 'date-fns/isWithinInterval' { export default isWithinInterval } +declare module 'date-fns/isYesterday' { + import { isYesterday } from 'date-fns' + export default isYesterday +} + declare module 'date-fns/lastDayOfDecade' { import { lastDayOfDecade } from 'date-fns' export default lastDayOfDecade @@ -1518,6 +1676,16 @@ declare module 'date-fns/startOfSecond' { export default startOfSecond } +declare module 'date-fns/startOfToday' { + import { startOfToday } from 'date-fns' + export default startOfToday +} + +declare module 'date-fns/startOfTomorrow' { + import { startOfTomorrow } from 'date-fns' + export default startOfTomorrow +} + declare module 'date-fns/startOfWeek' { import { startOfWeek } from 'date-fns' export default startOfWeek @@ -1533,6 +1701,11 @@ declare module 'date-fns/startOfYear' { export default startOfYear } +declare module 'date-fns/startOfYesterday' { + import { startOfYesterday } from 'date-fns' + export default startOfYesterday +} + declare module 'date-fns/subDays' { import { subDays } from 'date-fns' export default subDays @@ -1828,6 +2001,16 @@ declare module 'date-fns/endOfSecond/index' { export default endOfSecond } +declare module 'date-fns/endOfToday/index' { + import { endOfToday } from 'date-fns' + export default endOfToday +} + +declare module 'date-fns/endOfTomorrow/index' { + import { endOfTomorrow } from 'date-fns' + export default endOfTomorrow +} + declare module 'date-fns/endOfWeek/index' { import { endOfWeek } from 'date-fns' export default endOfWeek @@ -1838,6 +2021,11 @@ declare module 'date-fns/endOfYear/index' { export default endOfYear } +declare module 'date-fns/endOfYesterday/index' { + import { endOfYesterday } from 'date-fns' + export default endOfYesterday +} + declare module 'date-fns/format/index' { import { format } from 'date-fns' export default format @@ -1853,6 +2041,11 @@ declare module 'date-fns/formatDistanceStrict/index' { export default formatDistanceStrict } +declare module 'date-fns/formatDistanceToNow/index' { + import { formatDistanceToNow } from 'date-fns' + export default formatDistanceToNow +} + declare module 'date-fns/formatRelative/index' { import { formatRelative } from 'date-fns' export default formatRelative @@ -2013,6 +2206,11 @@ declare module 'date-fns/isFriday/index' { export default isFriday } +declare module 'date-fns/isFuture/index' { + import { isFuture } from 'date-fns' + export default isFuture +} + declare module 'date-fns/isLastDayOfMonth/index' { import { isLastDayOfMonth } from 'date-fns' export default isLastDayOfMonth @@ -2028,6 +2226,11 @@ declare module 'date-fns/isMonday/index' { export default isMonday } +declare module 'date-fns/isPast/index' { + import { isPast } from 'date-fns' + export default isPast +} + declare module 'date-fns/isSameDay/index' { import { isSameDay } from 'date-fns' export default isSameDay @@ -2088,11 +2291,61 @@ declare module 'date-fns/isSunday/index' { export default isSunday } +declare module 'date-fns/isThisHour/index' { + import { isThisHour } from 'date-fns' + export default isThisHour +} + +declare module 'date-fns/isThisISOWeek/index' { + import { isThisISOWeek } from 'date-fns' + export default isThisISOWeek +} + +declare module 'date-fns/isThisMinute/index' { + import { isThisMinute } from 'date-fns' + export default isThisMinute +} + +declare module 'date-fns/isThisMonth/index' { + import { isThisMonth } from 'date-fns' + export default isThisMonth +} + +declare module 'date-fns/isThisQuarter/index' { + import { isThisQuarter } from 'date-fns' + export default isThisQuarter +} + +declare module 'date-fns/isThisSecond/index' { + import { isThisSecond } from 'date-fns' + export default isThisSecond +} + +declare module 'date-fns/isThisWeek/index' { + import { isThisWeek } from 'date-fns' + export default isThisWeek +} + +declare module 'date-fns/isThisYear/index' { + import { isThisYear } from 'date-fns' + export default isThisYear +} + declare module 'date-fns/isThursday/index' { import { isThursday } from 'date-fns' export default isThursday } +declare module 'date-fns/isToday/index' { + import { isToday } from 'date-fns' + export default isToday +} + +declare module 'date-fns/isTomorrow/index' { + import { isTomorrow } from 'date-fns' + export default isTomorrow +} + declare module 'date-fns/isTuesday/index' { import { isTuesday } from 'date-fns' export default isTuesday @@ -2118,6 +2371,11 @@ declare module 'date-fns/isWithinInterval/index' { export default isWithinInterval } +declare module 'date-fns/isYesterday/index' { + import { isYesterday } from 'date-fns' + export default isYesterday +} + declare module 'date-fns/lastDayOfDecade/index' { import { lastDayOfDecade } from 'date-fns' export default lastDayOfDecade @@ -2303,6 +2561,16 @@ declare module 'date-fns/startOfSecond/index' { export default startOfSecond } +declare module 'date-fns/startOfToday/index' { + import { startOfToday } from 'date-fns' + export default startOfToday +} + +declare module 'date-fns/startOfTomorrow/index' { + import { startOfTomorrow } from 'date-fns' + export default startOfTomorrow +} + declare module 'date-fns/startOfWeek/index' { import { startOfWeek } from 'date-fns' export default startOfWeek @@ -2318,6 +2586,11 @@ declare module 'date-fns/startOfYear/index' { export default startOfYear } +declare module 'date-fns/startOfYesterday/index' { + import { startOfYesterday } from 'date-fns' + export default startOfYesterday +} + declare module 'date-fns/subDays/index' { import { subDays } from 'date-fns' export default subDays @@ -2613,6 +2886,16 @@ declare module 'date-fns/endOfSecond/index.js' { export default endOfSecond } +declare module 'date-fns/endOfToday/index.js' { + import { endOfToday } from 'date-fns' + export default endOfToday +} + +declare module 'date-fns/endOfTomorrow/index.js' { + import { endOfTomorrow } from 'date-fns' + export default endOfTomorrow +} + declare module 'date-fns/endOfWeek/index.js' { import { endOfWeek } from 'date-fns' export default endOfWeek @@ -2623,6 +2906,11 @@ declare module 'date-fns/endOfYear/index.js' { export default endOfYear } +declare module 'date-fns/endOfYesterday/index.js' { + import { endOfYesterday } from 'date-fns' + export default endOfYesterday +} + declare module 'date-fns/format/index.js' { import { format } from 'date-fns' export default format @@ -2638,6 +2926,11 @@ declare module 'date-fns/formatDistanceStrict/index.js' { export default formatDistanceStrict } +declare module 'date-fns/formatDistanceToNow/index.js' { + import { formatDistanceToNow } from 'date-fns' + export default formatDistanceToNow +} + declare module 'date-fns/formatRelative/index.js' { import { formatRelative } from 'date-fns' export default formatRelative @@ -2798,6 +3091,11 @@ declare module 'date-fns/isFriday/index.js' { export default isFriday } +declare module 'date-fns/isFuture/index.js' { + import { isFuture } from 'date-fns' + export default isFuture +} + declare module 'date-fns/isLastDayOfMonth/index.js' { import { isLastDayOfMonth } from 'date-fns' export default isLastDayOfMonth @@ -2813,6 +3111,11 @@ declare module 'date-fns/isMonday/index.js' { export default isMonday } +declare module 'date-fns/isPast/index.js' { + import { isPast } from 'date-fns' + export default isPast +} + declare module 'date-fns/isSameDay/index.js' { import { isSameDay } from 'date-fns' export default isSameDay @@ -2873,11 +3176,61 @@ declare module 'date-fns/isSunday/index.js' { export default isSunday } +declare module 'date-fns/isThisHour/index.js' { + import { isThisHour } from 'date-fns' + export default isThisHour +} + +declare module 'date-fns/isThisISOWeek/index.js' { + import { isThisISOWeek } from 'date-fns' + export default isThisISOWeek +} + +declare module 'date-fns/isThisMinute/index.js' { + import { isThisMinute } from 'date-fns' + export default isThisMinute +} + +declare module 'date-fns/isThisMonth/index.js' { + import { isThisMonth } from 'date-fns' + export default isThisMonth +} + +declare module 'date-fns/isThisQuarter/index.js' { + import { isThisQuarter } from 'date-fns' + export default isThisQuarter +} + +declare module 'date-fns/isThisSecond/index.js' { + import { isThisSecond } from 'date-fns' + export default isThisSecond +} + +declare module 'date-fns/isThisWeek/index.js' { + import { isThisWeek } from 'date-fns' + export default isThisWeek +} + +declare module 'date-fns/isThisYear/index.js' { + import { isThisYear } from 'date-fns' + export default isThisYear +} + declare module 'date-fns/isThursday/index.js' { import { isThursday } from 'date-fns' export default isThursday } +declare module 'date-fns/isToday/index.js' { + import { isToday } from 'date-fns' + export default isToday +} + +declare module 'date-fns/isTomorrow/index.js' { + import { isTomorrow } from 'date-fns' + export default isTomorrow +} + declare module 'date-fns/isTuesday/index.js' { import { isTuesday } from 'date-fns' export default isTuesday @@ -2903,6 +3256,11 @@ declare module 'date-fns/isWithinInterval/index.js' { export default isWithinInterval } +declare module 'date-fns/isYesterday/index.js' { + import { isYesterday } from 'date-fns' + export default isYesterday +} + declare module 'date-fns/lastDayOfDecade/index.js' { import { lastDayOfDecade } from 'date-fns' export default lastDayOfDecade @@ -3088,6 +3446,16 @@ declare module 'date-fns/startOfSecond/index.js' { export default startOfSecond } +declare module 'date-fns/startOfToday/index.js' { + import { startOfToday } from 'date-fns' + export default startOfToday +} + +declare module 'date-fns/startOfTomorrow/index.js' { + import { startOfTomorrow } from 'date-fns' + export default startOfTomorrow +} + declare module 'date-fns/startOfWeek/index.js' { import { startOfWeek } from 'date-fns' export default startOfWeek @@ -3103,6 +3471,11 @@ declare module 'date-fns/startOfYear/index.js' { export default startOfYear } +declare module 'date-fns/startOfYesterday/index.js' { + import { startOfYesterday } from 'date-fns' + export default startOfYesterday +} + declare module 'date-fns/subDays/index.js' { import { subDays } from 'date-fns' export default subDays @@ -6732,6 +7105,12 @@ declare module 'date-fns/esm' { function endOfSecond(date: Date | number): Date namespace endOfSecond {} + function endOfToday(): Date + namespace endOfToday {} + + function endOfTomorrow(): Date + namespace endOfTomorrow {} + function endOfWeek( date: Date | number, options?: { @@ -6744,6 +7123,9 @@ declare module 'date-fns/esm' { function endOfYear(date: Date | number): Date namespace endOfYear {} + function endOfYesterday(): Date + namespace endOfYesterday {} + function format( date: Date | number, format: string, @@ -6780,6 +7162,16 @@ declare module 'date-fns/esm' { ): string namespace formatDistanceStrict {} + function formatDistanceToNow( + date: Date | number, + options?: { + includeSeconds?: boolean + addSuffix?: boolean + locale?: Locale + } + ): string + namespace formatDistanceToNow {} + function formatRelative( date: Date | number, baseDate: Date | number, @@ -6912,6 +7304,9 @@ declare module 'date-fns/esm' { function isFriday(date: Date | number): boolean namespace isFriday {} + function isFuture(date: Date | number): boolean + namespace isFuture {} + function isLastDayOfMonth(date: Date | number): boolean namespace isLastDayOfMonth {} @@ -6921,6 +7316,9 @@ declare module 'date-fns/esm' { function isMonday(date: Date | number): boolean namespace isMonday {} + function isPast(date: Date | number): boolean + namespace isPast {} + function isSameDay(dateLeft: Date | number, dateRight: Date | number): boolean namespace isSameDay {} @@ -6988,9 +7386,45 @@ declare module 'date-fns/esm' { function isSunday(date: Date | number): boolean namespace isSunday {} + function isThisHour(date: Date | number): boolean + namespace isThisHour {} + + function isThisISOWeek(date: Date | number): boolean + namespace isThisISOWeek {} + + function isThisMinute(date: Date | number): boolean + namespace isThisMinute {} + + function isThisMonth(date: Date | number): boolean + namespace isThisMonth {} + + function isThisQuarter(date: Date | number): boolean + namespace isThisQuarter {} + + function isThisSecond(date: Date | number): boolean + namespace isThisSecond {} + + function isThisWeek( + date: Date | number, + options?: { + locale?: Locale + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 + } + ): boolean + namespace isThisWeek {} + + function isThisYear(date: Date | number): boolean + namespace isThisYear {} + function isThursday(date: Date | number): boolean namespace isThursday {} + function isToday(date: Date | number): boolean + namespace isToday {} + + function isTomorrow(date: Date | number): boolean + namespace isTomorrow {} + function isTuesday(date: Date | number): boolean namespace isTuesday {} @@ -7006,6 +7440,9 @@ declare module 'date-fns/esm' { function isWithinInterval(date: Date | number, interval: Interval): boolean namespace isWithinInterval {} + function isYesterday(date: Date | number): boolean + namespace isYesterday {} + function lastDayOfDecade(date: Date | number): Date namespace lastDayOfDecade {} @@ -7172,6 +7609,12 @@ declare module 'date-fns/esm' { function startOfSecond(date: Date | number): Date namespace startOfSecond {} + function startOfToday(): Date + namespace startOfToday {} + + function startOfTomorrow(): Date + namespace startOfTomorrow {} + function startOfWeek( date: Date | number, options?: { @@ -7194,6 +7637,9 @@ declare module 'date-fns/esm' { function startOfYear(date: Date | number): Date namespace startOfYear {} + function startOfYesterday(): Date + namespace startOfYesterday {} + function subDays(date: Date | number, amount: number): Date namespace subDays {} @@ -7472,6 +7918,16 @@ declare module 'date-fns/esm/endOfSecond' { export default endOfSecond } +declare module 'date-fns/esm/endOfToday' { + import { endOfToday } from 'date-fns/esm' + export default endOfToday +} + +declare module 'date-fns/esm/endOfTomorrow' { + import { endOfTomorrow } from 'date-fns/esm' + export default endOfTomorrow +} + declare module 'date-fns/esm/endOfWeek' { import { endOfWeek } from 'date-fns/esm' export default endOfWeek @@ -7482,6 +7938,11 @@ declare module 'date-fns/esm/endOfYear' { export default endOfYear } +declare module 'date-fns/esm/endOfYesterday' { + import { endOfYesterday } from 'date-fns/esm' + export default endOfYesterday +} + declare module 'date-fns/esm/format' { import { format } from 'date-fns/esm' export default format @@ -7497,6 +7958,11 @@ declare module 'date-fns/esm/formatDistanceStrict' { export default formatDistanceStrict } +declare module 'date-fns/esm/formatDistanceToNow' { + import { formatDistanceToNow } from 'date-fns/esm' + export default formatDistanceToNow +} + declare module 'date-fns/esm/formatRelative' { import { formatRelative } from 'date-fns/esm' export default formatRelative @@ -7657,6 +8123,11 @@ declare module 'date-fns/esm/isFriday' { export default isFriday } +declare module 'date-fns/esm/isFuture' { + import { isFuture } from 'date-fns/esm' + export default isFuture +} + declare module 'date-fns/esm/isLastDayOfMonth' { import { isLastDayOfMonth } from 'date-fns/esm' export default isLastDayOfMonth @@ -7672,6 +8143,11 @@ declare module 'date-fns/esm/isMonday' { export default isMonday } +declare module 'date-fns/esm/isPast' { + import { isPast } from 'date-fns/esm' + export default isPast +} + declare module 'date-fns/esm/isSameDay' { import { isSameDay } from 'date-fns/esm' export default isSameDay @@ -7732,11 +8208,61 @@ declare module 'date-fns/esm/isSunday' { export default isSunday } +declare module 'date-fns/esm/isThisHour' { + import { isThisHour } from 'date-fns/esm' + export default isThisHour +} + +declare module 'date-fns/esm/isThisISOWeek' { + import { isThisISOWeek } from 'date-fns/esm' + export default isThisISOWeek +} + +declare module 'date-fns/esm/isThisMinute' { + import { isThisMinute } from 'date-fns/esm' + export default isThisMinute +} + +declare module 'date-fns/esm/isThisMonth' { + import { isThisMonth } from 'date-fns/esm' + export default isThisMonth +} + +declare module 'date-fns/esm/isThisQuarter' { + import { isThisQuarter } from 'date-fns/esm' + export default isThisQuarter +} + +declare module 'date-fns/esm/isThisSecond' { + import { isThisSecond } from 'date-fns/esm' + export default isThisSecond +} + +declare module 'date-fns/esm/isThisWeek' { + import { isThisWeek } from 'date-fns/esm' + export default isThisWeek +} + +declare module 'date-fns/esm/isThisYear' { + import { isThisYear } from 'date-fns/esm' + export default isThisYear +} + declare module 'date-fns/esm/isThursday' { import { isThursday } from 'date-fns/esm' export default isThursday } +declare module 'date-fns/esm/isToday' { + import { isToday } from 'date-fns/esm' + export default isToday +} + +declare module 'date-fns/esm/isTomorrow' { + import { isTomorrow } from 'date-fns/esm' + export default isTomorrow +} + declare module 'date-fns/esm/isTuesday' { import { isTuesday } from 'date-fns/esm' export default isTuesday @@ -7762,6 +8288,11 @@ declare module 'date-fns/esm/isWithinInterval' { export default isWithinInterval } +declare module 'date-fns/esm/isYesterday' { + import { isYesterday } from 'date-fns/esm' + export default isYesterday +} + declare module 'date-fns/esm/lastDayOfDecade' { import { lastDayOfDecade } from 'date-fns/esm' export default lastDayOfDecade @@ -7947,6 +8478,16 @@ declare module 'date-fns/esm/startOfSecond' { export default startOfSecond } +declare module 'date-fns/esm/startOfToday' { + import { startOfToday } from 'date-fns/esm' + export default startOfToday +} + +declare module 'date-fns/esm/startOfTomorrow' { + import { startOfTomorrow } from 'date-fns/esm' + export default startOfTomorrow +} + declare module 'date-fns/esm/startOfWeek' { import { startOfWeek } from 'date-fns/esm' export default startOfWeek @@ -7962,6 +8503,11 @@ declare module 'date-fns/esm/startOfYear' { export default startOfYear } +declare module 'date-fns/esm/startOfYesterday' { + import { startOfYesterday } from 'date-fns/esm' + export default startOfYesterday +} + declare module 'date-fns/esm/subDays' { import { subDays } from 'date-fns/esm' export default subDays @@ -8257,6 +8803,16 @@ declare module 'date-fns/esm/endOfSecond/index' { export default endOfSecond } +declare module 'date-fns/esm/endOfToday/index' { + import { endOfToday } from 'date-fns/esm' + export default endOfToday +} + +declare module 'date-fns/esm/endOfTomorrow/index' { + import { endOfTomorrow } from 'date-fns/esm' + export default endOfTomorrow +} + declare module 'date-fns/esm/endOfWeek/index' { import { endOfWeek } from 'date-fns/esm' export default endOfWeek @@ -8267,6 +8823,11 @@ declare module 'date-fns/esm/endOfYear/index' { export default endOfYear } +declare module 'date-fns/esm/endOfYesterday/index' { + import { endOfYesterday } from 'date-fns/esm' + export default endOfYesterday +} + declare module 'date-fns/esm/format/index' { import { format } from 'date-fns/esm' export default format @@ -8282,6 +8843,11 @@ declare module 'date-fns/esm/formatDistanceStrict/index' { export default formatDistanceStrict } +declare module 'date-fns/esm/formatDistanceToNow/index' { + import { formatDistanceToNow } from 'date-fns/esm' + export default formatDistanceToNow +} + declare module 'date-fns/esm/formatRelative/index' { import { formatRelative } from 'date-fns/esm' export default formatRelative @@ -8442,6 +9008,11 @@ declare module 'date-fns/esm/isFriday/index' { export default isFriday } +declare module 'date-fns/esm/isFuture/index' { + import { isFuture } from 'date-fns/esm' + export default isFuture +} + declare module 'date-fns/esm/isLastDayOfMonth/index' { import { isLastDayOfMonth } from 'date-fns/esm' export default isLastDayOfMonth @@ -8457,6 +9028,11 @@ declare module 'date-fns/esm/isMonday/index' { export default isMonday } +declare module 'date-fns/esm/isPast/index' { + import { isPast } from 'date-fns/esm' + export default isPast +} + declare module 'date-fns/esm/isSameDay/index' { import { isSameDay } from 'date-fns/esm' export default isSameDay @@ -8517,11 +9093,61 @@ declare module 'date-fns/esm/isSunday/index' { export default isSunday } +declare module 'date-fns/esm/isThisHour/index' { + import { isThisHour } from 'date-fns/esm' + export default isThisHour +} + +declare module 'date-fns/esm/isThisISOWeek/index' { + import { isThisISOWeek } from 'date-fns/esm' + export default isThisISOWeek +} + +declare module 'date-fns/esm/isThisMinute/index' { + import { isThisMinute } from 'date-fns/esm' + export default isThisMinute +} + +declare module 'date-fns/esm/isThisMonth/index' { + import { isThisMonth } from 'date-fns/esm' + export default isThisMonth +} + +declare module 'date-fns/esm/isThisQuarter/index' { + import { isThisQuarter } from 'date-fns/esm' + export default isThisQuarter +} + +declare module 'date-fns/esm/isThisSecond/index' { + import { isThisSecond } from 'date-fns/esm' + export default isThisSecond +} + +declare module 'date-fns/esm/isThisWeek/index' { + import { isThisWeek } from 'date-fns/esm' + export default isThisWeek +} + +declare module 'date-fns/esm/isThisYear/index' { + import { isThisYear } from 'date-fns/esm' + export default isThisYear +} + declare module 'date-fns/esm/isThursday/index' { import { isThursday } from 'date-fns/esm' export default isThursday } +declare module 'date-fns/esm/isToday/index' { + import { isToday } from 'date-fns/esm' + export default isToday +} + +declare module 'date-fns/esm/isTomorrow/index' { + import { isTomorrow } from 'date-fns/esm' + export default isTomorrow +} + declare module 'date-fns/esm/isTuesday/index' { import { isTuesday } from 'date-fns/esm' export default isTuesday @@ -8547,6 +9173,11 @@ declare module 'date-fns/esm/isWithinInterval/index' { export default isWithinInterval } +declare module 'date-fns/esm/isYesterday/index' { + import { isYesterday } from 'date-fns/esm' + export default isYesterday +} + declare module 'date-fns/esm/lastDayOfDecade/index' { import { lastDayOfDecade } from 'date-fns/esm' export default lastDayOfDecade @@ -8732,6 +9363,16 @@ declare module 'date-fns/esm/startOfSecond/index' { export default startOfSecond } +declare module 'date-fns/esm/startOfToday/index' { + import { startOfToday } from 'date-fns/esm' + export default startOfToday +} + +declare module 'date-fns/esm/startOfTomorrow/index' { + import { startOfTomorrow } from 'date-fns/esm' + export default startOfTomorrow +} + declare module 'date-fns/esm/startOfWeek/index' { import { startOfWeek } from 'date-fns/esm' export default startOfWeek @@ -8747,6 +9388,11 @@ declare module 'date-fns/esm/startOfYear/index' { export default startOfYear } +declare module 'date-fns/esm/startOfYesterday/index' { + import { startOfYesterday } from 'date-fns/esm' + export default startOfYesterday +} + declare module 'date-fns/esm/subDays/index' { import { subDays } from 'date-fns/esm' export default subDays @@ -9042,6 +9688,16 @@ declare module 'date-fns/esm/endOfSecond/index.js' { export default endOfSecond } +declare module 'date-fns/esm/endOfToday/index.js' { + import { endOfToday } from 'date-fns/esm' + export default endOfToday +} + +declare module 'date-fns/esm/endOfTomorrow/index.js' { + import { endOfTomorrow } from 'date-fns/esm' + export default endOfTomorrow +} + declare module 'date-fns/esm/endOfWeek/index.js' { import { endOfWeek } from 'date-fns/esm' export default endOfWeek @@ -9052,6 +9708,11 @@ declare module 'date-fns/esm/endOfYear/index.js' { export default endOfYear } +declare module 'date-fns/esm/endOfYesterday/index.js' { + import { endOfYesterday } from 'date-fns/esm' + export default endOfYesterday +} + declare module 'date-fns/esm/format/index.js' { import { format } from 'date-fns/esm' export default format @@ -9067,6 +9728,11 @@ declare module 'date-fns/esm/formatDistanceStrict/index.js' { export default formatDistanceStrict } +declare module 'date-fns/esm/formatDistanceToNow/index.js' { + import { formatDistanceToNow } from 'date-fns/esm' + export default formatDistanceToNow +} + declare module 'date-fns/esm/formatRelative/index.js' { import { formatRelative } from 'date-fns/esm' export default formatRelative @@ -9227,6 +9893,11 @@ declare module 'date-fns/esm/isFriday/index.js' { export default isFriday } +declare module 'date-fns/esm/isFuture/index.js' { + import { isFuture } from 'date-fns/esm' + export default isFuture +} + declare module 'date-fns/esm/isLastDayOfMonth/index.js' { import { isLastDayOfMonth } from 'date-fns/esm' export default isLastDayOfMonth @@ -9242,6 +9913,11 @@ declare module 'date-fns/esm/isMonday/index.js' { export default isMonday } +declare module 'date-fns/esm/isPast/index.js' { + import { isPast } from 'date-fns/esm' + export default isPast +} + declare module 'date-fns/esm/isSameDay/index.js' { import { isSameDay } from 'date-fns/esm' export default isSameDay @@ -9302,11 +9978,61 @@ declare module 'date-fns/esm/isSunday/index.js' { export default isSunday } +declare module 'date-fns/esm/isThisHour/index.js' { + import { isThisHour } from 'date-fns/esm' + export default isThisHour +} + +declare module 'date-fns/esm/isThisISOWeek/index.js' { + import { isThisISOWeek } from 'date-fns/esm' + export default isThisISOWeek +} + +declare module 'date-fns/esm/isThisMinute/index.js' { + import { isThisMinute } from 'date-fns/esm' + export default isThisMinute +} + +declare module 'date-fns/esm/isThisMonth/index.js' { + import { isThisMonth } from 'date-fns/esm' + export default isThisMonth +} + +declare module 'date-fns/esm/isThisQuarter/index.js' { + import { isThisQuarter } from 'date-fns/esm' + export default isThisQuarter +} + +declare module 'date-fns/esm/isThisSecond/index.js' { + import { isThisSecond } from 'date-fns/esm' + export default isThisSecond +} + +declare module 'date-fns/esm/isThisWeek/index.js' { + import { isThisWeek } from 'date-fns/esm' + export default isThisWeek +} + +declare module 'date-fns/esm/isThisYear/index.js' { + import { isThisYear } from 'date-fns/esm' + export default isThisYear +} + declare module 'date-fns/esm/isThursday/index.js' { import { isThursday } from 'date-fns/esm' export default isThursday } +declare module 'date-fns/esm/isToday/index.js' { + import { isToday } from 'date-fns/esm' + export default isToday +} + +declare module 'date-fns/esm/isTomorrow/index.js' { + import { isTomorrow } from 'date-fns/esm' + export default isTomorrow +} + declare module 'date-fns/esm/isTuesday/index.js' { import { isTuesday } from 'date-fns/esm' export default isTuesday @@ -9332,6 +10058,11 @@ declare module 'date-fns/esm/isWithinInterval/index.js' { export default isWithinInterval } +declare module 'date-fns/esm/isYesterday/index.js' { + import { isYesterday } from 'date-fns/esm' + export default isYesterday +} + declare module 'date-fns/esm/lastDayOfDecade/index.js' { import { lastDayOfDecade } from 'date-fns/esm' export default lastDayOfDecade @@ -9517,6 +10248,16 @@ declare module 'date-fns/esm/startOfSecond/index.js' { export default startOfSecond } +declare module 'date-fns/esm/startOfToday/index.js' { + import { startOfToday } from 'date-fns/esm' + export default startOfToday +} + +declare module 'date-fns/esm/startOfTomorrow/index.js' { + import { startOfTomorrow } from 'date-fns/esm' + export default startOfTomorrow +} + declare module 'date-fns/esm/startOfWeek/index.js' { import { startOfWeek } from 'date-fns/esm' export default startOfWeek @@ -9532,6 +10273,11 @@ declare module 'date-fns/esm/startOfYear/index.js' { export default startOfYear } +declare module 'date-fns/esm/startOfYesterday/index.js' { + import { startOfYesterday } from 'date-fns/esm' + export default startOfYesterday +} + declare module 'date-fns/esm/subDays/index.js' { import { subDays } from 'date-fns/esm' export default subDays @@ -15146,6 +15892,10 @@ interface dateFns { endOfSecond(date: Date | number): Date + endOfToday(): Date + + endOfTomorrow(): Date + endOfWeek( date: Date | number, options?: { @@ -15156,6 +15906,8 @@ interface dateFns { endOfYear(date: Date | number): Date + endOfYesterday(): Date + format( date: Date | number, format: string, @@ -15189,6 +15941,15 @@ interface dateFns { } ): string + formatDistanceToNow( + date: Date | number, + options?: { + includeSeconds?: boolean + addSuffix?: boolean + locale?: Locale + } + ): string + formatRelative( date: Date | number, baseDate: Date | number, @@ -15289,12 +16050,16 @@ interface dateFns { isFriday(date: Date | number): boolean + isFuture(date: Date | number): boolean + isLastDayOfMonth(date: Date | number): boolean isLeapYear(date: Date | number): boolean isMonday(date: Date | number): boolean + isPast(date: Date | number): boolean + isSameDay(dateLeft: Date | number, dateRight: Date | number): boolean isSameHour(dateLeft: Date | number, dateRight: Date | number): boolean @@ -15326,8 +16091,34 @@ interface dateFns { isSunday(date: Date | number): boolean + isThisHour(date: Date | number): boolean + + isThisISOWeek(date: Date | number): boolean + + isThisMinute(date: Date | number): boolean + + isThisMonth(date: Date | number): boolean + + isThisQuarter(date: Date | number): boolean + + isThisSecond(date: Date | number): boolean + + isThisWeek( + date: Date | number, + options?: { + locale?: Locale + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 + } + ): boolean + + isThisYear(date: Date | number): boolean + isThursday(date: Date | number): boolean + isToday(date: Date | number): boolean + + isTomorrow(date: Date | number): boolean + isTuesday(date: Date | number): boolean isValid(date: any): boolean @@ -15338,6 +16129,8 @@ interface dateFns { isWithinInterval(date: Date | number, interval: Interval): boolean + isYesterday(date: Date | number): boolean + lastDayOfDecade(date: Date | number): Date lastDayOfISOWeek(date: Date | number): Date @@ -15467,6 +16260,10 @@ interface dateFns { startOfSecond(date: Date | number): Date + startOfToday(): Date + + startOfTomorrow(): Date + startOfWeek( date: Date | number, options?: { @@ -15486,6 +16283,8 @@ interface dateFns { startOfYear(date: Date | number): Date + startOfYesterday(): Date + subDays(date: Date | number, amount: number): Date subHours(date: Date | number, amount: number): Date