decko
A collection of the most useful decorators.
Installation
Available on npm:
npm i -S decko
Usage
Each decorator method is available as a named import.
import { bind, memoize, debounce } from 'decko';
@bind
class Example {
@bind
foo() {
return this;
}
}
let e = new Example();
assert.equal(e.foo(), e);
@memoize
Cache values returned from the decorated function.
Uses the first argument as a cache key.
Cache keys are always converted to strings.
Options:
caseSensitive: false
- Makes cache keys case-insensitive
cache: {}
- Presupply cache storage, for seeding or sharing entries
class Example {
@memoize
expensive(key) {
let start = Date.now();
while (Date.now()-start < 500) key++;
return key;
}
}
let e = new Example();
let one = e.expensive(1);
let two = e.expensive(1);
let three = e.expensive(2);
@debounce
Throttle calls to the decorated function. To debounce means "call this at most once per N ms".
All outward function calls get collated into a single inward call, and only the latest (most recent) arguments as passed on to the debounced function.
Options:
delay: 0
- The number of milliseconds to buffer calls for.
class Example {
@debounce
foo() {
return this;
}
}
let e = new Example();
for (let i=1000; i--) e.foo();
License
MIT