Skip to content

Commit

Permalink
Treat null as an invalid date (closes #537)
Browse files Browse the repository at this point in the history
  • Loading branch information
kossnocorp committed Sep 13, 2017
1 parent 7333078 commit 21ee450
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
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
4 changes: 4 additions & 0 deletions src/toDate/index.js
Expand Up @@ -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)
Expand Down
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

0 comments on commit 21ee450

Please sign in to comment.