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

adornment

v0.0.3

Published

An useful ES7 class decorator lib.

Downloads

10

Readme

MIT License Built with babel jest pass

Adornment.js

An useful ES7 class decorator lib.

Install

npm install adornment --save

Menu

Descriptor Decorators

Class Decorators

Method Decorators

Dev Decorators

React Component Decorators

Usage

Descriptor Decorators

writable(writable: Bool)

class A {

    @writable(false)
    foo() {}

    bar() {}
}

var a = new A()
a.foo = 123 // throw error

notice: Can not use @writable with ES7 class props, that will cause initial fail.

enumerable(enumerable: Bool)

class A {

    @enumerable(true)
    foo() {}

    @enumerable(false)
    bar() {}

    @enumerable(true)
    yo() {}
}

var a = new A()

for(var key in a) {
    console.log(key)
}
// => 'foo' 'yo'

configurable(Bool)

class A {
    @configurable(false)
    foo() {}
}

delete A.prototype.foo // throw Error

Class Decorators

decorate(methods: Array<Function>)

Decorate methods on your class.

function bar() {
    return 'barrrr'
}

@decorate([bar])
class A {

    foo(a, b) {
        return a + b
    }

}

var a = new A()
a.bar() // => 'barrrr'

autoBind

@autoBind
class A {

    foo() {
        return this
    }
    
}

var a = new A()
var _foo = a.foo

_foo() === a // true

NOTICE: @autoBind should put AFTER ALL other decorators. For example:

// this will NOT work

@trace
class A {

    @autoBind
    @defer
    foo() {
        return this
    }

    bar() {
        return this
    }
}
// works fine

@trace
@autoBind
class A {

    @defer
    foo() {
        return this
    }

    bar() {
        return this
    }
}

Method Decorators

bindWith(target: Object)

var q = {qq: 123}

class A {

    @bindWith(q)
    foo() {
        return this
    }
}

var a = new A()
var _foo = a.foo

console.log(_foo() === q) // true

memoize

class A {

    addCount = 0

    @memoize
    add(a, b) {
        this.addCount ++
        return a + b
    }
}

var a = new A()

a.add(1, 2) // => 3
a.add(1, 2) // => 3
a.add(1, 2) // => 3
console.log(a.addCount) // => 1
a.add(2, 2) // => 4
console.log(a.addCount) // => 2

curry

WIP

curryRight

WIP

defer

class A {

    @defer
    foo() {
        console.log('foo')
    }
}

var a = new A()

a.foo()
console.log('bar')

/*
=>
bar
foo
*/

delay(wait: Number)

class A {

    @delay(10)
    foo() {
        console.log('foo')
    }
}

var a = new A()

a.foo()
console.log('bar')

/*
=>
bar
foo
*/

throttle(wait: Number)

class A {

    fooCount = 0

    @throttle(100)
    foo() {
        this.fooCount ++
    }
}

var a = new A()

a.foo()
a.foo()
a.foo()
console.log(a.fooCount) // => 1

setTimeout(()=> {
    a.foo()
    console.log(a.fooCount) // => 2
}, 150)

debounce(wait: Number)

class A {

    fooCount = 0

    @debounce(100)
    foo() {
        this.fooCount ++
    }
}

var a = new A()

a.foo()
a.foo()
console.log(a.fooCount) // => 0

setTimeout(()=> {
    console.log(a.fooCount) // => 1
}, 150)

once

class A {
    fooCount = 0

    @once
    foo(a, b) {
        this.fooCount ++
        return a + b
    }
}

var a = new A()
console.log(a.foo(1, 1)) // => 2
// Always return null after triggered once.
console.log(a.foo(1, 1)) // => null
console.log(a.foo(1, 1)) // => null

before(times)

class A {

    @before(3)
    foo(a, b) {
        return a + b
    }
}

var a = new A()

a.foo(1, 2) // => 3
a.foo(1, 2) // => 3
a.foo(1, 2) // => null

after(times)

class A {

    @after(3)
    foo(a, b) {
        return a + b
    }
}

var a = new A()

a.foo(1, 2) // => null
a.foo(1, 2) // => null
a.foo(1, 2) // => null
a.foo(1, 2) // => 3

argWith(func: Function)

class A {

    @argWith(function(...args) {
        if(typeof args[0] === "object" && args.length === 1) {
            let {a, b} = args[0]
            return [a,b]
        }

        return args
    })
    foo(a, b) {
        return a + b
    }

}

var a = new A()
a.foo(1,2) // => 3
a.foo({a:1, b:2}) // => 3

Dev Decorators

trace({logger, blackList, whiteList, perf})

Log function input, output and cost time.

  • logger: Function: You can use cust logger like debug, default use console.log
class A {

    @trace({
        perf: true
    })
    add(a, b) {
        console.log('adding')
        return a + b
    }
}

var a = new A()
a.add(1,2)
/*
=>
@trace: add with args: [1, 2] this: a
adding
@trace: add return 3 cost 0.012345678 ms.
*/

with class:

@trace({
    blackList: ['foo'],
    perf: true,
})
class A {

    foo(bar) {
        console.log('bar')
    }

    add(a, b) {
        console.log('adding')
        return a + b
    }
}

var a = new A()
a.add(1,2)
a.bar()
/*
=>
@trace: add with args: [1, 2] this: a
adding
@trace: add return 3 cost 0.012345678 ms.
bar
*/

deprecated(hint)

class A {

    @deprecated(`Please use 'newAdd()'`)
    add(a, b) {
        return a + b
    }

    newAdd() {
    }
}

var a = new A()
console.log(a.add(1,2))
/*
=>
Warning: add is deprecated. Please use 'newAdd()'
3
*/

React Component Decorators

traceComponent({logger, perf})

Trace the React component lifecycle. Same as @trace with

whiteList: [
'getInitialState',
'getDefaultProps',
'componentWillMount',
'componentDidMount',
'componentWillReceiveProps',
'shouldComponentUpdate',
'componentWillUpdate',
'componentDidUpdate',
'componentWillUnmount',
'render'
]
@traceComponent
class A extends React.Component {

    foo() {
        console.log('foo')
    }

    shouldComponentUpdate(nextProps, nextState) {
        return true
    }
}
/*
=>
@trace: shouldComponentUpdate with args: [{}, {}] this: ReactElement
@trace: shouldComponentUpdate return true cost 0.012345678 ms.
*/

reactMixin(Mixin)

WIP

Test

Tests with jest.

npm test

License

MIT