@matatbread/typia
Version:
Superfast runtime validators with only one line
66 lines (63 loc) • 2.64 kB
JavaScript
import ts from 'typescript';
import { ExpressionFactory } from '../../factories/ExpressionFactory.mjs';
import { OptionPredicator } from '../helpers/OptionPredicator.mjs';
/** @internal */
const check_number = (props) => {
const base = ts.factory.createStrictEquality(ts.factory.createStringLiteral("number"), ts.factory.createTypeOfExpression(props.input));
const addition = props.numeric === true
? OptionPredicator.finite(props.context.options)
? ts.factory.createCallExpression(ts.factory.createIdentifier("Number.isFinite"), undefined, [props.input])
: OptionPredicator.numeric(props.context.options)
? ts.factory.createLogicalNot(ts.factory.createCallExpression(ts.factory.createIdentifier("Number.isNaN"), undefined, [props.input]))
: null
: null;
const conditions = check_numeric_type_tags({
context: props.context,
atomic: props.atomic,
input: props.input,
addition,
});
return {
expected: props.atomic.getName(),
expression: addition !== null && conditions.length === 0
? ts.factory.createLogicalAnd(base, addition)
: base,
conditions,
};
};
/** @internal */
const check_numeric_type_tags = (props) => props.atomic.tags
.map((row) => row.filter((tag) => !!tag.validate))
.filter((row) => !!row.length)
.map((row) => [
...(props.addition === null
? []
: row.some((tag) => tag.kind === "type" &&
(tag.value === "int32" ||
tag.value === "uint32" ||
tag.value === "int64" ||
tag.value === "uint64" ||
tag.value === "float")) ||
row.some((tag) => tag.kind === "multipleOf" && typeof tag.value === "number") ||
(row.some((tag) => (tag.kind === "minimum" || tag.kind === "exclusiveMinimum") &&
typeof tag.value === "number") &&
row.some((tag) => (tag.kind === "maximum" || tag.kind === "exclusiveMaximum") &&
typeof tag.value === "number"))
? []
: [
{
expected: "number",
expression: props.addition,
},
]),
...row.map((tag) => ({
expected: `number & ${tag.name}`,
expression: ExpressionFactory.transpile({
transformer: props.context.transformer,
importer: props.context.importer,
script: tag.validate,
})(props.input),
})),
]);
export { check_number };
//# sourceMappingURL=check_number.mjs.map