typia
Version:
Superfast runtime validators with only one line
92 lines (86 loc) • 2.85 kB
text/typescript
import ts from "typescript";
import { IdentifierFactory } from "../../factories/IdentifierFactory";
import { StatementFactory } from "../../factories/StatementFactory";
import { TypeFactory } from "../../factories/TypeFactory";
import { IProject } from "../../transformers/IProject";
import { FeatureProgrammer } from "../FeatureProgrammer";
import { IsProgrammer } from "../IsProgrammer";
import { FunctionImporter } from "../helpers/FunctionImporter";
import { MiscPruneProgrammer } from "./MiscPruneProgrammer";
export namespace MiscIsPruneProgrammer {
export const decompose = (props: {
project: IProject;
importer: FunctionImporter;
type: ts.Type;
name: string | undefined;
}): FeatureProgrammer.IDecomposed => {
const is: FeatureProgrammer.IDecomposed = IsProgrammer.decompose({
...props,
equals: false,
});
const prune: FeatureProgrammer.IDecomposed = MiscPruneProgrammer.decompose({
...props,
validated: true,
});
return {
functions: {
...is.functions,
...prune.functions,
},
statements: [
...is.statements,
...prune.statements,
StatementFactory.constant("__is", is.arrow),
StatementFactory.constant("__prune", prune.arrow),
],
arrow: ts.factory.createArrowFunction(
undefined,
undefined,
[IdentifierFactory.parameter("input", TypeFactory.keyword("any"))],
is.arrow.type,
undefined,
ts.factory.createBlock(
[
ts.factory.createIfStatement(
ts.factory.createEquality(
ts.factory.createFalse(),
ts.factory.createCallExpression(
ts.factory.createIdentifier("__is"),
undefined,
[ts.factory.createIdentifier("input")],
),
),
ts.factory.createReturnStatement(ts.factory.createFalse()),
),
ts.factory.createExpressionStatement(
ts.factory.createCallExpression(
ts.factory.createIdentifier("__prune"),
undefined,
[ts.factory.createIdentifier("input")],
),
),
ts.factory.createReturnStatement(ts.factory.createTrue()),
],
true,
),
),
};
};
export const write =
(project: IProject) =>
(modulo: ts.LeftHandSideExpression) =>
(type: ts.Type, name?: string): ts.CallExpression => {
const importer: FunctionImporter = new FunctionImporter(modulo.getText());
const result: FeatureProgrammer.IDecomposed = decompose({
project,
importer,
type,
name,
});
return FeatureProgrammer.writeDecomposed({
modulo,
importer,
result,
});
};
}