Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate isValid function to TS #2308

Merged
merged 4 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions deno
Submodule deno added at 20fe9c
16 changes: 10 additions & 6 deletions src/isValid/index.js → src/isValid/index.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
})
})