123 lines
2.3 KiB
Markdown
123 lines
2.3 KiB
Markdown
### `decorateWith(fn, ...args)`
|
|
|
|
Creates a new ([legacy](https://babeljs.io/docs/en/babel-plugin-syntax-decorators#legacy)) method decorator from a function decorator, for instance, allows using Lodash's functions as decorators:
|
|
|
|
```js
|
|
import { decorateWith } from '@vates/decorate-with'
|
|
|
|
class Foo {
|
|
@decorateWith(lodash.debounce, 150)
|
|
bar() {
|
|
// body
|
|
}
|
|
}
|
|
```
|
|
|
|
### `decorateClass(class, map)`
|
|
|
|
Decorates a number of accessors and methods directly, without using the decorator syntax:
|
|
|
|
```js
|
|
import { decorateClass } from '@vates/decorate-with'
|
|
|
|
class Foo {
|
|
get bar() {
|
|
// body
|
|
}
|
|
|
|
set bar(value) {
|
|
// body
|
|
}
|
|
|
|
baz() {
|
|
// body
|
|
}
|
|
}
|
|
|
|
decorateClass(Foo, {
|
|
// getter and/or setter
|
|
bar: {
|
|
// without arguments
|
|
get: lodash.memoize,
|
|
|
|
// with arguments
|
|
set: [lodash.debounce, 150],
|
|
},
|
|
|
|
// method (with or without arguments)
|
|
baz: lodash.curry,
|
|
})
|
|
```
|
|
|
|
The decorated class is returned, so you can export it directly.
|
|
|
|
To apply multiple transforms to an accessor/method, you can either call `decorateClass` multiple times or use [`@vates/compose`](https://www.npmjs.com/package/@vates/compose):
|
|
|
|
```js
|
|
decorateClass(Foo, {
|
|
baz: compose([
|
|
[lodash.debounce, 150]
|
|
lodash.curry,
|
|
])
|
|
})
|
|
```
|
|
|
|
### `decorateObject(object, map)`
|
|
|
|
Decorates an object the same way `decorateClass()` decorates a class:
|
|
|
|
```js
|
|
import { decorateObject } from '@vates/decorate-with'
|
|
|
|
const object = {
|
|
get bar() {
|
|
// body
|
|
},
|
|
|
|
set bar(value) {
|
|
// body
|
|
},
|
|
|
|
baz() {
|
|
// body
|
|
},
|
|
}
|
|
|
|
decorateObject(object, {
|
|
// getter and/or setter
|
|
bar: {
|
|
// without arguments
|
|
get: lodash.memoize,
|
|
|
|
// with arguments
|
|
set: [lodash.debounce, 150],
|
|
},
|
|
|
|
// method (with or without arguments)
|
|
baz: lodash.curry,
|
|
})
|
|
```
|
|
|
|
### `perInstance(fn, ...args)`
|
|
|
|
Helper to decorate the method by instance instead of for the whole class.
|
|
|
|
This is often necessary for caching or deduplicating calls.
|
|
|
|
```js
|
|
import { perInstance } from '@vates/decorateWith'
|
|
|
|
class Foo {
|
|
@decorateWith(perInstance, lodash.memoize)
|
|
bar() {
|
|
// body
|
|
}
|
|
}
|
|
```
|
|
|
|
Because it's a normal function, it can also be used with `decorateClass`, with `compose` or even by itself.
|
|
|
|
### `decorateMethodsWith(class, map)`
|
|
|
|
> Deprecated alias for [`decorateClass(class, map)`](#decorateclassclass-map).
|