diff --git a/projects/ngze/control-value-transformer/src/lib/.gitkeep b/projects/ngze/control-value-transformer/src/lib/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/projects/ngze/control-value-transformer/src/lib/metadata/control-value-transformer-metadata.interface.ts b/projects/ngze/control-value-transformer/src/lib/metadata/control-value-transformer-metadata.interface.ts new file mode 100644 index 0000000..94917e7 --- /dev/null +++ b/projects/ngze/control-value-transformer/src/lib/metadata/control-value-transformer-metadata.interface.ts @@ -0,0 +1,9 @@ +/** + * Interface that represents metadata related to control value transformer. + */ +export interface ControlValueTransformerMetadata { + /** + * Unique name for the control value transformer. + */ + name: string; +} diff --git a/projects/ngze/control-value-transformer/src/lib/metadata/control-value-transformer-metadata.utils.ts b/projects/ngze/control-value-transformer/src/lib/metadata/control-value-transformer-metadata.utils.ts new file mode 100644 index 0000000..b7707e0 --- /dev/null +++ b/projects/ngze/control-value-transformer/src/lib/metadata/control-value-transformer-metadata.utils.ts @@ -0,0 +1,45 @@ +import { Class } from 'utility-types'; + +import { Transformer } from '../shared'; + +import { ControlValueTransformerMetadata } from './control-value-transformer-metadata.interface'; + +/** + * Unique symbol used as a key to define and read metadata related to control value transformers. + */ +const CONTROL_VALUE_TRANSFORMER_METADATA = Symbol('CONTROL_VALUE_TRANSFORMER_METADATA'); + +/** + * Asserts the given transformer class is decorated with {@link Transformer}. + * @param transformer - Instance of control value transformer that should be decorated. + */ +const assertTransformerIsDecorated = (transformer: Transformer) => { + const hasMetadata = Reflect.hasMetadata(CONTROL_VALUE_TRANSFORMER_METADATA, transformer.constructor); + + if (!hasMetadata) { + throw new TypeError(`"${transformer.constructor}" is not decorated with ControlValueTransformer.`); + } +}; + +/** + * Returns metadata associate with the given control value transformer. + * @param transformer - Instance of control value transformer. + * @returns Control value transformer metadata. + */ +export const getControlValueTransformerMetadata = (transformer: Transformer): ControlValueTransformerMetadata => { + assertTransformerIsDecorated(transformer); + + return Reflect.getMetadata(CONTROL_VALUE_TRANSFORMER_METADATA, transformer.constructor); +}; + +/** + * Defines metadata on the given control value transformer class. + * @param transformerClass - Class that implements {@link Transformer}. + * @param metadata - Control value transformer metadata. + */ +export const definedControlValueTransformerMetadata = ( + transformerClass: Class, + metadata: ControlValueTransformerMetadata +) => { + return Reflect.defineMetadata(CONTROL_VALUE_TRANSFORMER_METADATA, metadata, transformerClass); +}; diff --git a/projects/ngze/control-value-transformer/src/lib/metadata/control-value-transformer.decorator.ts b/projects/ngze/control-value-transformer/src/lib/metadata/control-value-transformer.decorator.ts new file mode 100644 index 0000000..5fc3da1 --- /dev/null +++ b/projects/ngze/control-value-transformer/src/lib/metadata/control-value-transformer.decorator.ts @@ -0,0 +1,17 @@ +import { Class } from 'utility-types'; + +import { Transformer } from '../shared'; + +import { ControlValueTransformerMetadata } from './control-value-transformer-metadata.interface'; +import { definedControlValueTransformerMetadata } from './control-value-transformer-metadata.utils'; + +/** + * Decorator used to define required metadata on control value transformers. + * @param metadata - Control value transformer metadata. + */ +export const ControlValueTransformer = (metadata: ControlValueTransformerMetadata) => ( + transformerClass: Class +): Class => { + definedControlValueTransformerMetadata(transformerClass, metadata); + return transformerClass; +}; diff --git a/projects/ngze/control-value-transformer/src/lib/metadata/index.ts b/projects/ngze/control-value-transformer/src/lib/metadata/index.ts new file mode 100644 index 0000000..087635c --- /dev/null +++ b/projects/ngze/control-value-transformer/src/lib/metadata/index.ts @@ -0,0 +1,6 @@ +export { ControlValueTransformer } from './control-value-transformer.decorator'; +export { ControlValueTransformerMetadata } from './control-value-transformer-metadata.interface'; +export { + definedControlValueTransformerMetadata, + getControlValueTransformerMetadata, +} from './control-value-transformer-metadata.utils'; diff --git a/projects/ngze/control-value-transformer/src/lib/shared/index.ts b/projects/ngze/control-value-transformer/src/lib/shared/index.ts new file mode 100644 index 0000000..f56986a --- /dev/null +++ b/projects/ngze/control-value-transformer/src/lib/shared/index.ts @@ -0,0 +1 @@ +export { Transformer } from './transformer.interface'; diff --git a/projects/ngze/control-value-transformer/src/lib/shared/transformer.interface.ts b/projects/ngze/control-value-transformer/src/lib/shared/transformer.interface.ts new file mode 100644 index 0000000..53f2c8e --- /dev/null +++ b/projects/ngze/control-value-transformer/src/lib/shared/transformer.interface.ts @@ -0,0 +1,16 @@ +/** + * Interface that represents value transformer. + * `S` - Source value type. + * `T` - Target value type. + */ +export interface Transformer { + /** + * Transforms the given source value (`S`) to the target value type (`T`). + */ + toTarget: (sourceValue: S) => T; + + /** + * Transforms the given target value (`T`) to the source value type (`S`). + */ + toSource: (targetValue: T, currentSourceValue: S) => S; +} diff --git a/projects/ngze/control-value-transformer/src/public-api.ts b/projects/ngze/control-value-transformer/src/public-api.ts index 9475bea..381da94 100644 --- a/projects/ngze/control-value-transformer/src/public-api.ts +++ b/projects/ngze/control-value-transformer/src/public-api.ts @@ -1,3 +1,12 @@ /* * Public API Surface of Control Value Transformer */ + +import 'reflect-metadata'; + +export { + ControlValueTransformer, + ControlValueTransformerMetadata, + getControlValueTransformerMetadata, +} from './lib/metadata'; +export { Transformer } from './lib/shared';