diff --git a/CHANGELOG.md b/CHANGELOG.md index b1b9141777..10aa25640a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -292,6 +292,27 @@ This change log follows the format documented in [Keep a CHANGELOG]. - **BREAKING**: now `closestTo` and `closestIndexTo` don't throw an exception when the second argument is not an instance of array. +- **BREAKING**: now `isValid` doesn't throw an exception + if the first argument is not an instance of Date. + Instead, argument is converted beforehand using `toDate`. + + Examples: + | `isValid` argument | Before v2.0.0 | v2.0.0 onward | + |---------------------------|---------------|---------------| + | `new Date()` | `true` | `true` | + | `new Date('2016-01-01')` | `true` | `true` | + | `new Date('')` | `false` | `false` | + | `new Date(1488370835081)` | `true` | `true` | + | `new Date(NaN)` | `false` | `false` | + | `'2016-01-01'` | `TypeError` | `true` | + | `''` | `TypeError` | `false` | + | `1488370835081` | `TypeError` | `true` | + | `NaN` | `TypeError` | `false` | + + We introduce this change to make *date-fns* consistent with ECMAScript behavior + that try to coerce arguments to the expected type + (which is also the case with other *date-fns* functions). + - Every function now has `options` as the last argument which is passed to all its dependencies for consistency and future features. See [docs/Options.js](https://github.com/date-fns/date-fns/blob/master/docs/Options.js) diff --git a/src/fp/isValid/index.js.flow b/src/fp/isValid/index.js.flow index 6a4b522fff..39ada5eb51 100644 --- a/src/fp/isValid/index.js.flow +++ b/src/fp/isValid/index.js.flow @@ -45,4 +45,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 4ba9c84994..2f4fc7040d 100644 --- a/src/fp/isValidWithOptions/index.js.flow +++ b/src/fp/isValidWithOptions/index.js.flow @@ -45,4 +45,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 3fc7b753ad..ae5fc696f5 100644 --- a/src/isValid/index.js +++ b/src/isValid/index.js @@ -1,4 +1,4 @@ -import isDate from '../isDate/index.js' +import toDate from '../toDate/index.js' /** * @name isValid @@ -7,14 +7,14 @@ import isDate from '../isDate/index.js' * * @description * Returns false if argument is Invalid Date and true otherwise. + * Argument is converted to Date using `toDate`. See [toDate]{@link docs/toDate} * Invalid Date is a Date, whose time value is NaN. * * Time value of Date: http://es5.github.io/#x15.9.1.1 * - * @param {Date} date - the date to check + * @param {Date|String|Number} date - the date to check * @param {Options} [options] - the object with options. See [Options]{@link docs/Options} * @returns {Boolean} the date is valid - * @throws {TypeError} argument must be an instance of Date * * @example * // For the valid date: @@ -22,14 +22,16 @@ import isDate from '../isDate/index.js' * //=> true * * @example + * // For the value, convertable into a date: + * var result = isValid('2014-02-31') + * //=> true + * + * @example * // For the invalid date: * var result = isValid(new Date('')) * //=> false */ export default function isValid (dirtyDate, dirtyOptions) { - if (isDate(dirtyDate, dirtyOptions)) { - return !isNaN(dirtyDate) - } else { - throw new TypeError(toString.call(dirtyDate) + ' is not an instance of Date') - } + var date = toDate(dirtyDate, dirtyOptions) + return !isNaN(date) } diff --git a/src/isValid/index.js.flow b/src/isValid/index.js.flow index 384305e112..eab32cc1d6 100644 --- a/src/isValid/index.js.flow +++ b/src/isValid/index.js.flow @@ -32,6 +32,6 @@ type Locale = { } declare module.exports: ( - date: Date, + date: Date | string | number, options?: Options ) => boolean diff --git a/src/isValid/test.js b/src/isValid/test.js index 49c0114e1f..64e9efcede 100644 --- a/src/isValid/test.js +++ b/src/isValid/test.js @@ -15,9 +15,13 @@ describe('isValid', function () { assert(result === false) }) - it('throws an exception if the argument is not an instance of Date', function () { - // $ExpectedMistake - var block = isValid.bind(null, '') - assert.throws(block, TypeError, '[object String] is not an instance of Date') + it('accepts a string', function () { + assert(isValid(new Date(2014, 6 /* Jul */, 8).toString()) === true) + assert(isValid('') === false) + }) + + it('accepts a timestamp', function () { + assert(isValid(new Date(2014, 1 /* Feb */, 11).getTime()) === true) + assert(isValid(NaN) === false) }) }) diff --git a/typings.d.ts b/typings.d.ts index 98dd845cd1..ff4a3e4ebb 100644 --- a/typings.d.ts +++ b/typings.d.ts @@ -631,7 +631,7 @@ declare module 'date-fns' { namespace isTuesday {} function isValid ( - date: Date, + date: Date | string | number, options?: Options ): boolean namespace isValid {} @@ -3213,7 +3213,7 @@ declare module 'date-fns/fp' { let isTuesday: CurriedFn1 namespace isTuesday {} - let isValid: CurriedFn1 + let isValid: CurriedFn1 namespace isValid {} let isWednesday: CurriedFn1 @@ -3615,7 +3615,7 @@ declare module 'date-fns/fp' { let isTuesdayWithOptions: CurriedFn2 namespace isTuesdayWithOptions {} - let isValidWithOptions: CurriedFn2 + let isValidWithOptions: CurriedFn2 namespace isValidWithOptions {} let isWednesdayWithOptions: CurriedFn2 @@ -8352,7 +8352,7 @@ declare module 'date-fns/esm' { namespace isTuesday {} function isValid ( - date: Date, + date: Date | string | number, options?: Options ): boolean namespace isValid {} @@ -10934,7 +10934,7 @@ declare module 'date-fns/esm/fp' { let isTuesday: CurriedFn1 namespace isTuesday {} - let isValid: CurriedFn1 + let isValid: CurriedFn1 namespace isValid {} let isWednesday: CurriedFn1 @@ -11336,7 +11336,7 @@ declare module 'date-fns/esm/fp' { let isTuesdayWithOptions: CurriedFn2 namespace isTuesdayWithOptions {} - let isValidWithOptions: CurriedFn2 + let isValidWithOptions: CurriedFn2 namespace isValidWithOptions {} let isWednesdayWithOptions: CurriedFn2