# @vates/decorate-with

> Creates a decorator from a function wrapper

Latest version **2.1.0** (published 2024-01-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install @vates/decorate-with
pnpm add @vates/decorate-with
yarn add @vates/decorate-with
bun add @vates/decorate-with
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2024-01-29 |
| First published | 2020-06-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.10 |
| Dependencies | 0 |
| Unpacked size | 5.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 987 |
| Author | Vates SAS |
| Maintainers | b-nollet, arnogues, ggunullu, florent.beauchamp, mathieura, enishowk, tgoettelmann, julien-f, marsaud, olivierlambert, pdonias |
| Keywords | apply, decorator, factory, wrapper |

## Links

- npm: https://www.npmjs.com/package/@vates/decorate-with
- Repository: https://github.com/vatesfr/xen-orchestra
- Homepage: https://github.com/vatesfr/xen-orchestra/tree/master/@vates/decorate-with
- Issues: https://github.com/vatesfr/xen-orchestra/issues
- npm.io page: https://npm.io/package/@vates/decorate-with

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2024-01-29
- 2.0.0 — 2022-03-30
- 1.0.0 — 2021-12-16
- 0.1.0 — 2021-05-31
- 0.0.1 — 2020-06-12

## README

<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->

# @vates/decorate-with

[![Package Version](https://badgen.net/npm/v/@vates/decorate-with)](https://npmjs.org/package/@vates/decorate-with) ![License](https://badgen.net/npm/license/@vates/decorate-with) [![PackagePhobia](https://badgen.net/bundlephobia/minzip/@vates/decorate-with)](https://bundlephobia.com/result?p=@vates/decorate-with) [![Node compatibility](https://badgen.net/npm/node/@vates/decorate-with)](https://npmjs.org/package/@vates/decorate-with)

> Creates a decorator from a function wrapper

## Install

Installation of the [npm package](https://npmjs.org/package/@vates/decorate-with):

```sh
npm install --save @vates/decorate-with
```

## Usage

### `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).

## Contributions

Contributions are _very_ welcomed, either on the documentation or on
the code.

You may:

- report any [issue](https://github.com/vatesfr/xen-orchestra/issues)
  you've encountered;
- fork and create a pull request.

## License

[ISC](https://spdx.org/licenses/ISC) © [Vates SAS](https://vates.fr)

---
_Source: https://npm.io/package/@vates/decorate-with · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
