Skip to content

Commit

Permalink
Restore now-dependent functions (closes date-fns#1184) (date-fns#1197)
Browse files Browse the repository at this point in the history
  • Loading branch information
kossnocorp authored and elmomalmo committed Jul 12, 2019
1 parent 8609cad commit e1a44d9
Show file tree
Hide file tree
Showing 110 changed files with 3,830 additions and 97 deletions.
62 changes: 1 addition & 61 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down Expand Up @@ -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`.

Expand Down
2 changes: 1 addition & 1 deletion scripts/build/_lib/prettier.js
Expand Up @@ -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 }))
}
31 changes: 17 additions & 14 deletions 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}`
}),
','
Expand All @@ -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'
Expand All @@ -60,17 +63,17 @@ 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(' | ')})[]`
}

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

Expand Down
39 changes: 26 additions & 13 deletions scripts/build/docs.js
Expand Up @@ -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) || []
)
: []
),
[]
)

Expand Down Expand Up @@ -270,7 +281,7 @@ function generateFPFnWithOptionsDoc(dirtyDoc) {
}

function withOptions(args) {
return args[0].name === 'options'
return args && args[0].name === 'options'
}

function generateUsageTabs(isFPFn) {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions scripts/test/node.sh
Expand Up @@ -4,8 +4,6 @@
#
# It's a part of the test process.

set -ex

export PATH="$(yarn bin):$PATH"

# Update and source nvm
Expand All @@ -17,5 +15,5 @@ do
echo "Running tests using Node.js $version"
nvm install $version
npm rebuild
jest
jest || exit 1
done
9 changes: 9 additions & 0 deletions src/endOfToday/benchmark.js
@@ -0,0 +1,9 @@
// @flow
/* eslint-env mocha */
/* global suite, benchmark */

import endOfToday from '.'

suite('endOfToday', () => {
benchmark('date-fns', () => endOfToday())
})
4 changes: 4 additions & 0 deletions 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
28 changes: 28 additions & 0 deletions 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())
}
38 changes: 38 additions & 0 deletions 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
24 changes: 24 additions & 0 deletions 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))
})
})
9 changes: 9 additions & 0 deletions src/endOfTomorrow/benchmark.js
@@ -0,0 +1,9 @@
// @flow
/* eslint-env mocha */
/* global suite, benchmark */

import endOfTomorrow from '.'

suite('endOfTomorrow', () => {
benchmark('date-fns', () => endOfTomorrow())
})
4 changes: 4 additions & 0 deletions 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
34 changes: 34 additions & 0 deletions 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
}

0 comments on commit e1a44d9

Please sign in to comment.