NaN option

This commit is contained in:
Sebastian Seedorf
2020-05-26 20:35:54 +02:00
parent 836b1449bf
commit d33ce96d18
2 changed files with 12 additions and 2 deletions

View File

@@ -1,10 +1,13 @@
import { Validator, Args } from "../mod.ts";
export function isNumber(): Validator {
export function isNumber(
{ allowNaN = false }: { allowNaN?: boolean } = {},
): Validator {
return {
type: "isNumber",
check: (value: any) => {
if (value === null || value === undefined) return;
if (allowNaN && Number.isNaN(value)) return;
if (!Number.isFinite(value)) {
return {};
}
@@ -15,11 +18,14 @@ export function isNumber(): Validator {
};
}
export function isInteger(): Validator {
export function isInteger(
{ allowNaN = false }: { allowNaN?: boolean } = {},
): Validator {
return {
type: "isInteger",
check: (value: any) => {
if (value === null || value === undefined) return;
if (allowNaN && Number.isNaN(value)) return;
if (!Number.isInteger(value)) {
return {};
}