xen-orchestra/@vates/decorate-with
2022-02-22 12:34:41 +01:00
..
.npmignore feat: unified .npmignore for all packages 2021-04-07 13:58:14 +02:00
.USAGE.md chore: hide USAGE.md 2022-02-18 17:11:52 +01:00
index.js chore: enforce strict mode for CJS files 2022-02-22 12:34:41 +01:00
package.json feat(@vates/decorate-with): 1.0.0 2021-12-16 11:49:29 +01:00
README.md feat(decorate-with): perInstance helper 2021-12-16 11:48:48 +01:00

@vates/decorate-with

Package Version License PackagePhobia Node compatibility

Creates a decorator from a function wrapper

Install

Installation of the npm package:

> npm install --save @vates/decorate-with

Usage

decorateWith(fn, ...args)

Creates a new (legacy) method decorator from a function decorator, for instance, allows using Lodash's functions as decorators:

import { decorateWith } from '@vates/decorate-with'

class Foo {
  @decorateWith(lodash.debounce, 150)
  bar() {
    // body
  }
}

decorateMethodsWith(class, map)

Decorates a number of methods directly, without using the decorator syntax:

import { decorateMethodsWith } from '@vates/decorate-with'

class Foo {
  bar() {
    // body
  }

  baz() {
    // body
  }
}

decorateMethodsWith(Foo, {
  // without arguments
  bar: lodash.curry,

  // with arguments
  baz: [lodash.debounce, 150],
})

The decorated class is returned, so you can export it directly.

To apply multiple transforms to a method, you can either call decorateMethodsWith multiple times or use @vates/compose:

decorateMethodsWith(Foo, {
  bar: compose([
    [lodash.debounce, 150]
    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.

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 decorateMethodsWith, with compose or even by itself.

Contributions

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

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Vates SAS