npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

decorator-reflect

v2.4.5

Published

装饰器反射库

Downloads

621

Readme

Decorator build + reflection tool

Use to create decorators or decorator factories (decorators with parameters), merge existing decorators, and get typescript class information (properties, methods, method parameters < limited typescript>, and their decorators and types < limited typescript>) just like Java reflection does.

The DecoratorBuilder build shows how to use @Test() or @Test when the factory of the decorator has no arguments

Environment

  • node v12

Typescript Demo

ts-decorator-reflect-test

Javascript Demo

js-decorator-reflect-test

Installation

npm i decorator-reflect -s

API

import {ReflectUtil, DecoratorBuilder} from "decorator-reflect";

Usage

Basic Usage

import {ReflectUtil, DecoratorBuilder} from "decorator-reflect";

const Log = DecoratorBuilder.create()
    .method((target, propertyKey, descriptor, option) => ({
        ...descriptor,
        value: function(...args: object[]) {
            console.time();
            try {
                console.log('call method:', propertyKey, ', paramaters:', args);
                const returnValue = descriptor.value.apply(this, args)
                console.log('call method:', propertyKey, ', return:', returnValue);
                return returnValue;
            } finally {
                console.timeEnd();
            }
        }
    })).build();

class Demo1Class {
    constructor() {
    }
    @Log
    public method() {
        return 'test';
    }
}

new Demo1Class().method()
import {ReflectUtil, DecoratorBuilder} from "decorator-reflect";

/**
 * 创建装饰器工厂 TestAnnotation
 */
const TestAnnotation = DecoratorBuilder.create<string>()
    .method(((target, propertyKey, descriptor, option, definition) => {
        console.log('**********method');
        console.log(target); // TestClass {}
        console.log(propertyKey); // prop
        console.log(option); // test property
        console.log(definition);
        /**
         MethodDefinition {
  decorators: [ DecoratorDefinition { type: [Function], option: 'test method' } ],
  name: 'method',
  parameters: [ ParameterDefinition { decorators: [], type: [Function: String] } ],
  returnType: [Function: Boolean],
  isStatic: false
}
         */
    })).property(((target, propertyKey, option, definition) => {
        console.log('**********property');
        console.log(target); // [Function: TestClass]
        console.log(propertyKey); // prop
        console.log(option); // test property
        console.log(definition);
        /**
         PropertyDefinition {
  decorators: [ DecoratorDefinition { type: [Function], option: 'test property' } ],
  type: [Function: Boolean],
  name: 'prop',
  isStatic: true
}
         */
    })).parameter((target, propertyKey, parameterIndex, option, definition) => {
        console.log('**********parameter');
        console.log(target); // [Function: TestClass]
        console.log(propertyKey); // undefined
        console.log(parameterIndex); // 1
        console.log(definition);
        /**
         ParameterDefinition {
  decorators: [
    DecoratorDefinition { type: [Function], option: 'test parameter' }
  ],
  type: [Function: String]
}
         */
    }).class((target, option, definition) => {
        console.log('**********class');
        console.log(target); // [Function: TestClass]
        console.log(option); // test class
        console.log(definition); // ClassDefinition
    }).build();

const Test = DecoratorBuilder.create().class().build();

@Test()
@TestAnnotation('test class')
class TestClass {

    @TestAnnotation('test property')
    static prop?: boolean;

    constructor(a: number, @TestAnnotation('test parameter') b: string) {
    }
    @TestAnnotation('test method')
    method(a: string): boolean {
        return true;
    }
}
// Get reflection information
// console.log(ReflectUtil.getDefinition(TestClass));

Merge multiple existing decorators

import {DecoratorBuilder, ReflectUtil} from "decorator-reflect";
const Test: ClassDecorator = (target) => {
    // do something
}
const Test2: ClassDecorator = (target) => {
    // do something
}
const Test3: MethodDecorator = (target) => {
    // do something
}

const TestWrapper = DecoratorBuilder.create().class(Test).class(Test2).method(Test3).build();

@TestWrapper()
class TestClass {
    @TestWrapper()
    test() {}
}

console.log(ReflectUtil.getDefinition(TestClass));
// export type ClassDefinition<T extends Function> = {
//     name: string;
//     type: T;
//     decorators: DecoratorDefinition[];
//     properties: PropertyDefinition[];
//     methods: MethodDefinition[];
//     parameters: ParameterDefinition[];
// }

中文