Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat null as an invalid date (closes #537) #557

Merged
merged 3 commits into from Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -366,6 +366,14 @@ for the list of changes made since `v2.0.0-alpha.1`.

- **BREAKING**: The Bower & UMD/CDN package versions are no longer supported.

- **BREAKING**: `null` now is not a valid date. `isValid(null)` returns `false`;
`toDate(null)` returns an invalid date. Since `toDate` is used internally
by all the functions, operations over `null` will also return an invalid date.
[See #537](https://github.com/date-fns/date-fns/issues/537) for the reasoning.

- `toDate` (previously `parse`) and `isValid` functions now accept `any` type
as the first argument.

## [1.28.5] - 2017-05-19

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/fp/isValid/index.js.flow
Expand Up @@ -63,4 +63,4 @@ type CurriedFn4<A, B, C, D, R> = <A>(a: A) => CurriedFn3<B, C, D, R>
| <A,B,C>(a: A, b: B, c: C) => CurriedFn1<D, R>
| <A,B,C,D>(a: A, b: B, c: C, d: D) => R

declare module.exports: CurriedFn1<Date | string | number, boolean>
declare module.exports: CurriedFn1<any, boolean>
2 changes: 1 addition & 1 deletion src/fp/isValidWithOptions/index.js.flow
Expand Up @@ -63,4 +63,4 @@ type CurriedFn4<A, B, C, D, R> = <A>(a: A) => CurriedFn3<B, C, D, R>
| <A,B,C>(a: A, b: B, c: C) => CurriedFn1<D, R>
| <A,B,C,D>(a: A, b: B, c: C, d: D) => R

declare module.exports: CurriedFn2<Options, Date | string | number, boolean>
declare module.exports: CurriedFn2<Options, any, boolean>
2 changes: 1 addition & 1 deletion src/fp/toDate/index.js.flow
Expand Up @@ -63,4 +63,4 @@ type CurriedFn4<A, B, C, D, R> = <A>(a: A) => CurriedFn3<B, C, D, R>
| <A,B,C>(a: A, b: B, c: C) => CurriedFn1<D, R>
| <A,B,C,D>(a: A, b: B, c: C, d: D) => R

declare module.exports: CurriedFn1<Date | string | number, Date>
declare module.exports: CurriedFn1<any, Date>
2 changes: 1 addition & 1 deletion src/fp/toDateWithOptions/index.js.flow
Expand Up @@ -63,4 +63,4 @@ type CurriedFn4<A, B, C, D, R> = <A>(a: A) => CurriedFn3<B, C, D, R>
| <A,B,C>(a: A, b: B, c: C) => CurriedFn1<D, R>
| <A,B,C,D>(a: A, b: B, c: C, d: D) => R

declare module.exports: CurriedFn2<Options, Date | string | number, Date>
declare module.exports: CurriedFn2<Options, any, Date>
2 changes: 1 addition & 1 deletion src/isValid/index.js
Expand Up @@ -12,7 +12,7 @@ import toDate from '../toDate/index.js'
*
* Time value of Date: http://es5.github.io/#x15.9.1.1
*
* @param {Date|String|Number} date - the date to check
* @param {*} date - the date to check
* @param {Options} [options] - the object with options. See [Options]{@link https://date-fns.org/docs/Options}
* @param {0|1|2} [options.additionalDigits=2] - passed to `toDate`. See [toDate]{@link https://date-fns.org/docs/toDate}
* @returns {Boolean} the date is valid
Expand Down
2 changes: 1 addition & 1 deletion src/isValid/index.js.flow
Expand Up @@ -50,6 +50,6 @@ type Locale = {
}

declare module.exports: (
date: Date | string | number,
date: any,
options?: Options
) => boolean
5 changes: 5 additions & 0 deletions src/isValid/test.js
Expand Up @@ -25,6 +25,11 @@ describe('isValid', function () {
assert(isValid(NaN) === false)
})

it('treats null as an invalid date', function () {
var result = isValid(null)
assert(result === false)
})

it('throws `RangeError` if `options.additionalDigits` is not convertable to 0, 1, 2 or undefined', function () {
// $ExpectedMistake
var block = isValid.bind(null, new Date(), {additionalDigits: NaN})
Expand Down
8 changes: 7 additions & 1 deletion src/toDate/index.js
Expand Up @@ -54,12 +54,14 @@ var patterns = {
* Function accepts complete ISO 8601 formats as well as partial implementations.
* ISO 8601: http://en.wikipedia.org/wiki/ISO_8601
*
* If the argument is null, it is treated as an invalid date.
*
* If all above fails, the function passes the given argument to Date constructor.
*
* **Note**: *all* Date arguments passed to any *date-fns* function is processed by `toDate`.
* All *date-fns* functions will throw `RangeError` if `options.additionalDigits` is not 0, 1, 2 or undefined.
*
* @param {Date|String|Number} argument - the value to convert
* @param {*} argument - the value to convert
* @param {Options} [options] - the object with options. See [Options]{@link https://date-fns.org/docs/Options}
* @param {0|1|2} [options.additionalDigits=2] - the additional number of digits in the extended year format
* @returns {Date} the parsed date in the local time zone
Expand All @@ -82,6 +84,10 @@ export default function toDate (argument, dirtyOptions) {
throw new TypeError('1 argument required, but only ' + arguments.length + ' present')
}

if (argument === null) {
return new Date(NaN)
}

var options = dirtyOptions || {}

var additionalDigits = options.additionalDigits === undefined ? DEFAULT_ADDITIONAL_DIGITS : Number(options.additionalDigits)
Expand Down
2 changes: 1 addition & 1 deletion src/toDate/index.js.flow
Expand Up @@ -50,6 +50,6 @@ type Locale = {
}

declare module.exports: (
argument: Date | string | number,
argument: any,
options?: Options
) => Date
6 changes: 6 additions & 0 deletions src/toDate/test.js
Expand Up @@ -247,6 +247,12 @@ describe('toDate', function () {
assert(result instanceof Date)
assert(isNaN(result))
})

it('returns Invalid Date if argument is null', function () {
var result = toDate(null)
assert(result instanceof Date)
assert(isNaN(result))
})
})

it('implicitly converts options', function () {
Expand Down
28 changes: 14 additions & 14 deletions typings.d.ts
Expand Up @@ -650,7 +650,7 @@ declare module 'date-fns' {
namespace isTuesday {}

function isValid (
date: Date | string | number,
date: any,
options?: Options
): boolean
namespace isValid {}
Expand Down Expand Up @@ -952,7 +952,7 @@ declare module 'date-fns' {
namespace subYears {}

function toDate (
argument: Date | string | number,
argument: any,
options?: Options
): Date
namespace toDate {}
Expand Down Expand Up @@ -3493,10 +3493,10 @@ declare module 'date-fns/fp' {
const isTuesdayWithOptions: CurriedFn2<Options, Date | string | number, boolean>
namespace isTuesdayWithOptions {}

const isValid: CurriedFn1<Date | string | number, boolean>
const isValid: CurriedFn1<any, boolean>
namespace isValid {}

const isValidWithOptions: CurriedFn2<Options, Date | string | number, boolean>
const isValidWithOptions: CurriedFn2<Options, any, boolean>
namespace isValidWithOptions {}

const isWednesday: CurriedFn1<Date | string | number, boolean>
Expand Down Expand Up @@ -3769,10 +3769,10 @@ declare module 'date-fns/fp' {
const subYearsWithOptions: CurriedFn3<Options, number, Date | string | number, Date>
namespace subYearsWithOptions {}

const toDate: CurriedFn1<Date | string | number, Date>
const toDate: CurriedFn1<any, Date>
namespace toDate {}

const toDateWithOptions: CurriedFn2<Options, Date | string | number, Date>
const toDateWithOptions: CurriedFn2<Options, any, Date>
namespace toDateWithOptions {}
}

Expand Down Expand Up @@ -8372,7 +8372,7 @@ declare module 'date-fns/esm' {
namespace isTuesday {}

function isValid (
date: Date | string | number,
date: any,
options?: Options
): boolean
namespace isValid {}
Expand Down Expand Up @@ -8674,7 +8674,7 @@ declare module 'date-fns/esm' {
namespace subYears {}

function toDate (
argument: Date | string | number,
argument: any,
options?: Options
): Date
namespace toDate {}
Expand Down Expand Up @@ -11215,10 +11215,10 @@ declare module 'date-fns/esm/fp' {
const isTuesdayWithOptions: CurriedFn2<Options, Date | string | number, boolean>
namespace isTuesdayWithOptions {}

const isValid: CurriedFn1<Date | string | number, boolean>
const isValid: CurriedFn1<any, boolean>
namespace isValid {}

const isValidWithOptions: CurriedFn2<Options, Date | string | number, boolean>
const isValidWithOptions: CurriedFn2<Options, any, boolean>
namespace isValidWithOptions {}

const isWednesday: CurriedFn1<Date | string | number, boolean>
Expand Down Expand Up @@ -11491,10 +11491,10 @@ declare module 'date-fns/esm/fp' {
const subYearsWithOptions: CurriedFn3<Options, number, Date | string | number, Date>
namespace subYearsWithOptions {}

const toDate: CurriedFn1<Date | string | number, Date>
const toDate: CurriedFn1<any, Date>
namespace toDate {}

const toDateWithOptions: CurriedFn2<Options, Date | string | number, Date>
const toDateWithOptions: CurriedFn2<Options, any, Date>
namespace toDateWithOptions {}
}

Expand Down Expand Up @@ -17239,7 +17239,7 @@ interface dateFns {
): boolean

isValid(
date: Date | string | number,
date: any,
options?: Options
): boolean

Expand Down Expand Up @@ -17495,7 +17495,7 @@ interface dateFns {
): Date

toDate(
argument: Date | string | number,
argument: any,
options?: Options
): Date
}