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 🙏

© 2025 – Pkg Stats / Ryan Hefner

decorator-class

v1.0.15

Published

class装饰器扩展

Readme

decorator-class插件使用说明

  1. @TransformParma(arg):用在class的方法上面,参数可以为function或string,当参数为function时,会接受函数调用时真正的参数,将function(arg)执行完后的结果当作参数传给当前修饰的class方法,当参数为string时,若class的实例上存在名为string的方法,则会调用,将调用后的结果传给当前修饰的方法.见下面demo
  2. @CheckParma(arg):用在class的方法上面,参数可以为function或string,当参数为function时,会接受函数调用时真正的参数,当function(arg)执行完后的结果为true时,执行给当前修饰的class方法,当参数为string时,若class的实例上存在名为string的方法(执行后返回结果)或属性,当值为true时,执行当前修饰的方法.见下面demo
  3. @NextAction(fns,fne):用在class的方法上面,执行当前修饰的class方法,若返回为promise对象,则会将then的值传给fns,将catch的值传给fne。若不为promise则直接将值传给fns。参数(fns,fne)均可以为function或string(同上),见下面demo
  4. @InitState 作用于属性上,加上此注解后,实例对象上会自动注入一个方法,如下demo,则可使用this.resetFormdata方法重置formData
  5. @Before 作用于方法上,参数(fn:函数),若fn返回promise则会在then中执行当前方法。
const Test = opt => `${opt}-666`;

const isNumber = opt => isNaN(opt);

class Demo {

    @InitState
    formData = {
        id: ''
    }

    show=true;

    go(opt) {
        retunrn `${opt-555}`
    }

    isBoolean(opt){
        return Boolean(opt);
    }

    @TransformParma(Test)
    show1(opt) {
        console.log(opt)
    }

    @TransformParma('go')
    show2(opt) {
        console.log(opt)
    }

    @CheckParma(isNumber)
    show3(opt) {
        console.log(opt)
    }

    @CheckParma('isBoolean')
    show4(opt) {
        console.log(opt)
    }

    @CheckParma('show')
    show5(opt) {
        console.log(opt)
    }

    @NextAction('success','fail')
    ajax(flag) {
        return new Promise((resolve,reject)=> {
            if(flag) {
                console.log(true)
                resolve(true)
            } else {
                console.log(false)
                reject(false)
            }
        })
    }

    @NextAction('success')   
    test() {
        console.log(1);
        return 1;
    }
    

    success(data) {
        cosole.log(data+1)
    }
    fail(e) {
        cosole.log(e)
    }

    @Before(() => new Promise(resolve => {
        console.log('before:1')
        resolve()
    }))   
    testBefore() {
        console.log('before:2');
        return 1;
    }
}

// 调用
const demo = new Demo();

demo.show1('2'); // 打印2-666
demo.show2('2'); // 打印2-555

demo.show3('2'); // 方法不执行,不打印
demo.show4(true); // 方法执行,打印true
demo.show5('2'); // 方法执行,打印2

demo.ajax(true); // 先打印true,后打印1
demo.test(); // 先打印1,后打印2
demo.testBefore(); // 先打印before:1,后打印before:2

欢迎提出各种bug,有问题可以直接发送邮件到 [email protected]