Adornment.js
An useful ES7 class decorator lib.
Install
npm install adornment --save
Menu
Descriptor Decorators
Class Decorators
Method Decorators
- bindWith(target: Object)
- memoize
- curry
- curryRight
- defer
- delay(wait: Number)
- throttle(wait: Number)
- debounce
- once
- before(times: Number)
- after(times: Number)
- argWith(func: Function)
Dev Decorators
React Component Decorators
Usage
Descriptor Decorators
writable(writable: Bool)
@ {} {} var a = afoo = 123 // throw error
notice: Can not use @writable
with ES7 class props, that will cause initial fail.
enumerable(enumerable: Bool)
@ {} @ {} @ {} var a = forvar key in a console// => 'foo' 'yo'
configurable(Bool)
@ {} delete Aprototypefoo // throw Error
Class Decorators
decorate(methods: Array<Function>)
Decorate methods on your class.
{ return 'barrrr'} @ { return a + b } var a = a // => 'barrrr'
autoBind
@autoBind { return this } var a = var _foo = afoo === a // true
NOTICE: @autoBind
should put AFTER ALL other decorators. For example:
// this will NOT work @trace @autoBind @defer { return this } { return this }
// works fine @trace@autoBind @defer { return this } { return this }
Method Decorators
bindWith(target: Object)
var q = qq: 123 @ { return this } var a = var _foo = afoo console // true
memoize
addCount = 0 @memoize { thisaddCount ++ return a + b } var a = a // => 3a // => 3a // => 3console // => 1a // => 4console // => 2
curry
WIP
curryRight
WIP
defer
@defer { console } var a = aconsole /*=>barfoo*/
delay(wait: Number)
@ { console } var a = aconsole /*=>barfoo*/
throttle(wait: Number)
fooCount = 0 @ { thisfooCount ++ } var a = aaaconsole // => 1
debounce(wait: Number)
fooCount = 0 @ { thisfooCount ++ } var a = aaconsole // => 0
once
fooCount = 0 @once { thisfooCount ++ return a + b } var a = console // => 2// Always return null after triggered once.console // => nullconsole // => null
before(times)
@ { return a + b } var a = a // => 3a // => 3a // => null
after(times)
@ { return a + b } var a = a // => nulla // => nulla // => nulla // => 3
argWith(func: Function)
@ { return a + b } var a = a // => 3a // => 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 useconsole.log
@ { console return a + b } var a = a/*=>@trace: add with args: [1, 2] this: aadding@trace: add return 3 cost 0.012345678 ms.*/
with class:
@ { console } { console return a + b } var a = aa/*=>@trace: add with args: [1, 2] this: aadding@trace: add return 3 cost 0.012345678 ms.bar*/
deprecated(hint)
@ { return a + b } { } var a = console/*=>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'
@traceComponentComponent { console } { return true }/*=>@trace: shouldComponentUpdate with args: [{}, {}] this: ReactElement@trace: shouldComponentUpdate return true cost 0.012345678 ms.*/
reactMixin(Mixin)
WIP
Test
npm test
License
MIT