Skip to content
This repository was archived by the owner on Nov 3, 2024. It is now read-only.
/ ts-metadata-helper Public archive

[ES7 reflect metadata wrapper in TypeScript][archived][2016]

Notifications You must be signed in to change notification settings

alxptr/ts-metadata-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ts-metadata-helper

An implementation of wrapper ES7 reflect metadata at TypeScript 2.0.

Features

Compatible with the Angular 2.0.0 annotations.

Dependencies

Installation

First you need to install the npm module:

npm install ts-metadata-helper --save

Use

import {Input, Output, Component} from '@angular/core';
import {PropertyAnnotationFactory, IAnnotation, MetadataHelper} from 'ts-metadata-helper/index';

...

class ViewFieldMetadata {
    configValue:number;

    constructor(config) {
        this.configValue = config.configValue;
    }
}

const ViewField:IAnnotation = PropertyAnnotationFactory(ViewFieldMetadata);

@Component({
    template: '<div>Template</div>'
})
class View {
    @ViewField({configValue: 100})
    private field1:string;

    @ViewField({configValue: 200})
    private field2:number;

    @Input()
    private field3:number;

    @Input()
    private field4:number;

    @Output()
    private field5:number;
}

const viewInstance:View = new View();

expect(Object.keys(MetadataHelper.findPropertyMetadata(viewInstance, ViewField))).toEqual(['field1', 'field2']);
expect(Object.keys(MetadataHelper.findPropertyMetadata(viewInstance, Input))).toEqual(['field3', 'field4']);
expect(Object.keys(MetadataHelper.findPropertyMetadata(viewInstance, Output))).toEqual(['field5']);

const annotationMetadataHolder:IAnnotationMetadataHolder = MetadataHelper.findPropertyMetadata(viewInstance, ViewField);
expect(Reflect.get(Reflect.get(annotationMetadataHolder, 'field1'), 'configValue')).toBe(100);
expect(Reflect.get(Reflect.get(annotationMetadataHolder, 'field2'), 'configValue')).toBe(200);

expect(
    Reflect.get(MetadataHelper.findAnnotationsMetaData(View, Component)[0], 'template')
).toEqual('<div>Template</div>');

Publish

npm run deploy

License

Licensed under MIT.

About

[ES7 reflect metadata wrapper in TypeScript][archived][2016]

Topics

Resources

Stars

Watchers

Forks