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

ts-decorators-utils

v1.1.2

Published

Typescript decorator declarations and reflection tools

Downloads

41

Readme

ts-decorators-utils

What is it ?

Create the typescript decorator declaratively, Just like Java.

It also provides tools to implement 'reflection'

It can run on the web or in nodejs

Getting Start

install

npm i ts-decorator-utils

tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist",
    "module": "commonjs",
    "target": "es6",
    "declaration": true,
    "lib": [
      "es6",
      "dom",
      "esnext"
    ],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "strictNullChecks": true
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

Notice 'experimentalDecorators' and 'emitDecoratorMetadata'

Create a decorator

Create the required location, parameters, and metadata

import {DecoratorFactoryBuilder} from 'ts-decorators-utils';
const Demo = DecoratorFactoryBuilder.create()
    .class().method().property().parameter().build();




type Demo2MetadataValue = {
    value: string | number;
}

type Demo2PropOption = {
    value: string;
} | string | void;

type Demo2ParamOption = {
    value1: number;
    value2: number;
};
const Demo2 = DecoratorFactoryBuilder.create<Demo2MetadataValue>()
    .property<Demo2PropOption>(option => typeof option === 'string' ? )
    .parameter<Demo2ParamOption>()
    .build();

Use decorators

@Demo()
class TestClass {
    @Demo2('demo2PropTest')
    @Demo()
    public prop: string;
    @Demo()
    public staticProp: string;

    constructor(num: number) {}
    
    @Demo()
    public method(@Demo() param: string) {}
    
    @Demo()
    public static staticMethod(): string {
        return '';
    }
}

Getting information is similar to Java reflection

import {DecoratorUtil} from 'ts-decorators-utils';

// Is there a specified decorator on the target on the class, Some will return true or Object, and none will return undefined
console.log(DecoratorUtil.getMetadata(Demo, TestClass)); // true
console.log(DecoratorUtil.getMetadata(Demo, TestClass, 'staticMethod')); // true
console.log(DecoratorUtil.getMetadata(Demo, TestClass, 'staticProp')); // true
console.log(DecoratorUtil.getMetadata(Demo, TestClass.prototype, 'prop')); // true
console.log(DecoratorUtil.getMetadata(Demo, TestClass.prototype, 'method')); // true

console.log(DecoratorUtil.getMetadata(Demo, TestClass.prototype)); // undefined
console.log(DecoratorUtil.getMetadata(Demo, TestClass, 'staticMethod2')); // undefined

console.log(DecoratorUtil.getMethods(TestClass)); // [ [ 'staticMethod', [ [Function: String] ], undefined ] ]

Demo1

import {DecoratorFactoryBuilder, DecoratorUtil} from 'ts-decorators-utils';

const Value = DecoratorFactoryBuilder.create()
    .property().parameter().build();


class Demo {
    @Value()
    public a: string;

    public test(@Value() b: string) {
    }
}

console.log(DecoratorUtil.getMetadata(Value, Demo.prototype, 'a')); // true
console.log(DecoratorUtil.getMetadata(Value, Demo.prototype, 'test')); // undefined
console.log(DecoratorUtil.getMetadata(Value, Demo.prototype, 'test', 0)); // true

let metadataKey = Value.metadataKey; // is a Symbol Type
import {DecoratorFactoryBuilder, DecoratorUtil} from 'ts-decorators-utils';

const Value2 = DecoratorFactoryBuilder.create<string>()
    .method<string>().class<string>().build();

@Value2('demo2')
class Demo2 {

    @Value2('testStatic')
    public static testStatic(a: number): void {
    }

    constructor(public a: string, public b: number) {}

    @Value2('test')
    public test(b: string): Date {
        return new Date();
    }
}

console.log(DecoratorUtil.getMetadata(Value2, Demo2)); // demo2
console.log(DecoratorUtil.getMetadata(Value2, Demo2.prototype, 'test')); // test
console.log(DecoratorUtil.getMetadata(Value2, Demo2, 'testStatic')); // testStatic

// 可以获得类中 使用装饰器的实例方法 集合
console.log(DecoratorUtil.getMethods(Demo2.prototype)); // [ [ 'test', [ [Function: String] ], [Function: Date] ] ]
// 可以获得类中 使用装饰器的实例属性 集合 返回类型  [string, Constructor][]
console.log(DecoratorUtil.getProperties(Demo2.prototype)); // []
// 可以获得类中 使用装饰器的静态属性 集合  返回类型  [string, Constructor[], Constructor][]  数组中元组对应 方法名,方法参数类型数组,方法返回类型
console.log(DecoratorUtil.getMethods(Demo2)); // [ [ 'testStatic', [ [Function: Number] ], undefined ] ]
// 可以获得类中 使用装饰器的静态属性 集合
console.log(DecoratorUtil.getProperties(Demo2)); // []