Skip to content

Commit

Permalink
Change currying functions to keyworded.
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Sep 14, 2024
1 parent 9e0b22f commit b61caf1
Show file tree
Hide file tree
Showing 118 changed files with 1,576 additions and 1,381 deletions.
80 changes: 43 additions & 37 deletions src/programmers/AssertProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { IdentifierFactory } from "../factories/IdentifierFactory";
import { StatementFactory } from "../factories/StatementFactory";
import { TypeFactory } from "../factories/TypeFactory";

import { IProject } from "../transformers/IProject";
import { IProgrammerProps } from "../transformers/IProgrammerProps";
import { ITypiaContext } from "../transformers/ITypiaContext";

import { CheckerProgrammer } from "./CheckerProgrammer";
import { FeatureProgrammer } from "./FeatureProgrammer";
Expand All @@ -14,24 +15,34 @@ import { OptionPredicator } from "./helpers/OptionPredicator";
import { check_object } from "./internal/check_object";

export namespace AssertProgrammer {
export const decompose = (props: {
project: IProject;
export interface IConfig {
equals: boolean;
guard: boolean;
}
export interface IProps extends IProgrammerProps {
config: IConfig;
}

export const decompose = (props: {
context: ITypiaContext;
config: IConfig;
importer: FunctionImporter;
type: ts.Type;
name: string | undefined;
init: ts.Expression | undefined;
init?: ts.Expression | undefined;
}): FeatureProgrammer.IDecomposed => {
const is: FeatureProgrammer.IDecomposed = IsProgrammer.decompose(props);
const is: FeatureProgrammer.IDecomposed = IsProgrammer.decompose({
...props,
equals: props.config.equals,

Check failure on line 36 in src/programmers/AssertProgrammer.ts

View workflow job for this annotation

GitHub Actions / NodeJS

Object literal may only specify known properties, and 'equals' does not exist in type '{ context: ITypiaContext; importer: FunctionImporter; config: IConfig; type: Type; name: string | undefined; }'.

Check failure on line 36 in src/programmers/AssertProgrammer.ts

View workflow job for this annotation

GitHub Actions / Build-and-Upload-to-pkg-pr-new-and-artifactory

Object literal may only specify known properties, and 'equals' does not exist in type '{ context: ITypiaContext; importer: FunctionImporter; config: IConfig; type: Type; name: string | undefined; }'.

Check failure on line 36 in src/programmers/AssertProgrammer.ts

View workflow job for this annotation

GitHub Actions / Bun

Object literal may only specify known properties, and 'equals' does not exist in type '{ context: ITypiaContext; importer: FunctionImporter; config: IConfig; type: Type; name: string | undefined; }'.
});
const composed: FeatureProgrammer.IComposed = CheckerProgrammer.compose({
...props,
config: {
prefix: "$a",
path: true,
trace: true,
numeric: OptionPredicator.numeric(props.project.options),
equals: props.equals,
numeric: OptionPredicator.numeric(props.context.options),
equals: props.config.equals,
atomist: (explore) => (entry) => (input) =>
[
...(entry.expression ? [entry.expression] : []),
Expand Down Expand Up @@ -83,8 +94,8 @@ export namespace AssertProgrammer {
),
]),
].reduce((x, y) => ts.factory.createLogicalAnd(x, y)),
combiner: combiner(props.equals)(props.project)(props.importer),
joiner: joiner(props.equals)(props.project)(props.importer),
combiner: combiner(props.config.equals)(props.context)(props.importer),
joiner: joiner(props.config.equals)(props.context)(props.importer),
success: ts.factory.createTrue(),
},
});
Expand All @@ -95,18 +106,18 @@ export namespace AssertProgrammer {
IdentifierFactory.parameter("input", TypeFactory.keyword("any")),
Guardian.parameter(props.init),
],
props.guard
props.config.guard
? ts.factory.createTypePredicateNode(
ts.factory.createToken(ts.SyntaxKind.AssertsKeyword),
ts.factory.createIdentifier("input"),
ts.factory.createTypeReferenceNode(
props.name ??
TypeFactory.getFullName(props.project.checker)(props.type),
TypeFactory.getFullName(props.context.checker)(props.type),
),
)
: ts.factory.createTypeReferenceNode(
props.name ??
TypeFactory.getFullName(props.project.checker)(props.type),
TypeFactory.getFullName(props.context.checker)(props.type),
),
undefined,
ts.factory.createBlock(
Expand Down Expand Up @@ -152,7 +163,7 @@ export namespace AssertProgrammer {
),
undefined,
),
...(props.guard === false
...(props.config.guard === false
? [
ts.factory.createReturnStatement(
ts.factory.createIdentifier(`input`),
Expand All @@ -179,31 +190,24 @@ export namespace AssertProgrammer {
};
};

export const write =
(project: IProject) =>
(modulo: ts.LeftHandSideExpression) =>
(props: boolean | { equals: boolean; guard: boolean }) =>
(type: ts.Type, name?: string, init?: ts.Expression): ts.CallExpression => {
if (typeof props === "boolean") props = { equals: props, guard: false };
const importer: FunctionImporter = new FunctionImporter(modulo.getText());
const result: FeatureProgrammer.IDecomposed = decompose({
...props,
project,
importer,
type,
name,
init,
});
return FeatureProgrammer.writeDecomposed({
modulo,
importer,
result,
});
};
export const write = (props: IProps): ts.CallExpression => {
const importer: FunctionImporter = new FunctionImporter(
props.modulo.getText(),
);
const result: FeatureProgrammer.IDecomposed = decompose({
...props,
importer,
});
return FeatureProgrammer.writeDecomposed({
modulo: props.modulo,
importer,
result,
});
};

const combiner =
(equals: boolean) =>
(project: IProject) =>
(project: ITypiaContext) =>
(importer: FunctionImporter): CheckerProgrammer.IConfig.Combiner =>
(explore: CheckerProgrammer.IExplore) => {
if (explore.tracable === false)
Expand Down Expand Up @@ -268,7 +272,9 @@ export namespace AssertProgrammer {
};

const assert_object =
(equals: boolean) => (project: IProject) => (importer: FunctionImporter) =>
(equals: boolean) =>
(project: ITypiaContext) =>
(importer: FunctionImporter) =>
check_object({
equals,
assert: true,
Expand Down Expand Up @@ -298,7 +304,7 @@ export namespace AssertProgrammer {

const joiner =
(equals: boolean) =>
(project: IProject) =>
(project: ITypiaContext) =>
(importer: FunctionImporter): CheckerProgrammer.IConfig.IJoiner => ({
object: assert_object(equals)(project)(importer),
array: (input, arrow) =>
Expand Down
48 changes: 27 additions & 21 deletions src/programmers/CheckerProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { MetadataObject } from "../schemas/metadata/MetadataObject";
import { MetadataTuple } from "../schemas/metadata/MetadataTuple";
import { MetadataTupleType } from "../schemas/metadata/MetadataTupleType";

import { IProject } from "../transformers/IProject";
import { ITypiaContext } from "../transformers/ITypiaContext";
import { TransformerError } from "../transformers/TransformerError";

import { FeatureProgrammer } from "./FeatureProgrammer";
Expand Down Expand Up @@ -98,37 +98,43 @@ export namespace CheckerProgrammer {
WRITERS
----------------------------------------------------------- */
export const compose = (props: {
project: IProject;
context: ITypiaContext;
config: IConfig;
importer: FunctionImporter;
type: ts.Type;
name: string | undefined;
}) =>
FeatureProgrammer.compose({
...props,
config: configure(props.project)(props.config)(props.importer),
config: configure(props.context)(props.config)(props.importer),
});

export const write =
(project: IProject) => (config: IConfig) => (importer: FunctionImporter) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
FeatureProgrammer.write(project)(configure(project)(config)(importer))(
importer,
);

export const write_object_functions =
(project: IProject) => (config: IConfig) => (importer: FunctionImporter) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
FeatureProgrammer.write_object_functions(
configure(project)(config)(importer),
)(importer);

export const write_union_functions =
(project: IProject) => (config: IConfig) => (importer: FunctionImporter) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
FeatureProgrammer.write_union_functions(
configure(project)({ ...config, numeric: false })(importer),
);

export const write_array_functions =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(collection: MetadataCollection): ts.VariableStatement[] =>
Expand Down Expand Up @@ -164,7 +170,7 @@ export namespace CheckerProgrammer {
);

export const write_tuple_functions =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(collection: MetadataCollection): ts.VariableStatement[] =>
Expand Down Expand Up @@ -197,7 +203,7 @@ export namespace CheckerProgrammer {
);

const configure =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter): FeatureProgrammer.IConfig => ({
types: {
Expand All @@ -218,7 +224,7 @@ export namespace CheckerProgrammer {
const collection: MetadataCollection = new MetadataCollection();
const result = MetadataFactory.analyze(
project.checker,
project.context,
project.transformer,
)({
escape: false,
constant: true,
Expand Down Expand Up @@ -289,7 +295,7 @@ export namespace CheckerProgrammer {
* @internal
*/
export const decode =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(
Expand Down Expand Up @@ -669,7 +675,7 @@ export namespace CheckerProgrammer {
};

const decode_array =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(input: ts.Expression, array: MetadataArray, explore: IExplore) => {
Expand Down Expand Up @@ -702,7 +708,7 @@ export namespace CheckerProgrammer {
};

const decode_array_inline =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(
Expand All @@ -726,7 +732,7 @@ export namespace CheckerProgrammer {
};

const decode_tuple =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(
Expand Down Expand Up @@ -761,7 +767,7 @@ export namespace CheckerProgrammer {
};

const decode_tuple_inline =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(
Expand Down Expand Up @@ -862,7 +868,7 @@ export namespace CheckerProgrammer {
};

const decode_escaped =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(input: ts.Expression, meta: Metadata, explore: IExplore): ts.Expression =>
Expand Down Expand Up @@ -895,7 +901,7 @@ export namespace CheckerProgrammer {
UNION TYPE EXPLORERS
----------------------------------------------------------- */
const explore_sets =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(
Expand All @@ -919,7 +925,7 @@ export namespace CheckerProgrammer {
);

const explore_maps =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(
Expand Down Expand Up @@ -963,7 +969,7 @@ export namespace CheckerProgrammer {
);

const explore_tuples =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(
Expand All @@ -985,7 +991,7 @@ export namespace CheckerProgrammer {
)(input, tuples, explore);

const explore_arrays =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(
Expand All @@ -1007,7 +1013,7 @@ export namespace CheckerProgrammer {
)(input, arrays, explore);

const explore_arrays_and_tuples =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(
Expand Down
10 changes: 5 additions & 5 deletions src/programmers/FeatureProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Metadata } from "../schemas/metadata/Metadata";
import { MetadataArray } from "../schemas/metadata/MetadataArray";
import { MetadataObject } from "../schemas/metadata/MetadataObject";

import { IProject } from "../transformers/IProject";
import { ITypiaContext } from "../transformers/ITypiaContext";

import { CheckerProgrammer } from "./CheckerProgrammer";
import { FunctionImporter } from "./helpers/FunctionImporter";
Expand Down Expand Up @@ -46,7 +46,7 @@ export namespace FeatureProgrammer {
* Initializer of metadata.
*/
initializer: (
project: IProject,
project: ITypiaContext,
) => (
importer: FunctionImporter,
) => (type: ts.Type) => [MetadataCollection, Metadata];
Expand Down Expand Up @@ -212,13 +212,13 @@ export namespace FeatureProgrammer {
}

export const compose = (props: {
project: IProject;
context: ITypiaContext;
config: IConfig;
importer: FunctionImporter;
type: ts.Type;
name: string | undefined;
}): IComposed => {
const [collection, meta] = props.config.initializer(props.project)(
const [collection, meta] = props.config.initializer(props.context)(
props.importer,
)(props.type);
return {
Expand Down Expand Up @@ -288,7 +288,7 @@ export namespace FeatureProgrammer {
);

export const write =
(project: IProject) =>
(project: ITypiaContext) =>
(config: IConfig) =>
(importer: FunctionImporter) =>
(type: ts.Type, name?: string): ts.ArrowFunction => {
Expand Down
Loading

0 comments on commit b61caf1

Please sign in to comment.