From 01170ede653215bc0eae4932bec0d5d3c66d16f3 Mon Sep 17 00:00:00 2001 From: Sasha Koss Date: Wed, 13 Sep 2017 16:41:04 +0700 Subject: [PATCH] Treat null as an invalid date (closes #537) --- CHANGELOG.md | 4 ++++ src/isValid/test.js | 5 +++++ src/toDate/index.js | 4 ++++ src/toDate/test.js | 6 ++++++ 4 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59b3638657..b2ea6e0277 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -366,6 +366,10 @@ 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. + ## [1.28.5] - 2017-05-19 ### Fixed 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..7dc505413e 100644 --- a/src/toDate/index.js +++ b/src/toDate/index.js @@ -82,6 +82,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/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 () {