Skip to content

Commit

Permalink
feat: 🎸 get rid of reflect-metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeevKatz committed Nov 12, 2020
1 parent 49e2d34 commit d7056b5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Transformer } from './shared';
/**
* Injection token that used to run {@link registerControlValueTransformers}.
*/
export const REGISTER_CONTROL_VALUE_TRANSFORMERS = new InjectionToken<void>('REGISTER_CONTROL_VALUE_TRANSFORMERS');
export const REGISTER_CONTROL_VALUE_TRANSFORMERS = new InjectionToken('REGISTER_CONTROL_VALUE_TRANSFORMERS');

/**
* Registration factory for new control value transformers.
Expand Down Expand Up @@ -52,5 +52,5 @@ export class ControlValueTransformerModule {
};
}

constructor(@Optional() @Inject(REGISTER_CONTROL_VALUE_TRANSFORMERS) registerControlValueTransformers: void) {}
constructor(@Optional() @Inject(REGISTER_CONTROL_VALUE_TRANSFORMERS) registerControlValueTransformers) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ import { ControlValueTransformerMetadata } from './control-value-transformer-met
*/
const CONTROL_VALUE_TRANSFORMER_METADATA = Symbol('CONTROL_VALUE_TRANSFORMER_METADATA');

/**
* Returns metadata property descriptor associate with the given control value transformer.
* @param transformer - Instance of control value transformer that should be decorated.
* @returns Transformer metadata property descriptor.
*/
const getTransformerMetadataPropertyDescriptor = (transformer: Transformer) => {
return Object.getOwnPropertyDescriptor(transformer.constructor, 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) {
if (!getTransformerMetadataPropertyDescriptor(transformer)) {
throw new TypeError(`"${transformer.constructor}" is not decorated with ControlValueTransformer.`);
}
};
Expand All @@ -28,8 +35,8 @@ const assertTransformerIsDecorated = (transformer: Transformer) => {
*/
export const getControlValueTransformerMetadata = (transformer: Transformer): ControlValueTransformerMetadata => {
assertTransformerIsDecorated(transformer);

return Reflect.getMetadata(CONTROL_VALUE_TRANSFORMER_METADATA, transformer.constructor);
const metadataPropertyDescriptor = getTransformerMetadataPropertyDescriptor(transformer);
return metadataPropertyDescriptor.value;
};

/**
Expand All @@ -41,5 +48,7 @@ export const definedControlValueTransformerMetadata = (
transformerClass: Class<Transformer>,
metadata: ControlValueTransformerMetadata
) => {
return Reflect.defineMetadata(CONTROL_VALUE_TRANSFORMER_METADATA, metadata, transformerClass);
return Object.defineProperty(transformerClass, CONTROL_VALUE_TRANSFORMER_METADATA, {
value: metadata,
});
};
2 changes: 0 additions & 2 deletions projects/ngze/control-value-transformer/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* Public API Surface of Control Value Transformer
*/

import 'reflect-metadata';

export {
ControlValueTransformer,
ControlValueTransformerMetadata,
Expand Down

0 comments on commit d7056b5

Please sign in to comment.