Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/misc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* ===========================================================
MISCELLANEOUS
- LITERALS
- CLASS_TRANSFORM
- CLONE
- PRUNE
- FACTORY FUNCTIONS
Expand Down Expand Up @@ -56,6 +57,65 @@ export function literals(): never {
NoTransformConfigurationError("misc.literals");
}

/* -----------------------------------------------------------
CLASS_TRANSFORM
----------------------------------------------------------- */
/**
* Transform input into a class instance.
*
* Transforms a primitive input data into an instance following type `T`. This function
* converts plain objects, arrays, and primitive values into the specified class type,
* instantiating any necessary class constructors and applying the correct prototype.
*
* For reference, this `typia.misc.classTransform()` function does not validate the input value
* type. It assumes that the input value can be transformed to the type `T`. If you need
* type validation, it would be better to call {@link assertClassTransform} function instead.
*
* @template T Type of the target class
* @param input A value to be transformed into class instance
* @return Transformed class instance
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function classTransform<T>(input: unknown): T;

/**
* @internal
*/
export function classTransform(): never {
NoTransformConfigurationError("misc.classTransform");
}

/**
* Transform input into a class instance with type assertion.
*
* Transforms a primitive input data into an instance following type `T`, with type assertion.
* This function converts plain objects, arrays, and primitive values into the specified class type,
* instantiating any necessary class constructors and applying the correct prototype.
*
* When `input` value is not compatible with the type `T`, it throws an
* {@link TypeGuardError} or custom error generated by *errorFactory*. Otherwise,
* if there's no problem with the `input` value, transformed class instance would be returned.
*
* @template T Type of the target class
* @param input A value to be transformed into class instance
* @param errorFactory Custom error factory. Default is `TypeGuardError`
* @return Transformed class instance
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function assertClassTransform<T>(
input: unknown,
errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),
): T;

/**
* @internal
*/
export function assertClassTransform(): never {
NoTransformConfigurationError("misc.assertClassTransform");
}

/* -----------------------------------------------------------
CLONE
----------------------------------------------------------- */
Expand Down
59 changes: 59 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,65 @@ export function random(): never {
NoTransformConfigurationError("random");
}

/* -----------------------------------------------------------
CLASS TRANSFORMATION
----------------------------------------------------------- */
/**
* Transform input into a class instance.
*
* Transforms a primitive input data into an instance following type `T`. This function
* converts plain objects, arrays, and primitive values into the specified class type,
* instantiating any necessary class constructors and applying the correct prototype.
*
* For reference, this `typia.classTransform()` function does not validate the input value
* type. It assumes that the input value can be transformed to the type `T`. If you need
* type validation, it would be better to call {@link assertClassTransform} function instead.
*
* @template T Type of the target class
* @param input A value to be transformed into class instance
* @return Transformed class instance
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function classTransform<T>(input: unknown): T;

/**
* @internal
*/
export function classTransform(): never {
NoTransformConfigurationError("classTransform");
}

/**
* Transform input into a class instance with type assertion.
*
* Transforms a primitive input data into an instance following type `T`, with type assertion.
* This function converts plain objects, arrays, and primitive values into the specified class type,
* instantiating any necessary class constructors and applying the correct prototype.
*
* When `input` value is not compatible with the type `T`, it throws an
* {@link TypeGuardError} or custom error generated by *errorFactory*. Otherwise,
* if there's no problem with the `input` value, transformed class instance would be returned.
*
* @template T Type of the target class
* @param input A value to be transformed into class instance
* @param errorFactory Custom error factory. Default is `TypeGuardError`
* @return Transformed class instance
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function assertClassTransform<T>(
input: unknown,
errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),
): T;

/**
* @internal
*/
export function assertClassTransform(): never {
NoTransformConfigurationError("assertClassTransform");
}

/* -----------------------------------------------------------
FACTORY FUNCTIONS
----------------------------------------------------------- */
Expand Down
92 changes: 92 additions & 0 deletions src/programmers/misc/MiscAssertClassTransformProgrammer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import ts from "typescript";

import { IdentifierFactory } from "../../factories/IdentifierFactory";
import { StatementFactory } from "../../factories/StatementFactory";
import { TypeFactory } from "../../factories/TypeFactory";

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

import { AssertProgrammer } from "../AssertProgrammer";
import { FeatureProgrammer } from "../FeatureProgrammer";
import { FunctionProgrammer } from "../helpers/FunctionProgrammer";
import { MiscClassTransformProgrammer } from "./MiscClassTransformProgrammer";

export namespace MiscAssertClassTransformProgrammer {
export const decompose = (props: {
context: ITypiaContext;
functor: FunctionProgrammer;
type: ts.Type;
name: string | undefined;
init?: ts.Expression | undefined;
}): FeatureProgrammer.IDecomposed => {
const assert: FeatureProgrammer.IDecomposed = AssertProgrammer.decompose({
...props,
config: {
equals: false,
guard: false,
},
});
const classTransform: FeatureProgrammer.IDecomposed = MiscClassTransformProgrammer.decompose({
...props,
validated: true,
});
return {
functions: {
...assert.functions,
...classTransform.functions,
},
statements: [
...assert.statements,
...classTransform.statements,
StatementFactory.constant({
name: "__assert",
value: assert.arrow,
}),
StatementFactory.constant({
name: "__classTransform",
value: classTransform.arrow,
}),
],
arrow: ts.factory.createArrowFunction(
undefined,
undefined,
[
IdentifierFactory.parameter("input", TypeFactory.keyword("unknown")),
AssertProgrammer.Guardian.parameter({
context: props.context,
init: props.init,
}),
],
classTransform.arrow.type,
undefined,
ts.factory.createCallExpression(
ts.factory.createIdentifier("__classTransform"),
undefined,
[
ts.factory.createCallExpression(
ts.factory.createIdentifier("__assert"),
undefined,
[ts.factory.createIdentifier("input")],
),
],
),
),
};
};

export const write = (props: IProgrammerProps): ts.CallExpression => {
const functor: FunctionProgrammer = new FunctionProgrammer(
props.modulo.getText(),
);
const result: FeatureProgrammer.IDecomposed = decompose({
...props,
functor,
});
return FeatureProgrammer.writeDecomposed({
modulo: props.modulo,
functor,
result,
});
};
}
Loading