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-helper

v1.0.3

Published

Provides useful functions for creating ES6 decorators

Downloads

3

Readme

decorator-helper

Provides useful functions for creating ES6 decorators

Installation

via npm

npm i --save decorator-helper

via yarn

yarn add decorator-helper

API

defineClassDecorator

create a decorator for class

definition
declare function defineClassDecorator<TArgs extends any[] = []>(
    decorator: (target: Function, ...args: TArgs) => void | Function,
    isFactory?: boolean
): ClassDecorator
No parameter decorator
import { defineClassDecorator } from 'decorator-helpers'

const MyDecorator = defineClassDecorator(target => {})

//target=TestClass
@MyDecorator
class TestClass {}
Decorator factory
const MyDecorator = defineClassDecorator((target, text) => {}, true)

//target=TestClass  text='hello'
@MyDecorator('hello')
class TestClass {}
Optional parameters decorator
const MyDecorator = defineClassDecorator((target, text) => {})

//target=TestClass1  text='hello'
@MyDecorator('hello')
class TestClass1 {}

//target=TestClass1  text=undefined
@MyDecorator
class TestClass2 {}

defineMethodDecorator

create a decorator for class method

definition
declare function defineMethodDecorator<TArgs extends any[] = []>(
    decorator: (
        target: Object,
        propertyKey: string | symbol,
        descriptor: TypedPropertyDescriptor<Function>,
        ...args: TArgs
    ) => void | TypedPropertyDescriptor<Function>,
    isFactory?: boolean
): MethodDecorator
No parameter decorator
import { defineMethodDecorator } from 'decorator-helpers'

const MyDecorator = defineMethodDecorator((target, key, descriptor) => {})

class TestClass {
    //target=TestClass.prototype  key='method1'  descriptor={ value: TestClass.prototype.method1 }
    @MyDecorator
    method1() {
        return 'hello'
    }

    //target=TestClass  key='method2'  descriptor={ value: TestClass.method2 }
    @MyDecorator
    static method2() {
        return 'world'
    }
}
Decorator factory
import { defineMethodDecorator } from 'decorator-helpers'

const MyDecorator = defineMethodDecorator((target, key, descriptor, text) => {}, true)

class TestClass {
    //target=TestClass.prototype  key='method1'  descriptor={ value: TestClass.prototype.method1 } text='name'
    @MyDecorator('name')
    method1() {
        return 'hello'
    }
}
Optional parameters decorator
import { defineMethodDecorator } from 'decorator-helpers'

const MyDecorator = defineMethodDecorator((target, key, descriptor, text) => {})

class TestClass {
    //target=TestClass.prototype  key='method1'  descriptor={ value: TestClass.prototype.method1 } text='name'
    @MyDecorator('name')
    method1() {
        return 'hello'
    }

    //target=TestClass.prototype  key='method1'  descriptor={ value: TestClass.prototype.method1 } text=undefined
    @MyDecorator
    method2() {
        return 'hello'
    }
}

definePropertyDecorator

create a decorator for class property

definition
declare function definePropertyDecorator<TArgs extends any[] = []>(
    decorator: (
        target: Object,
        propertyKey: string | symbol
        ...args: TArgs
    ) => void,
    isFactory?: boolean
): PropertyDecorator
No parameter decorator
import { definePropertyDecorator } from 'decorator-helpers'

const MyDecorator = definePropertyDecorator((target, key) => {})

class TestClass {
    //target=TestClass.prototype  key='id'
    @MyDecorator
    id = ''

    //target=TestClass  key='fullname'
    @MyDecorator
    static fullname = 'hello'
}
Decorator factory
const MyDecorator = definePropertyDecorator((target, key, text) => {}, true)

class TestClass {
    //target=TestClass.prototype  key='id'  text='hello'
    @MyDecorator('hello')
    id = ''
}
Optional parameters decorator
const MyDecorator = definePropertyDecorator((target, key, text) => {})

class TestClass {
    //target=TestClass.prototype  key='id'  text='hello'
    @MyDecorator('hello')
    id1 = ''

    //target=TestClass.prototype  key='id2'  text=undefined
    @MyDecorator
    id2 = ''
}

defineAccessorDecorator

create a decorator for accessor

definition
declare function defineAccessorDecorator<TArgs extends any[] = []>(
    decorator: (
        target: Object,
        propertyKey: string | symbol,
        descriptor: PropertyDescriptor
        ...args: TArgs
    ) => void | PropertyDescriptor,
    isFactory?: boolean
): AccessorDecorator
No parameter decorator
import { defineAccessorDecorator } from 'decorator-helpers'

const MyDecorator = defineAccessorDecorator((target, key, descriptor) => {})

class TestClass {
    //target=TestClass.prototype  key='id'  descriptor={ get:Function; set:Function }
    @MyDecorator
    get id() {
        return ''
    }
    set id(value) {}

    //target=TestClass  key='fullname'  descriptor={ get:Function }
    @MyDecorator
    static get fullname() {
        return ''
    }
}
Decorator factory
const MyDecorator = defineAccessorDecorator((target, key, descriptor, text) => {}, true)

class TestClass {
    //target=TestClass.prototype  key='id'  descriptor={ get:Function } text='hello'
    @MyDecorator('hello')
    get id() {
        return ''
    }
}
Optional parameters decorator
const MyDecorator = defineAccessorDecorator((target, key, descriptor, text) => {})

class TestClass {
    //target=TestClass.prototype  key='id'  descriptor={ get:Function } text='hello'
    @MyDecorator('hello')
    get id() {
        return ''
    }

    //target=TestClass.prototype  key='id'  descriptor={ get:Function } text=undefined
    @MyDecorator
    get fullname() {
        return ''
    }
}

defineParameterDecorator

create a decorator for method parameter

definition
declare function defineParameterDecorator<TArgs extends any[] = []>(
    decorator: (target: Object, propertyKey: string | symbol, parameterIndex: number, ...args: TArgs) => void,
    isFactory?: boolean
): ParameterDecorator
No parameter decorator
import { defineParameterDecorator } from 'decorator-helpers'

const MyDecorator = defineParameterDecorator((target, key, index) => {})

class TestClass {
    //target=TestClass key='method1' index=0
    method1(@MyDecorator arg1) {}

    //target=TestClass  key='method2'  index=1
    @MyDecorator
    static method2(arg1, @MyDecorator arg2) {}
}
Decorator factory
const MyDecorator = defineParameterDecorator((target, key, index, text) => {}, true)

class TestClass {
    //target=TestClass key='method1' index=0  text='hello'
    method1(@MyDecorator('hello') arg1) {}
}
Optional parameters decorator
const MyDecorator = defineParameterDecorator((target, key, index, text) => {})

class TestClass {
    //target=TestClass key='method1' index=0  text='hello'
    method1(@MyDecorator('hello') arg1) {}

    //target=TestClass  key='method2'  index=1  text=undefined
    @MyDecorator
    static method2(arg1, @MyDecorator arg2) {}
}

defineCompatibleDecorator

create a decorator for all targets. (class, method, property, accessor, parameter)

definition
declare interface DecoratingTarget {
    type: 'class' | 'method' | 'property' | 'accessor' | 'parameter'
    target: Object //class or class prototype
    propertyKey: string | symble //will be undefined if type=class
    descriptor: PropertyDescriptor //will be undefined if type=class or property or parameter
    parameterIndex: number //type=parameter only
}

declare function defineParameterDecorator<TArgs extends any[] = []>(
    decorator: (target: DecoratingTarget, ...args: TArgs) => Function | PropertyDescriptor | void,
    isFactory?: boolean
): CompatibleDecorator
No parameter decorator
import { defineCompatibleDecorator } from 'decorator-helpers'

const MyDecorator = defineCompatibleDecorator(target => {})

//target={ type:'class', target:TestClass }
@MyDecorator
class TestClass {
    //target={ type:'method', target:TestClass.prototype, propertyKey:'method1', descriptor:{ value: Function } }
    @MyDecorator
    //target={ type: 'parameter', target:TestClass.prototype, propertyKey:'method1', descriptor:{ value: Function } }
    method1(@MyDecorator arg1) {}

    //target={ type:'property', target:TestClass.prototype, propertyKey:'id' }
    @MyDecorator
    id = 1

    //target={ type:'accessor', target:TestClass.prototype, propertyKey:'fullname', descriptor: { get: Function } }
    @MyDecorator
    get fullname() {
        return ''
    }
}
Decorator factory
const MyDecorator = defineCompatibleDecorator((target, text) => {}, true)

//target={ type:'class', target:TestClass }
@MyDecorator('this is class')
class TestClass {
    //target={ type:'method', target:TestClass.prototype, propertyKey:'method1', descriptor:{ value: Function } }
    @MyDecorator('this is method')
    //target={ type: 'parameter', target:TestClass.prototype, propertyKey:'method1', descriptor:{ value: Function } }
    method1(@MyDecorator('this is parameter') arg1) {}

    //target={ type:'property', target:TestClass.prototype, propertyKey:'id' }
    @MyDecorator('this is property')
    id = 1

    //target={ type:'accessor', target:TestClass.prototype, propertyKey:'fullname', descriptor: { get: Function } }
    @MyDecorator('this is accessor')
    get fullname() {
        return ''
    }
}
Optional parameters decorator
const MyDecorator = defineCompatibleDecorator((target, text) => {})

//target={ type:'class', target:TestClass }
@MyDecorator
class TestClass {
    //target={ type:'method', target:TestClass.prototype, propertyKey:'method1', descriptor:{ value: Function } }
    @MyDecorator('this is method')
    //target={ type: 'parameter', target:TestClass.prototype, propertyKey:'method1', descriptor:{ value: Function } }
    method1(@MyDecorator arg1) {}

    //target={ type:'property', target:TestClass.prototype, propertyKey:'id' }
    @MyDecorator('this is property')
    id = 1

    //target={ type:'accessor', target:TestClass.prototype, propertyKey:'fullname', descriptor: { get: Function } }
    @MyDecorator
    get fullname() {
        return ''
    }
}