Skip to content

Commit

Permalink
Migrate isValid function to TS (#2308)
Browse files Browse the repository at this point in the history
Co-authored-by: Tetiana <ttobin@protonmail.ch>
Co-authored-by: Sasha Koss <koss@nocorp.me>
  • Loading branch information
3 people committed Sep 16, 2021
1 parent 0e5d948 commit 237f831
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
16 changes: 10 additions & 6 deletions src/isValid/index.js → src/isValid/index.ts
@@ -1,3 +1,4 @@
import isDate from '../isDate/index'
import toDate from '../toDate/index'
import requiredArgs from '../_lib/requiredArgs/index'

Expand Down Expand Up @@ -45,22 +46,25 @@ import requiredArgs from '../_lib/requiredArgs/index'
*
* @example
* // For the valid date:
* var result = isValid(new Date(2014, 1, 31))
* const result = isValid(new Date(2014, 1, 31))
* //=> true
*
* @example
* // For the value, convertable into a date:
* var result = isValid(1393804800000)
* const result = isValid(1393804800000)
* //=> true
*
* @example
* // For the invalid date:
* var result = isValid(new Date(''))
* const result = isValid(new Date(''))
* //=> false
*/
export default function isValid(dirtyDate) {
export default function isValid(dirtyDate: unknown): boolean {
requiredArgs(1, arguments)

var date = toDate(dirtyDate)
return !isNaN(date)
if (!isDate(dirtyDate) && typeof dirtyDate !== 'number') {
return false
}
const date = toDate(dirtyDate)
return !isNaN(Number(date))
}
31 changes: 0 additions & 31 deletions src/isValid/test.js

This file was deleted.

31 changes: 31 additions & 0 deletions src/isValid/test.ts
@@ -0,0 +1,31 @@
/* eslint-env mocha */

import assert from 'assert'
import isValid from '.'

describe('isValid', function () {
it('returns true if the given date is valid', function () {
const result = isValid(new Date())
assert(result === true)
})

it('returns false if the given date is invalid', function () {
const result = isValid(new Date(''))
assert(result === false)
})

it('accepts a timestamp', function () {
assert(isValid(new Date(2014, 1 /* Feb */, 11).getTime()) === true)
assert(isValid(NaN) === false)
})

it('treats null as an invalid date', function () {
const result = isValid(null)
assert(result === false)
})

it('throws TypeError exception if passed less than 1 argument', function () {
// @ts-expect-error
assert.throws(isValid.bind(null), TypeError)
})
})

0 comments on commit 237f831

Please sign in to comment.