diff --git a/CHANGELOG.md b/CHANGELOG.md index 59b3638657..aeded1fd21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/fp/isValid/index.js.flow b/src/fp/isValid/index.js.flow index aa98db831a..60e11ab7b5 100644 --- a/src/fp/isValid/index.js.flow +++ b/src/fp/isValid/index.js.flow @@ -63,4 +63,4 @@ type CurriedFn4 = (a: A) => CurriedFn3 | (a: A, b: B, c: C) => CurriedFn1 | (a: A, b: B, c: C, d: D) => R -declare module.exports: CurriedFn1 +declare module.exports: CurriedFn1 diff --git a/src/fp/isValidWithOptions/index.js.flow b/src/fp/isValidWithOptions/index.js.flow index afcfb6b713..3b17d9b08e 100644 --- a/src/fp/isValidWithOptions/index.js.flow +++ b/src/fp/isValidWithOptions/index.js.flow @@ -63,4 +63,4 @@ type CurriedFn4 = (a: A) => CurriedFn3 | (a: A, b: B, c: C) => CurriedFn1 | (a: A, b: B, c: C, d: D) => R -declare module.exports: CurriedFn2 +declare module.exports: CurriedFn2 diff --git a/src/fp/toDate/index.js.flow b/src/fp/toDate/index.js.flow index f59bc03899..1e903eb3bb 100644 --- a/src/fp/toDate/index.js.flow +++ b/src/fp/toDate/index.js.flow @@ -63,4 +63,4 @@ type CurriedFn4 = (a: A) => CurriedFn3 | (a: A, b: B, c: C) => CurriedFn1 | (a: A, b: B, c: C, d: D) => R -declare module.exports: CurriedFn1 +declare module.exports: CurriedFn1 diff --git a/src/fp/toDateWithOptions/index.js.flow b/src/fp/toDateWithOptions/index.js.flow index ea9a8f1ae8..96674a9f9b 100644 --- a/src/fp/toDateWithOptions/index.js.flow +++ b/src/fp/toDateWithOptions/index.js.flow @@ -63,4 +63,4 @@ type CurriedFn4 = (a: A) => CurriedFn3 | (a: A, b: B, c: C) => CurriedFn1 | (a: A, b: B, c: C, d: D) => R -declare module.exports: CurriedFn2 +declare module.exports: CurriedFn2 diff --git a/src/isValid/index.js b/src/isValid/index.js index eff0fc8790..57b9fc6e84 100644 --- a/src/isValid/index.js +++ b/src/isValid/index.js @@ -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 diff --git a/src/isValid/index.js.flow b/src/isValid/index.js.flow index b97eaa0b07..a7177efe99 100644 --- a/src/isValid/index.js.flow +++ b/src/isValid/index.js.flow @@ -50,6 +50,6 @@ type Locale = { } declare module.exports: ( - date: Date | string | number, + date: any, options?: Options ) => boolean diff --git a/src/isValid/test.js b/src/isValid/test.js index 261c2049ad..40ed1aaea6 100644 --- a/src/isValid/test.js +++ b/src/isValid/test.js @@ -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}) diff --git a/src/toDate/index.js b/src/toDate/index.js index c34a29253d..f26e1706e5 100644 --- a/src/toDate/index.js +++ b/src/toDate/index.js @@ -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 @@ -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) diff --git a/src/toDate/index.js.flow b/src/toDate/index.js.flow index 8817897f9c..e03b8a4c9d 100644 --- a/src/toDate/index.js.flow +++ b/src/toDate/index.js.flow @@ -50,6 +50,6 @@ type Locale = { } declare module.exports: ( - argument: Date | string | number, + argument: any, options?: Options ) => Date diff --git a/src/toDate/test.js b/src/toDate/test.js index 80120b8835..1d14a36495 100644 --- a/src/toDate/test.js +++ b/src/toDate/test.js @@ -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 () { diff --git a/typings.d.ts b/typings.d.ts index ee4942857b..6428fcebed 100644 --- a/typings.d.ts +++ b/typings.d.ts @@ -650,7 +650,7 @@ declare module 'date-fns' { namespace isTuesday {} function isValid ( - date: Date | string | number, + date: any, options?: Options ): boolean namespace isValid {} @@ -952,7 +952,7 @@ declare module 'date-fns' { namespace subYears {} function toDate ( - argument: Date | string | number, + argument: any, options?: Options ): Date namespace toDate {} @@ -3493,10 +3493,10 @@ declare module 'date-fns/fp' { const isTuesdayWithOptions: CurriedFn2 namespace isTuesdayWithOptions {} - const isValid: CurriedFn1 + const isValid: CurriedFn1 namespace isValid {} - const isValidWithOptions: CurriedFn2 + const isValidWithOptions: CurriedFn2 namespace isValidWithOptions {} const isWednesday: CurriedFn1 @@ -3769,10 +3769,10 @@ declare module 'date-fns/fp' { const subYearsWithOptions: CurriedFn3 namespace subYearsWithOptions {} - const toDate: CurriedFn1 + const toDate: CurriedFn1 namespace toDate {} - const toDateWithOptions: CurriedFn2 + const toDateWithOptions: CurriedFn2 namespace toDateWithOptions {} } @@ -8372,7 +8372,7 @@ declare module 'date-fns/esm' { namespace isTuesday {} function isValid ( - date: Date | string | number, + date: any, options?: Options ): boolean namespace isValid {} @@ -8674,7 +8674,7 @@ declare module 'date-fns/esm' { namespace subYears {} function toDate ( - argument: Date | string | number, + argument: any, options?: Options ): Date namespace toDate {} @@ -11215,10 +11215,10 @@ declare module 'date-fns/esm/fp' { const isTuesdayWithOptions: CurriedFn2 namespace isTuesdayWithOptions {} - const isValid: CurriedFn1 + const isValid: CurriedFn1 namespace isValid {} - const isValidWithOptions: CurriedFn2 + const isValidWithOptions: CurriedFn2 namespace isValidWithOptions {} const isWednesday: CurriedFn1 @@ -11491,10 +11491,10 @@ declare module 'date-fns/esm/fp' { const subYearsWithOptions: CurriedFn3 namespace subYearsWithOptions {} - const toDate: CurriedFn1 + const toDate: CurriedFn1 namespace toDate {} - const toDateWithOptions: CurriedFn2 + const toDateWithOptions: CurriedFn2 namespace toDateWithOptions {} } @@ -17239,7 +17239,7 @@ interface dateFns { ): boolean isValid( - date: Date | string | number, + date: any, options?: Options ): boolean @@ -17495,7 +17495,7 @@ interface dateFns { ): Date toDate( - argument: Date | string | number, + argument: any, options?: Options ): Date }