DecorateThis
Simple, vanilla JS type checking through ES7 decorators ...and a few other decorators, to boot.
- Installation
- Changelog
- Documentation
If you like this project, be sure to check out FluxThis, the immutable Flux framework by AddThis.
Type Validation
Throw errors when unexpected types are provided or returned from class or object functions. For more details, see the Type Validator API.
; { thisx = 0; thisy = 0; } // The first param of this method takes a Point, and will throw // a type error if a non-Point is passed @ // The method returns a Number. If it doesn't, a type error will // be thrown before the value is returned. @ { let squaredDistance = pointx - thisx ** 2 + pointy - thisy ** 2; return Math; } // Two number args, no return value @ @ { thisx += x; thisy += y; }
Memoization
Automatically memoize functions for greater efficiency
;let obj = // Results of the function are stored in a map, which maps arguments // to the function's result. This expensive func is only run a single // time for a given a/b pair. @memoize { return Math; };
Property descriptors
; @ @ @ { /* ... */ }
Currying
Build up long argument lists with currying
let obj = @curry { return a + b + c; } let addToFive = obj; // Functionlet addToFiveAndThree = ; // Functionlet sum = ; // 15
Debouncing
Rate-limit expensive or frequently called functions
let obj = @ // call after 500ms of no further calls { console; }
Promises
Similar to returns
, but validates the fulfillment value of a promise
let obj = @ { return { ; }; }
Incorporating in your project
npm install decorate-this
- Build your project with Babel
- Enable stage 1 (experimental) features
- Profit