Skip to content

Commit

Permalink
Clean argument using toDate in isValid (closes #440) (#455)
Browse files Browse the repository at this point in the history
* Clean argument using `toDate` in `isValid`

* `isValid` changelog entry

* Regenerate typings
  • Loading branch information
leshakoss committed May 4, 2017
1 parent 5649dcd commit 2bc0ddb
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/fp/isValid/index.js.flow
Expand Up @@ -45,4 +45,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, boolean>
declare module.exports: CurriedFn1<Date | string | number, boolean>
2 changes: 1 addition & 1 deletion src/fp/isValidWithOptions/index.js.flow
Expand Up @@ -45,4 +45,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, boolean>
declare module.exports: CurriedFn2<Options, Date | string | number, boolean>
18 changes: 10 additions & 8 deletions src/isValid/index.js
@@ -1,4 +1,4 @@
import isDate from '../isDate/index.js'
import toDate from '../toDate/index.js'

/**
* @name isValid
Expand All @@ -7,29 +7,31 @@ 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:
* var result = isValid(new Date(2014, 1, 31))
* //=> 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)
}
2 changes: 1 addition & 1 deletion src/isValid/index.js.flow
Expand Up @@ -32,6 +32,6 @@ type Locale = {
}

declare module.exports: (
date: Date,
date: Date | string | number,
options?: Options
) => boolean
12 changes: 8 additions & 4 deletions src/isValid/test.js
Expand Up @@ -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)
})
})
12 changes: 6 additions & 6 deletions typings.d.ts
Expand Up @@ -631,7 +631,7 @@ declare module 'date-fns' {
namespace isTuesday {}

function isValid (
date: Date,
date: Date | string | number,
options?: Options
): boolean
namespace isValid {}
Expand Down Expand Up @@ -3213,7 +3213,7 @@ declare module 'date-fns/fp' {
let isTuesday: CurriedFn1<Date | string | number, boolean>
namespace isTuesday {}

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

let isWednesday: CurriedFn1<Date | string | number, boolean>
Expand Down Expand Up @@ -3615,7 +3615,7 @@ declare module 'date-fns/fp' {
let isTuesdayWithOptions: CurriedFn2<Options, Date | string | number, boolean>
namespace isTuesdayWithOptions {}

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

let isWednesdayWithOptions: CurriedFn2<Options, Date | string | number, boolean>
Expand Down Expand Up @@ -8352,7 +8352,7 @@ declare module 'date-fns/esm' {
namespace isTuesday {}

function isValid (
date: Date,
date: Date | string | number,
options?: Options
): boolean
namespace isValid {}
Expand Down Expand Up @@ -10934,7 +10934,7 @@ declare module 'date-fns/esm/fp' {
let isTuesday: CurriedFn1<Date | string | number, boolean>
namespace isTuesday {}

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

let isWednesday: CurriedFn1<Date | string | number, boolean>
Expand Down Expand Up @@ -11336,7 +11336,7 @@ declare module 'date-fns/esm/fp' {
let isTuesdayWithOptions: CurriedFn2<Options, Date | string | number, boolean>
namespace isTuesdayWithOptions {}

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

let isWednesdayWithOptions: CurriedFn2<Options, Date | string | number, boolean>
Expand Down

0 comments on commit 2bc0ddb

Please sign in to comment.